framework/js/lib/utils/computed.js
2015-04-25 22:28:39 +09:30

23 lines
581 B
JavaScript

export default function computed() {
var args = [].slice.apply(arguments);
var keys = args.slice(0, -1);
var compute = args.slice(-1)[0];
var values = {};
var computed;
return function() {
var recompute = false;
keys.forEach(function(key) {
var value = typeof this[key] === 'function' ? this[key]() : this[key];
if (values[key] !== value) {
recompute = true;
values[key] = value;
}
}.bind(this));
if (recompute) {
computed = compute.apply(this, keys.map((key) => values[key]));
}
return computed;
}
};