/
SubtreeRetainer.ts
74 lines (66 loc) · 1.92 KB
/
SubtreeRetainer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* The `SubtreeRetainer` class keeps track of a number of pieces of data,
* comparing the values of these pieces at every iteration.
*
* This is useful for preventing redraws to relatively static (or huge)
* components whose VDOM only depends on very few values, when none of them
* have changed.
*
* @example
* // Check two callbacks for changes on each update
* this.subtree = new SubtreeRetainer(
* () => this.attrs.post.freshness,
* () => this.showing
* );
*
* // Add more callbacks to be checked for updates
* this.subtree.check(() => this.attrs.user.freshness);
*
* // In a component's onbeforeupdate() method:
* return this.subtree.needsRebuild()
*
* @see https://mithril.js.org/lifecycle-methods.html#onbeforeupdate
*/
export default class SubtreeRetainer {
protected callbacks: (() => any)[];
protected data: Record<string, any>;
/**
* @param callbacks Functions returning data to keep track of.
*/
constructor(...callbacks: (() => any)[]) {
this.callbacks = callbacks;
this.data = {};
// Build the initial data, so it is available when calling
// needsRebuild from the onbeforeupdate hook.
this.needsRebuild();
}
/**
* Return whether any data has changed since the last check.
* If so, Mithril needs to re-diff the vnode and its children.
*/
needsRebuild(): boolean {
let needsRebuild = false;
this.callbacks.forEach((callback, i) => {
const result = callback();
if (result !== this.data[i]) {
this.data[i] = result;
needsRebuild = true;
}
});
return needsRebuild;
}
/**
* Add another callback to be checked.
*/
check(...callbacks: (() => any)[]): void {
this.callbacks = this.callbacks.concat(callbacks);
// Update the data cache when new checks are added.
this.needsRebuild();
}
/**
* Invalidate the subtree, forcing it to be redrawn.
*/
invalidate(): void {
this.data = {};
}
}