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/Factory.ts

58 lines
1.6 KiB

import {AbstractFactory} from './AbstractFactory'
import {
DEPENDENCY_KEYS_METADATA_KEY,
DependencyRequirement,
Instantiable,
PropertyDependency,
} from '../types'
import {Collection} from '../../util'
import 'reflect-metadata'
/**
* Standard static-class factory. The token of this factory is a reference to a
* static class that is instantiated when the factory produces.
*
* Dependency keys are inferred from injection metadata on the constructor's params,
* as are the injected properties.
*
* @example
* ```typescript
* class A {
* constructor(
* protected readonly myService: MyService
* ) { }
* }
*
* const fact = new Factory(A)
*
* fact.produce([myServiceInstance], []) // => A { myService: myServiceInstance }
* ```
*/
export class Factory<T> extends AbstractFactory<T> {
constructor(
protected readonly token: Instantiable<T>,
) {
super(token)
}
produce(dependencies: any[], parameters: any[]): any {
return new this.token(...dependencies, ...parameters)
}
match(something: unknown): boolean {
return something === this.token // || (something?.name && something.name === this.token.name)
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.token)
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
return this.getInjectedPropertiesForPrototypeChain(this.token)
}
}