all files / dist/utils/ memoize.js

80.95% Statements 17/21
50% Branches 3/6
100% Functions 3/3
80.95% Lines 17/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                  29× 29×                 29×   29× 1150× 1150×     1150× 1150× 1150× 434×     716× 716×             1150×          
// tests already exist online. No needt o repeat them
"use strict";
/**
 * Caches the return value of get accessors and methods.
 *
 * Notes:
 * - Doesn't really make sense to put this on a method with parameters
 * - Creates an obscure non-enumerable property on the instance to store the memoized value
 */
function Memoize(target, propertyName, descriptor) {
    Eif (descriptor.value != null) {
        descriptor.value = getNewFunction(descriptor.value);
    }
    else if (descriptor.get != null) {
        descriptor.get = getNewFunction(descriptor.get);
    }
    else {
        throw "Only put a Memoize decorator on a method or get accessor.";
    }
}
exports.Memoize = Memoize;
var counter = 0;
function getNewFunction(originalFunction) {
    var identifier = ++counter;
    // tslint:disable-next-line
    return function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        var propName = "__memoized_value_" + identifier;
        var returnedValue;
        if (this.hasOwnProperty(propName)) {
            returnedValue = this[propName];
        }
        else {
            returnedValue = originalFunction.apply(this, args);
            Object.defineProperty(this, propName, {
                configurable: false,
                enumerable: false,
                writable: false,
                value: returnedValue
            });
        }
        return returnedValue;
    };
}
 
//# sourceMappingURL=memoize.js.map