2021-06-02 01:59:40 +00:00
|
|
|
import { Factory } from './Factory'
|
|
|
|
import { Collection } from '../../util'
|
2021-06-03 03:36:25 +00:00
|
|
|
import {DependencyKey, DependencyRequirement, PropertyDependency} from '../types'
|
2021-06-02 01:59:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container factory which returns its token as its value, without attempting
|
|
|
|
* to instantiate anything. This is used to register already-produced-singletons
|
|
|
|
* with the container.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```typescript
|
|
|
|
* class A {}
|
|
|
|
* const exactlyThisInstanceOfA = new A()
|
|
|
|
*
|
|
|
|
* const fact = new SingletonFactory(A, a)
|
|
|
|
*
|
|
|
|
* fact.produce([], []) // => exactlyThisInstanceOfA
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @extends Factory
|
|
|
|
*/
|
2021-06-03 03:36:25 +00:00
|
|
|
export default class SingletonFactory<T> extends Factory<T> {
|
2021-06-02 01:59:40 +00:00
|
|
|
constructor(
|
|
|
|
/**
|
2021-06-03 03:36:25 +00:00
|
|
|
* Token identifying this singleton.
|
2021-06-02 01:59:40 +00:00
|
|
|
*/
|
2021-06-03 03:36:25 +00:00
|
|
|
protected token: DependencyKey,
|
2021-06-02 01:59:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-06-03 03:36:25 +00:00
|
|
|
* The value of this singleton.
|
2021-06-02 01:59:40 +00:00
|
|
|
*/
|
2021-06-03 03:36:25 +00:00
|
|
|
protected value: T,
|
2021-06-02 01:59:40 +00:00
|
|
|
) {
|
|
|
|
super(token)
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
produce(): T {
|
|
|
|
return this.value
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getDependencyKeys(): Collection<DependencyRequirement> {
|
|
|
|
return new Collection<DependencyRequirement>()
|
|
|
|
}
|
|
|
|
|
|
|
|
getInjectedProperties(): Collection<PropertyDependency> {
|
|
|
|
return new Collection<PropertyDependency>()
|
|
|
|
}
|
|
|
|
}
|