1 import { isDeinitable } from './isDeinitable'
3 function isNotUndefined<T>(val: T | undefined | null): val is T {
4 return val != undefined
7 export class DependencyContainer {
8 private factory = new Map<symbol, () => unknown>()
9 private dependencies = new Map<symbol, unknown>()
14 const deps = this.getAll()
15 for (const dep of deps) {
16 if (isDeinitable(dep)) {
21 this.dependencies.clear()
24 public getAll(): unknown[] {
25 return Array.from(this.dependencies.values()).filter(isNotUndefined)
28 public bind<T>(sym: symbol, maker: () => T) {
29 this.factory.set(sym, maker)
32 public get<T>(sym: symbol): T {
33 const dep = this.dependencies.get(sym)
38 const maker = this.factory.get(sym)
40 throw new Error(`No dependency maker found for ${sym.toString()}`)
43 const instance = maker()
45 /** Could be optional */
49 this.dependencies.set(sym, instance)