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

@homebots/injector

v1.6.4

Published

Dependency Injection library in Typescript

Downloads

16

Readme

@homebots/injector

Depedency Injection library for any project, written in Typescript

Introduction

The library exports a "global" injector, which can be used as-is to inject everything. It's called Injector.global. You can pair that with the "inject" function exported in the library.

But if you need to nest injectors and have a tree of dependencies, use TreeInjector instead.

How it works

There are two ways of using injections:

  • using a global injector: the library exports Injector.global, a singleton object that can be accessed from anywhere and used as the injector of an entire app.

  • using a tree of injector with the ability to override classes/tokens on each level. The library exports a class, TreeInjector, which can be instantiated as a starting point, and forked as needed to create children. See documentation below for more details.

First you create a class:

class ElectricCar {}

That class can have one or more dependencies. You can declare them with a decorator:

class Battery {}
class Motor {}

class ElectricCar {
  @Inject() battery: Battery;
  @Inject() motor: Motor;
}

But sometimes you don't have a concrete value just yet, or the value is not an object. Maybe you just want to have a placeholder.

You can do that using an InjectionToken:

import { Injector, InjectionToken } from '@homebots/injector';

export const CarColor = new InjectionToken<string>('color');

export class ElectricCar {
  @Inject(CarColor) color: string;

  // or

  get color() {
    return inject(CarColor);
  }
}

But who is gonna provide the CarColor? You can declare a provider for it:

import { Injector, Value } from '@homebots/injector';

Injector.global.provide(CarColor, Value('blue') });

// or

provide(CarColor, Value('blue') });

You can also replace the implementation of classes:

import { Injector, provide } from '@homebots/injector';

class Motor {}
class ElectricMotor {}

provide(Motor, ElectricMotor);

class Car {
  // 'motor' will have an instance of ElectricMotor
  @Inject() motor: Motor;
}

Examples

import { Injector, TreeInjector, Value, Inject, InjectionToken } from '@homebots/injector';

const Color = new InjectionToken('color');
class Battery {}
class Engine {}

class GasolineEngine extends Engine {}

class ElectricMotor extends Engine {
  @Inject() battery: Battery;
}

class Car {
  @Inject() engine: Engine;
  @Inject(Color) color: string;
}

// common injector for all cars
const baseInjector = new TreeInjector();
baseInjector.provide(Color, Value('black'));

// branch off to provide electric car dependencies
const electricCarInjector = baseInjector.fork();
electricCarInjector.provide(Engine, ElectricMotor);
electricCarInjector.provide(Car);

// branch off again to provide gasoline car dependencies
const gasolineCarInjector = baseInjector.fork();
gasolineCarInjector.provide(Engine, GasolineEngine);
gasolineCarInjector.provide(Car);

const electricCar = electricCarInjector.get(Car);
const gasolineCar = gasolineCarInjector.get(Car);

expect(electricCar.engine instanceof ElectricMotor).toBe(true);
expect(gasolineCar.engine instanceof GasolineEngine).toBe(true);
expect(electricCar.color).toBe('black');

API

Injector

  • get(token)

Retrieves a value for a given injectable token. The token can be either a Class or a InjectionToken. Values are cached once they are resolved, so constructors and factories will be singleton.

  • createNew(token)

Retrieves a value for a given injectable token. The token can be either a Class or a InjectionToken. Values are never cached.

  • has(token)

Check if the injector has a value stored for a given token. Returns false before the first time the token is retrieved, true otherwise.

  • canProvide(token)

Checks if a token can be provided by this injector. It differs from has() because it just checks for the possibility of an injection.

  • provide(Class)
  • provide(Class, SubstitutionClass)
  • provide(AbstractClass, Class)
  • provide(InjectionToken, Class)
  • provide(InjectionToken, factory)

Declares an injectable type.

factory is a function that returns a value.

It can be a static value, in which case the Value wrapper should be used

It can also be dynamic value. For that case, pass a function to Factory, and optionally an array of dependencies.

Example:

class A { number = 1 }
class B { number = 2 }
const C = new InjectionToken<number>();
const T = new InjectionToken<number>();

injector.provide(C, Value(3));

injector.provide(T, Factory(
  (a: A, b: B, c: number) => a.number + b.number + c,
  [A, B, C]
));

expect(injector.get(T)).toBe(6);

TreeInjector

  • fork()
  • fork(Injector)

Creates a new injector that can hold its own provided dependencies. If a provider is not found, it will look at its parent for dependencies. A parent can be provided at construction time. By default it points to the root injector.