import { DependencyItem , DependencyKey , DependencyValue , Disposable , InitPromise , isDisposable , Ctor } from './typings' ; export class DependencyCollection implements Disposable { public disposed : boolean = false ; private readonly items = new Map < DependencyKey < any > , DependencyValue < any > | any > ( ) ; constructor ( deps = [ ] ) { for ( const dep of deps ) { if ( dep instanceof Array ) { const [ depKey , depItem ] = dep ; this . add ( depKey , depItem ) ; } else { this . add ( dep ) ; } } } add < T > ( ctor ) ; add < T > ( key , item ) ; add < T > ( ctorOrKey , item ? ) { this . ensureCollectionNotDisposed ( ) ; if ( item ) { this . items . set ( ctorOrKey , item ) ; } else { this . items . set ( ctorOrKey , new InitPromise ( ctorOrKey as Ctor < T > ) ) ; } } has ( key ) { this . ensureCollectionNotDisposed ( ) ; return this . items . has ( key ) ; } get < T > ( key ) { this . ensureCollectionNotDisposed ( ) ; return this . items . get ( key ) ; } dispose ( ) { this . disposed = true ; this . items . forEach ( ( item ) => { if ( isDisposable ( item ) ) { item . dispose ( ) ; } } ) ; } private ensureCollectionNotDisposed ( ) { if ( this . disposed ) { throw new Error ( `[wedi] Dependency collection is not accessible after it disposes!` ) ; } } }