all files / dist/utils/decorators/ Memoize.js

100% Statements 21/21
100% Branches 8/8
100% Functions 3/3
100% Lines 21/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  44× 42×     43× 1136× 1136× 1136× 1135× 663×   472× 472×             1135×   43×        
"use strict";
function Memoize(target, propertyName, descriptor) {
    if (descriptor.value != null)
        descriptor.value = getNewFunction(descriptor.value);
    else if (descriptor.get != null)
        descriptor.get = getNewFunction(descriptor.get);
    else
        throw new Error("Only put a Memoize decorator on a method or get accessor.");
}
exports.Memoize = Memoize;
var counter = 0;
function getNewFunction(originalFunction) {
    var identifier = ++counter;
    function decorator() {
        var propName = "__memoized_value_" + identifier;
        var returnedValue;
        if (arguments.length > 0)
            throw new Error("Should not use memoize with a function that requires arguments.");
        if (this.hasOwnProperty(propName))
            returnedValue = this[propName];
        else {
            returnedValue = originalFunction.apply(this, arguments);
            Object.defineProperty(this, propName, {
                configurable: false,
                enumerable: false,
                writable: false,
                value: returnedValue
            });
        }
        return returnedValue;
    }
    return decorator;
}
 
//# sourceMappingURL=Memoize.js.map