memoize-weak-decorator
Options
All
  • Public
  • Public/Protected
  • All
Menu

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');

Index

External modules

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc