All Projects → Alorel → typescript-lazy-get-decorator

Alorel / typescript-lazy-get-decorator

Licence: MIT license
Lazily evaluates a getter on an object and caches the returned value

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to typescript-lazy-get-decorator

invokable
Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs).
Stars: ✭ 40 (+21.21%)
Mutual labels:  memoize
lazy-load-images.js
Progressive & lazy loading images.
Stars: ✭ 17 (-48.48%)
Mutual labels:  lazy
getx-snippets-intelliJ
An extension to accelerate the process of developing applications with flutter, aimed at everyone using the GetX package.
Stars: ✭ 52 (+57.58%)
Mutual labels:  get
lazy-kit
A new design system for developing with less effort. See how it looks:
Stars: ✭ 68 (+106.06%)
Mutual labels:  lazy
laravelmanthra
Laravel Crud Generator, I have working for years and I can tell you... It's all CRUD 💩💩💩
Stars: ✭ 27 (-18.18%)
Mutual labels:  lazy
react-component-transition
Easy animations between react component transitions.
Stars: ✭ 20 (-39.39%)
Mutual labels:  lazy
go-memoize
An easy, no-frills memoizer for Go. Cache your expensive function calls.
Stars: ✭ 63 (+90.91%)
Mutual labels:  memoize
react-reflorp
Basically a simple ORM using Refetch, but the data is stored in Redux. Feel free to open an issue if you have a question or feature request.
Stars: ✭ 45 (+36.36%)
Mutual labels:  decorator
presenter
Easy class decoration.
Stars: ✭ 13 (-60.61%)
Mutual labels:  decorator
singleton decorator
A testable singleton decorator
Stars: ✭ 39 (+18.18%)
Mutual labels:  decorator
oh-my-design-patterns
🎨 Record the articles and code I wrote while learning design patterns
Stars: ✭ 33 (+0%)
Mutual labels:  decorator
session-resume
Annotate fields to be persisted on navigation away from the current page
Stars: ✭ 105 (+218.18%)
Mutual labels:  decorator
hent
A small utility to fetch remote files into buffers
Stars: ✭ 23 (-30.3%)
Mutual labels:  get
Memoize One
A memoization library which only remembers the latest invocation
Stars: ✭ 2,649 (+7927.27%)
Mutual labels:  memoize
relaze
Tiny image lazy loading library for React.
Stars: ✭ 20 (-39.39%)
Mutual labels:  lazy
typescript-memoize
A memoize decorator for Typescript
Stars: ✭ 85 (+157.58%)
Mutual labels:  memoize
nvim-config
My neovim config
Stars: ✭ 63 (+90.91%)
Mutual labels:  lazy
min
A decorator web framework for deno
Stars: ✭ 21 (-36.36%)
Mutual labels:  decorator
check-all
Multiple checkbox selection helper.
Stars: ✭ 70 (+112.12%)
Mutual labels:  decorator
regex-not
Create a javascript regular expression for matching everything except for the given string.
Stars: ✭ 31 (-6.06%)
Mutual labels:  memoize

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;
  }
}

Resetting LazyGetter

The cached value can be reset if the decorator does not modify the class prototype, i.e. is not called as @LazyGetter(true):

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

const instanceDec = LazyGetter();
const staticDec = LazyGetter();

class MyClass {
  public instanceCount = 0;
  public static staticCount = 0;
  
  @instanceDec
  public get count(): number {
    return this.instanceCount++;
  }
  
  @staticDec
  public static get count(): number {
    return MyClass.staticCount++;
  }
}

const inst = new MyClass();

console.log(inst.count); // 0
console.log(inst.count); // 0
instanceDec.reset(inst);
console.log(inst.count); // 1

console.log(MyClass.count); // 0
console.log(MyClass.count); // 0
staticDec.reset(MyClass);
console.log(MyClass.count); // 1

Resetting the decoration performs the following steps:

  1. Resets the property descriptor to its state before the decoration
  2. Re-applies the decorator

This means that any descriptor changes made by other decorators may be lost, therefore you should ensure LazyGetter is applied last if you intend on resetting it, i.e. place it at the very top of your decorators list:

class MyClass {

  @LazyGetter()
  @decorator2
  @decorator1
  get getter() {
    return 1;
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].