memorize-decorator

0.3.1 • Public • Published

NPM version Repository package.json version MIT License Discord

Memorize Decorator

A simple decorator that memorizes results of 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

For CommonJS and experimental decorator, use version "0.2".

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.

Readme

Keywords

none

Package Sidebar

Install

npm i memorize-decorator

Weekly Downloads

1,568

Version

0.3.1

License

MIT

Unpacked Size

11.3 kB

Total Files

11

Last publish

Collaborators

  • vilicvane