npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@space-station/hotwire

v0.1.4

Published

An IOC depndency injection container for environments without decorators

Downloads

5

Readme

Hotwire IOC

Hotwire is a simple Dependency Injection container. It allows you to use DI in environments that don't support TypeScript decorator metadata (ex. Vite), which are standard on many other solutions.

Basic Usage

import { container, inject } from '@space-station/hotwire';

class Idol {}
class Whip {}

class HiddenTemple {
	constructor(
		public idol: Idol,
		public whip: Whip,
	) {}
}

inject(HiddenTemple, [Idol, Whip]);

container.register(Idol);
container.register(Whip);
container.register(HiddenTemple);

const temple = container.resolve(HiddenTemple);

console.log(temple.idol); // Idol{}
console.log(temple.whip); // Whip{}

The Gist

Hotwire works by registering any injectable resource (a class, instance, static value, etc.) with the container via a token (a constructor, symbol, string, etc.). When the container resolves a token, it will automatically pass required dependencies to class instances and apply any additional configuration of lifecycle handling to those values. This allows for any number of class instances to be connected to each other automatically.

Class Definition

Use the inject helper to mark a class constructor as being injected with dependencies. The first argument is the class to be marked as injectable. The second is an array of tokens representing injectable values registered with the container. At runtime the DI container will determine which dependencies must be resolved and passed into the class constructor.

The array of tokens passed to inject should resolve to match the parameter types of the constructor.

Registration

Any injectable value must be registered with the container prior to the first resolution. If registering a class constructor you can pass it as the only argument to register(). This will link a token of the constructor to the constructor itself. Alternatively, you can specify the token to use. When doing so, the token must be utilized when calling inject() or resolve().

container.register('SecretTemple', HiddenTemple);

container.resolve('SecretTemple'); // HiddenTemple{}

Tokens can be constructor functions, strings, or symbols and should be unique. If you register another provider with an existing token it will override the previous entry.

By default register() will look for the second argument to be a class constructor. You can specify other types, or be explicit about using a class, by passing an options object.

// An instance of the class will be created during resolution
container.register('DoomTemple', { class: HiddenTemple });

// Any static value (object, string, number, instance, etc.) will be resolved
container.register('SecretTemple', { value: new HiddenTemple() });

// The factory function will be called at resolution
container.register('RandomTemple', { factory: () => createTemple() });

Lifespan

For class and factory providers you can additionally specify a lifespan. This determines the scope in which a single instance or return value is used through the container and resolution process.

container.register('DoomTemple', {
	class: HiddenTemple,
	lifespan: Lifespan.Transient,
});

| Lifespan | Description | | --------------------- | -------------------------------------------------------------------------- | | Lifespan.Transient | A new instance or value is generted for each resolution | | Lifespan.Resolution | A single instance will be created for the scope an entire resolution chain | | Lifespan.Singleton | A single instance will be created for all resolutions |