-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathtracker.js
65 lines (58 loc) · 1.27 KB
/
tracker.js
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
/**
* Keep list of all components to be updated on layout change.
* Update components using forceUpdate() method.
*/
class Tracker {
/**
* Constructor
*/
constructor() {
this._components = new Map();
this._isUpdating = false;
}
/**
* Start tracking updates to component
* @param {ReactComponent} component
*/
add(component) {
this._components.set(component, true);
}
/**
* Stop tracking updates to component
* @param {ReactComponent} component
*/
remove(component) {
this._components.delete(component);
}
/**
* Update all tracked components
*/
updateComponents() {
this._isUpdating = true;
this._components.forEach((value, component) => this._components.set(component, false));
this._components.forEach((value, component) => {
if (value === false) {
component.forceUpdate();
this.setUpdated(component);
}
});
this._isUpdating = false;
}
/**
* Is updating
* @returns {Boolean}
*/
isUpdating() {
return this._isUpdating;
}
/**
* Mark component updated.
* @param {ReactComponent} component
*/
setUpdated(component) {
if (this._components.has(component)) {
this._components.set(component, true);
}
}
}
export default new Tracker();