Initial commit

This commit is contained in:
garrettmills
2020-06-15 20:35:30 -05:00
commit 6c4696227b
19 changed files with 3225 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
import {Collection} from '../../../lib/src/collection/Collection.ts'
import {DependencyRequirement} from '../type/DependencyRequirement.ts'
export default abstract class AbstractFactory {
protected constructor(
protected token: any
) {}
abstract produce(dependencies: any[], parameters: any[]): any
abstract match(something: any): boolean
abstract get_dependency_keys(): Collection<DependencyRequirement>
}

28
di/src/factory/Factory.ts Executable file
View File

@@ -0,0 +1,28 @@
import Instantiable from '../type/Instantiable.ts'
import { DEPENDENCY_KEYS_METADATA_KEY } from '../type/DependencyKey.ts'
import { Reflect } from '../../../lib/src/external/reflect.ts'
import { Collection } from '../../../lib/src/collection/Collection.ts'
import { DependencyRequirement } from '../type/DependencyRequirement.ts'
import AbstractFactory from './AbstractFactory.ts'
export default class Factory extends AbstractFactory {
constructor(
protected token: Instantiable<any>
) {
super(token)
}
produce(dependencies: any[], parameters: any[]) {
return new this.token(...dependencies, ...parameters)
}
match(something: any) {
return something === this.token || (something.toString && String(something) === this.token.name)
}
get_dependency_keys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.token)
if ( meta ) return meta
return new Collection<DependencyRequirement>()
}
}

View File

@@ -0,0 +1,24 @@
import AbstractFactory from './AbstractFactory.ts'
import {DependencyRequirement} from '../type/DependencyRequirement.ts'
import {Collection} from '../../../lib/src/collection/Collection.ts'
export default class FunctionFactory extends AbstractFactory {
constructor(
protected name: string,
protected token: () => any,
) {
super(token)
}
get_dependency_keys(): Collection<DependencyRequirement> {
return new Collection<DependencyRequirement>()
}
match(something: any) {
return something === this.name
}
produce(dependencies: any[], parameters: any[]): any {
return this.token()
}
}

View File

@@ -0,0 +1,15 @@
import Factory from './Factory.ts'
import Instantiable from "../type/Instantiable.ts";
export default class NamedFactory extends Factory {
constructor(
protected name: string,
protected token: Instantiable<any>,
) {
super(token)
}
match(something: any) {
return something === this.name
}
}

View File

@@ -0,0 +1,24 @@
import Factory from './Factory.ts'
import {Collection} from '../../../lib/src/collection/Collection.ts'
import {DependencyRequirement} from '../type/DependencyRequirement.ts'
export default class SingletonFactory extends Factory {
constructor(
protected token: FunctionConstructor,
protected key: string,
) {
super(token)
}
produce(dependencies: any[], parameters: any[]) {
return this.token
}
match(something: any) {
return something === this.key
}
get_dependency_keys(): Collection<DependencyRequirement> {
return new Collection<DependencyRequirement>()
}
}