// 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
|