mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 09:41:49 +08:00
39 lines
815 B
JavaScript
39 lines
815 B
JavaScript
/**
|
|
// constructor
|
|
this.subtree = new SubtreeRetainer(
|
|
() => this.props.post.freshness,
|
|
() => this.showing
|
|
);
|
|
this.subtree.check(() => this.props.user.freshness);
|
|
|
|
// view
|
|
this.subtree.retain() || 'expensive expression'
|
|
*/
|
|
export default class SubtreeRetainer {
|
|
constructor() {
|
|
this.invalidate();
|
|
this.callbacks = [].slice.call(arguments);
|
|
this.old = {};
|
|
}
|
|
|
|
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'};
|
|
}
|
|
|
|
check() {
|
|
this.callbacks = this.callbacks.concat([].slice.call(arguments));
|
|
}
|
|
|
|
invalidate() {
|
|
this.old = {};
|
|
}
|
|
}
|