blob: aca3e1c2b01666191c36fe6fd936a66ec5a84905 [file] [log] [blame]
class LazilyEvaluatedFunction {
constructor(callback, ...observedPropertiesList)
{
console.assert(typeof(callback) == 'function');
this._callback = callback;
this._observedPropertiesList = observedPropertiesList;
this._cachedArguments = null;
this._cachedResult = undefined;
}
evaluate(...args)
{
if (this._cachedArguments) {
const length = this._cachedArguments.length;
if (args.length == length && (!length || this._cachedArguments.every((cached, i) => cached === args[i])))
return this._cachedResult;
}
this._cachedArguments = args;
this._cachedResult = this._callback.apply(null, args);
return this._cachedResult;
}
}
if (typeof module != 'undefined')
module.exports.LazilyEvaluatedFunction = LazilyEvaluatedFunction;