Code coverage report for core/src/decorators/lookup.ts

Statements: 100% (22 / 22)      Branches: 59.09% (13 / 22)      Functions: 100% (3 / 3)      Lines: 100% (22 / 22)      Ignored: none     

All files » core/src/decorators/ » lookup.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391 17 17     17       23 23 23 23   23 23 23 23   23         23 23 23 23   23 23 23 23   23        
export class Lookup<Factory, Instance> {
  protected readonly factories = new WeakMap<any, Record<string, Factory | null>>();
  protected readonly instances = new WeakMap<any, Record<string, Instance | null>>();
 
 
  constructor(private readonly lookupFactory?: () => Factory | undefined) {}
 
 
  getInstance(thisArg: any, name: string, create: () => Instance | undefined): Instance | undefined {
    let map = this.instances.get(thisArg);
    Eif (!map) {
      map = {};
      this.instances.set(this, map);
    }
    let instance = map ? map[name] : undefined;
    Eif (instance === undefined) {
      instance = create();
      map[name] = instance || null;
    }
    return instance || undefined;
  }
 
 
  getFactory(thisArg: any, name: string, prefer?: Factory): Factory | undefined {
    let map = this.factories.get(thisArg);
    Eif (!map) {
      map = {};
      this.factories.set(this, map);
    }
    let factory: Factory | null | undefined = prefer || map[name];
    Eif (factory === undefined && this.lookupFactory) {
      factory = this.lookupFactory();
      map[name] = factory || null;
    }
    return factory || undefined;
  }
 
}