Nachos Preventing Motivation

    memorize-decorator
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.4 • Public • Published

    NPM Package Build Status

    Memorize Decorator

    A simple decorator that memorizes methods and getters. It can also wrap normal functions via the old-fashioned way.

    Note: It takes this and arguments (for methods and functions) as keys for the memorized results.

    Install

    yarn add memorize-decorator
    # or 
    npm install memorize-decorator --save

    API References

    export declare function memorize<T extends Function>(fn: T): T;
    export declare function memorize(): MethodDecorator;
     
    export default memorize;

    Usage

    import deprecated from 'memorize-decorator';
     
    class Foo {
      @memorize({
        // Delete cache after 100 milliseconds.
        ttl: 100,
      })
      method() {
        return 'abc';
      }
     
      @memorize({
        // Keep cache until returned Promise gets fulfilled.
        ttl: 'async',
      })
      async asyncMethod() {
        return 'abc';
      }
     
      @memorize({
        // Use `asap` package to schedule cache deletion.
        ttl: false,
      })
      get property() {
        return 123;
      }
     
      @memorize()
      static method() {
        return 'abc';
      }
     
      @memorize()
      static get property() {
        return 123;
      }
    }

    For functions:

    import memorize from 'memorize-decorator';
     
    let foo = memorize(function foo() {
      // ...
    });

    License

    MIT License.

    Install

    npm i memorize-decorator

    DownloadsWeekly Downloads

    220

    Version

    0.2.4

    License

    MIT

    Unpacked Size

    10.7 kB

    Total Files

    6

    Last publish

    Collaborators

    • vilic