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

1.0.3 • Public • Published

Usage

TypeScript example:

type XObject = {x: number};
type ResultObject = {result: string};

class Foo
{
    // used as a decorator
    @memoize
    bar(x: XObject, y: string, z?:number):ResultObject
    {
        return {
            result: JSON.stringify({x, y, z})
        };
    }

    // used inline
    baz = memoize({
        getAdditionalArgs: () => [this.y],
        function: (x:XObject):ResultObject => this.bar(x, this.y)
    });

    y = 'y';
}

Test:

function assert(shouldBeTrue:boolean, msg:string)
{
    if (!shouldBeTrue)
        throw new Error('fail: ' + msg);
}

let foo = new Foo();
let foo2 = new Foo();
let x = {x: 123};
let bar = foo.bar(x, 'y');
let baz = foo.baz(x);

assert(bar === foo.bar(x, 'y'), 'memoized bar');
assert(bar !== foo2.bar(x, 'y'), 'memoized per instance');
assert(bar === baz, "foo.y === 'y' when baz was created");
assert(baz === foo.baz(x), 'memoized baz');
foo.y = 'yy';
assert(baz !== foo.baz(x), 'additional args for baz changed');
assert(foo.bar(x, 'y', 3) === foo.bar(x, 'y', 3), 'supports variable number of arguments');

Readme

Keywords

Package Sidebar

Install

npm i memoize-weak-decorator

Weekly Downloads

79

Version

1.0.3

License

MIT

Last publish

Collaborators

  • adufilie