framework/js/lib/utils/subtree-retainer.js
2015-05-04 10:43:40 +09:30

38 lines
782 B
JavaScript

/**
// constructor
this.subtree = new SubtreeRetainer(
() => this.props.post.freshness,
() => this.showing
);
this.subtree.add(() => this.props.user.freshness);
// view
this.subtree.retain() || 'expensive expression'
*/
export default class SubtreeRetainer {
constructor() {
this.clear();
this.callbacks = [].slice.call(arguments);
}
retain() {
var needsRebuild = false;
this.callbacks.forEach((callback, i) => {
var result = callback();
if (result !== this.old[i]) {
this.old[i] = result;
needsRebuild = true;
}
});
return needsRebuild ? false : {subtree: 'retain'};
}
add() {
this.callbacks = this.callbacks.concat([].slice.call(arguments));
}
clear() {
this.old = [];
}
}