All files / fuse-ui-shared/decorators lazy.ts

100% Statements 13/13
50% Branches 1/2
100% Functions 3/3
100% Lines 11/11
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                  1x 1x 1x 1x 1x 1x   1x   1x       1x   1x     1x      
/**
 * lazy decorator
 *
 * @lazy()
 * get prop() : T {
 *   // code to create the return type and then once
 *   // created it is cached for future invocations
 * }
 */
export function lazy() {
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    let { configurable, enumerable, writable } = descriptor;
    let innerGet = descriptor.get;
    Eif (typeof innerGet === 'function') {
      let getter = function () {
        // tslint:disable-next-line:no-invalid-this
        let val = innerGet.apply(this);
        // tslint:disable-next-line:no-invalid-this
        Object.defineProperty(this, propertyKey, {
          configurable, enumerable, writable, value: val
        });
 
        return val;
      };
      descriptor.get = getter;
    }
 
    return descriptor;
  };
}