framework/js/lib/utils/computed.js

23 lines
581 B
JavaScript
Raw Normal View History

2015-04-25 20:58:39 +08:00
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;
}
};