@lit-kit/di
TypeScript icon, indicating that this package has built-in type declarations

2.1.3 • Public • Published

Di

Dependency Injection in ~800 bytes

Installation:

npm i @lit-kit/di

Example:

import { Injector, Inject } from '@lit-kit/di';

class FooService {
  sayHello() {
    return 'Hello From FooService';
  }
}

class BarService {
  constructor(@Inject(FooService) private foo: FooService) {}

  sayHello() {
    return 'Hello From BarService and ' + this.foo.sayHello();
  }
}

const app = new Injector();

app.get(BarService).sayHello(); // Hello from BarService and Hello from FooService

Override A Service:

import { Injector, Inject } from '@lit-kit/di';

class FooService {
  sayHello() {
    return 'Hello From FooService';
  }
}

class BarService {
  constructor(@Inject(FooService) private foo: FooService) {}

  sayHello() {
    return 'Hello From BarService and ' + this.foo.sayHello();
  }
}

// Override FooService with an alternate implementation
const app = new Injector({
  providers: [
    {
      provide: FooService,
      useClass: class extends FooService {
        sayHello() {
          return 'IT HAS BEEN OVERRIDEN'
        }
      }
    }
  ]
});

app.get(BarService).sayHello(); // Hello from BarService and IT HAS BEEN OVERRIDEN

Root Service

If you have nested injectors and you still want singleton instances decorator your services with @Service()

import { Service } from '@lit-kit/di';

@Service()
class FooService {
  sayHello() {
    return 'Hello From FooService';
  }
}

Inject services with custom decorators:

import { Injector, Inject } from '@lit-kit/di';

function FooRef(c: any, k: string, i: number) {
  Inject(FooService)(c, k, i)
}

class FooService {
  sayHello() {
    return 'Hello From FooService';
  }
}

class BarService {
  constructor(@FooRef private foo: FooService) {}

  sayHello() {
    return 'Hello From BarService and ' + this.foo.sayHello();
  }
}

// create a new instance of our injector
const app = new Injector();

app.get(BarService).sayHello(); // Hello from BarService and Hello from FooService

Package Sidebar

Install

npm i @lit-kit/di

Weekly Downloads

1

Version

2.1.3

License

MIT

Unpacked Size

73.9 kB

Total Files

63

Last publish

Collaborators

  • deebloo