import { Container , interfaces } from 'inversify' ; import { Abstract , IServiceManager , Newable } from './types' ; type identifier < T > = string | symbol | Newable < T > | Abstract < T > ; export class ServiceManager implements IServiceManager { constructor ( private container ) { } public add < T > ( serviceIdentifier , constructor , name ? ) { if ( name ) { this . container . bind < T > ( serviceIdentifier ) . to ( constructor ) . whenTargetNamed ( name ) ; } else { this . container . bind < T > ( serviceIdentifier ) . to ( constructor ) ; } } public addFactory < T > ( factoryIdentifier , factoryMethod ) { this . container . bind < interfaces . Factory < T > > ( factoryIdentifier ) . toFactory < T > ( factoryMethod ) ; } public addSingleton < T > ( serviceIdentifier , constructor , name ? ) { if ( name ) { this . container . bind < T > ( serviceIdentifier ) . to ( constructor ) . inSingletonScope ( ) . whenTargetNamed ( name ) ; } else { this . container . bind < T > ( serviceIdentifier ) . to ( constructor ) . inSingletonScope ( ) ; } } public addSingletonInstance < T > ( serviceIdentifier , instance , name ? ) { if ( name ) { this . container . bind < T > ( serviceIdentifier ) . toConstantValue ( instance ) . whenTargetNamed ( name ) ; } else { this . container . bind < T > ( serviceIdentifier ) . toConstantValue ( instance ) ; } } public get < T > ( serviceIdentifier , name ? ) { return name ? this . container . getNamed < T > ( serviceIdentifier , name ) : this . container . get < T > ( serviceIdentifier ) ; } public getAll < T > ( serviceIdentifier , name ? ) { return name ? this . container . getAllNamed < T > ( serviceIdentifier , name ) : this . container . getAll < T > ( serviceIdentifier ) ; } }