mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 06:03:39 +08:00
23 lines
581 B
JavaScript
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;
|
||
|
}
|
||
|
};
|