|
|
|
@ -3,6 +3,8 @@ import NamedFactory from './factory/NamedFactory'
|
|
|
|
|
import {AbstractFactory} from './factory/AbstractFactory'
|
|
|
|
|
import {Factory} from './factory/Factory'
|
|
|
|
|
import {ClosureFactory} from './factory/ClosureFactory'
|
|
|
|
|
import {Collection, collect} from '../util/collection/Collection'
|
|
|
|
|
import {Subscription, Unsubscribe} from '../util/support/BehaviorSubject'
|
|
|
|
|
|
|
|
|
|
/** Simple type alias for a callback to a container's onResolve method. */
|
|
|
|
|
export type ContainerResolutionCallback<T> = (() => unknown) | ((t: T) => unknown)
|
|
|
|
@ -25,11 +27,11 @@ export class ContainerBlueprint {
|
|
|
|
|
return this.instance
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected factories: (() => AbstractFactory<any>)[] = []
|
|
|
|
|
protected factories: Collection<(() => AbstractFactory<any>)> = collect()
|
|
|
|
|
|
|
|
|
|
protected constructableFactories: StaticClass<AbstractFactory<any>, any>[] = []
|
|
|
|
|
protected constructableFactories: Collection<StaticClass<AbstractFactory<any>, any>> = collect()
|
|
|
|
|
|
|
|
|
|
protected resolutionCallbacks: ({key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>})[] = []
|
|
|
|
|
protected resolutionCallbacks: Collection<{key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>}> = collect()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register some factory class with the container. Should take no construction params.
|
|
|
|
@ -74,7 +76,16 @@ export class ContainerBlueprint {
|
|
|
|
|
* Get an array of factory instances in the blueprint.
|
|
|
|
|
*/
|
|
|
|
|
resolve(): AbstractFactory<any>[] {
|
|
|
|
|
return this.factories.map(x => x())
|
|
|
|
|
return this.factories.map(x => x()).all()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subscribe to new factories being registered.
|
|
|
|
|
* Used by `Container` implementations to listen for factories being registered after the container is realized.
|
|
|
|
|
* @param sub
|
|
|
|
|
*/
|
|
|
|
|
resolve$(sub: Subscription<() => AbstractFactory<any>>): Unsubscribe {
|
|
|
|
|
return this.factories.push$(sub)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -94,14 +105,32 @@ export class ContainerBlueprint {
|
|
|
|
|
* Get an array of static Factory classes that need to be instantiated by
|
|
|
|
|
* the container itself.
|
|
|
|
|
*/
|
|
|
|
|
resolveConstructable(): StaticClass<AbstractFactory<any>, any> {
|
|
|
|
|
return [...this.constructableFactories]
|
|
|
|
|
resolveConstructable(): StaticClass<AbstractFactory<any>, any>[] {
|
|
|
|
|
return this.constructableFactories.all()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subscribe to new constructable factories being registered.
|
|
|
|
|
* Used by `Container` implementations to listen for factories registered after the container is realized.
|
|
|
|
|
* @param sub
|
|
|
|
|
*/
|
|
|
|
|
resolveConstructable$(sub: Subscription<StaticClass<AbstractFactory<any>, any>>): Unsubscribe {
|
|
|
|
|
return this.constructableFactories.push$(sub)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an array of DependencyKey-callback pairs to register with new containers.
|
|
|
|
|
*/
|
|
|
|
|
resolveResolutionCallbacks(): ({key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>})[] {
|
|
|
|
|
return [...this.resolutionCallbacks]
|
|
|
|
|
return this.resolutionCallbacks.all()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subscribe to new resolution callbacks being registered.
|
|
|
|
|
* Used by `Container` implementations to listen for callbacks registered after the container is realized.
|
|
|
|
|
* @param sub
|
|
|
|
|
*/
|
|
|
|
|
resolveResolutionCallbacks$(sub: Subscription<{key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>}>): Unsubscribe {
|
|
|
|
|
return this.resolutionCallbacks.push$(sub)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|