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.
66 lines
2.0 KiB
66 lines
2.0 KiB
3 years ago
|
import {AbstractFactory} from "./AbstractFactory";
|
||
|
import {
|
||
|
DEPENDENCY_KEYS_METADATA_KEY,
|
||
|
DEPENDENCY_KEYS_PROPERTY_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 extends AbstractFactory {
|
||
|
constructor(
|
||
|
protected readonly token: Instantiable<any>
|
||
|
) {
|
||
|
super(token)
|
||
|
}
|
||
|
|
||
|
produce(dependencies: any[], parameters: any[]): any {
|
||
|
return new this.token(...dependencies, ...parameters)
|
||
|
}
|
||
|
|
||
|
match(something: any) {
|
||
|
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> {
|
||
|
const meta = new Collection<PropertyDependency>()
|
||
|
let currentToken = this.token
|
||
|
|
||
|
do {
|
||
|
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||
|
if ( loadedMeta ) meta.concat(loadedMeta)
|
||
|
currentToken = Object.getPrototypeOf(currentToken)
|
||
|
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
|
||
|
|
||
|
return meta
|
||
|
}
|
||
|
}
|