inversify#LazyServiceIdentifer TypeScript Examples
The following examples show how to use
inversify#LazyServiceIdentifer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: inject.ts From reactant with MIT License | 5 votes |
/**
* ## Description
*
* You can use `@inject()` to perform the required dependency injection module to decorate in the constructor of an injectable class.
*
* If the default is a dependency injection of the class itself as a type, e.g. `@inject(Foo) foo: Foo`, then it is exactly the same as `foo: Foo`.
*
* ## Example
*
* ```ts
* @injectable()
* class Bar {
* getValue() {
* return 'bar';
* }
* }
*
* @injectable()
* class Foo {
* getValue() {
* return 'foo';
* }
* }
*
* @injectable()
* class FooBar {
* constructor(@inject() public bar: Bar, @inject('foo') public foo: Foo) {}
* }
*
* const fooBar = testBed({
* modules: [
* Bar,
* { provide: 'foo', useClass: Foo },
* ],
* main: FooBar,
* });
*
* expect(fooBar.instance.foo.getValue()).toBe('foo');
* ```
*/
export function inject(serviceIdentifierOrFunc?: ServiceIdentifierOrFunc<any>) {
return (target: object, key?: string, index?: number) => {
const self = Reflect.getMetadata(METADATA_KEY.paramtypes, target)[index!];
let serviceIdentifier: ServiceIdentifierOrFunc<any>;
if (serviceIdentifierOrFunc instanceof LazyServiceIdentifer) {
serviceIdentifier = serviceIdentifierOrFunc;
} else if (typeof serviceIdentifierOrFunc === 'undefined') {
serviceIdentifier = forwardRef(() =>
lookupServiceIdentifier(target, self, index)
);
} else {
serviceIdentifier = forwardRef(() =>
lookupServiceIdentifier(target, serviceIdentifierOrFunc, index)
);
}
decorate(
injectWithInversify(serviceIdentifier) as ClassDecorator,
target,
index
);
};
}
Example #2
Source File: forwardRef.ts From reactant with MIT License | 5 votes |
forwardRef = (callback: () => ServiceIdentifier<any>) =>
new LazyServiceIdentifer(callback)