JSPM

lazy-get-decorator

2.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 23442
  • Score
    100M100P100Q144872F
  • License MIT

Lazily evaluates a getter on an object and caches the returned value

Package Exports

  • lazy-get-decorator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lazy-get-decorator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Lazy Get decorator

Build Status Coverage Status Greenkeeper badge

NPM

Previously known as typescript-lazy-get-decorator.

Compatibility

  • Typescript - full
  • Spec-compliant decorator proposal - full
  • Babel (current proposal) - full
  • Babel (legacy) - full

API

/**
 * Evaluate the getter function and cache the result
 * @param {boolean} [setProto=false] Set the value on the class prototype as well. Only applies to non-static getters.
 * @param {boolean} [makeNonConfigurable=false] Set to true to make the resolved property non-configurable
 * @param {ResultSelectorFn} [resultSelector] A filter function that must return true for the value to cached
 * @return A Typescript decorator function
 */
function LazyGetter(setProto?: boolean, makeNonConfigurable?: boolean, resultSelector?: (value: any) => boolean): MethodDecorator;

Usage

import {LazyGetter} from 'lazy-get-decorator';

class AClass {

    @LazyGetter()
    get lazyNoProto(): string {
        console.log('Evaluating lazyNoProto');

        return 'lazyNoProtoValue';
    }

    @LazyGetter(true)
    get lazyWithProto(): string {
        console.log('Evaluating lazyWithProto');

        return 'lazyWithProtoValue';
    }
}

const inst1 = new AClass();

console.log('==== inst 1 ====\n');

console.log(inst1.lazyNoProto);
console.log(inst1.lazyNoProto);
console.log(inst1.lazyWithProto);
console.log(inst1.lazyWithProto);

const inst2 = new AClass();

console.log('\n\n==== inst 2 ====\n');

console.log(inst2.lazyNoProto);
console.log(inst2.lazyNoProto);
console.log(inst2.lazyWithProto);
console.log(inst2.lazyWithProto);

Output:

==== inst 1 ====

Evaluating lazyNoProto
lazyNoProtoValue
lazyNoProtoValue
Evaluating lazyWithProto
lazyWithProtoValue
lazyWithProtoValue


==== inst 2 ====

Evaluating lazyNoProto
lazyNoProtoValue
lazyNoProtoValue
lazyWithProtoValue
lazyWithProtoValue

Using the result selector

import {LazyGetter} from 'lazy-get-decorator';

class MyClass {
  public readonly someCondition = 10;
  
  @LazyGetter(false, false, (v: number) => v === 10)
  public get prop1(): number {
    // This will get cached
    return this.someCondition;
  }
  
  @LazyGetter(false, false, (v: number) => v === 1)
  public get prop2(): number {
    // This won't get cached
    return this.someCondition;
  }
}