You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/di/factory/SingletonFactory.ts

49 lines
1.2 KiB

import { Factory } from './Factory'
import { Collection } from '../../util'
import {DependencyKey, DependencyRequirement, PropertyDependency} from '../types'
/**
* 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
*/
export default class SingletonFactory<T> extends Factory<T> {
constructor(
/**
* Token identifying this singleton.
*/
protected token: DependencyKey,
/**
* The value of this singleton.
*/
protected value: T,
) {
super(token)
}
produce(): T {
return this.value
}
getDependencyKeys(): Collection<DependencyRequirement> {
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
return new Collection<PropertyDependency>()
}
}