import { AsyncFactory, BindAs, RegisterDescriptor, SyncFactory } from './binding'; import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector'; import { Provider } from './provider'; import { State } from './state'; /** * Injector (aka Container) to handle (a)synchronous dependency management. */ export declare class Container implements Injector { protected parent?: Injector | undefined; /** * Create a new Container, with an optional parent Injector which will be searched if any given InjectableId is not bound within this Container. */ constructor(parent?: Injector | undefined); protected providers: Map, Provider>; /** * @inheritDoc */ isIdKnown(id: InjectableId, ascending?: boolean): boolean; /** * @inheritDoc */ get(id: InjectableId): T; /** * @inheritDoc */ resolve(id: InjectableId): Promise; /** * Removes a binding from this Container (and optionally its ancestor chain). * Most useful in unit tests to replace or clear bindings between test cases. * **Caution:** Removing a binding after initialization may have unexpected consequences if other parts of the application still hold or expect to obtain an instance of the removed id. * * @param id The id of the binding to remove. * @param ascending If true, the binding is also removed from every Container in the parent chain. * @param releaseIfSingleton If true, Provider.releaseIfSingleton is called before removal. */ removeBinding(id: InjectableId, ascending?: boolean, releaseIfSingleton?: boolean): void; /** * Alias for {@link isIdKnown}. Familiar to users migrating from TypeDI. */ has(id: InjectableId, ascending?: boolean): boolean; /** * Alias for {@link removeBinding}. Familiar to users migrating from InversifyJS. */ unbind(id: InjectableId, ascending?: boolean, releaseIfSingleton?: boolean): void; /** * Creates a new child Container that inherits unbound ids from this Container. * Familiar to users migrating from TSyringe. */ createChildContainer(): Container; /** * Descriptor-based binding dispatching to the appropriate bindXXX method. * Familiar to users migrating from TSyringe. * Returns a {@link BindAs} chain for class and factory bindings; returns undefined for value bindings (which are always singletons). */ register(id: InjectableId, descriptor: RegisterDescriptor): BindAs | undefined; /** * Binds a class as a singleton in one step. * Shorthand for {@link bindClass}(...).{@link BindAs.asSingleton asSingleton}(). * Familiar to users migrating from TSyringe. */ registerSingleton(id: InjectableId, constructor: ClassConstructor): void; /** * @inheritDoc */ bindConstant(id: InjectableId, value: T): T; /** * @inheritDoc */ bindClass(id: ClassConstructor, constructor?: ClassConstructor): BindAs>; bindClass(id: string | symbol | AbstractConstructor | InjectableId, constructor: ClassConstructor): BindAs>; /** * @inheritDoc */ bindFactory(id: InjectableId, factory: SyncFactory): BindAs>; /** * @inheritDoc */ bindAsyncFactory(id: InjectableId, factory: AsyncFactory): BindAs>; /** * @inheritDoc */ resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise; /** * As implied by the name prefix, this is a factored out method invoked only by the 'resolve' method. * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container. */ protected resolveState(id: InjectableId): State; /** * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated. * It will walk through all registered Providers (of this Container only), and invoke their Provider.releaseIfSingleton method. * This method is not part of the Injector interface, because you normally only create (and release) from Containers. * NOTE: * This *only* releases active/pending Singleton's that have already been created by this Container. * The most likely use of this method would be when you have created a new child Container for a limited-duration transaction, and you want to easily clean up temporary resources. * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction). * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier. */ releaseSingletons(): void; /** * Releases a Singleton instance if it exists. * However, it does **not** remove the binding, so if you call get or resolve (directly or indirectly) on the id, a new Singleton will be created. * If not a singleton, this method returns undefined. * If the singleton has been resolved, it is returned, otherwise null is returned. * If the singleton is pending resolution, a Promise for the singleton (or for null) is returned. * Note that if a singleton is returned, its Release method will already have been invoked. */ releaseSingleton(id: InjectableId): T | undefined | null | Promise; /** * Make a copy of this Container. * This is an experimental feature! * I have not thought through all the dark corners, so use at your own peril! * Here are some notes: * The injector parameter for SyncFactory and AsyncFactory callbacks will be the Container invoking the factory. * So a factory that uses a parent closure instead of the supplied injector may get unexpected results. * The injector parameter for OnSuccess and OnError callbacks will be the Container performing the resolution. * Singletons are cloned at their *existing* state.. * If resolved in "this" container, they will not be re-resolved for the clone. * If released by the clone, they will be considered released by "this" container. * If a singleton is currently being asynchronously constructed any callbacks will reference "this" Container, however both Containers should have no problem awaiting resolution. * If a singleton is not resolved when the container is cloned, then if both containers resolve, you will create *two* "singletons". * The way to avoid this last effect is to call resolveSingletons first */ clone(clazz?: ClassConstructor): Container; }