-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFQReflowTree.js
246 lines (211 loc) · 5.9 KB
/
NFQReflowTree.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import NFQReflowStore from './NFQReflowStore';
/**
* Reflow Tree Singleton.
*/
class NFQReflowTreeClass {
/**
* Constructs the Reflow Tree.
*/
constructor() {
this.nodeTree = {};
this.numberOfValues = 0;
}
/**
* Adds an Node with Hash to the Table.
*
* @param {NFQReflowComponent} node Component node.
* @param {String} renderedTemplate Components rendered html.
*
* @returns {String} MD5 Hash for this Component.
*/
addNode(node, renderedTemplate) {
if (node.hash === null) {
node.hash = this.generateUUID();
this.numberOfValues++;
}
this.nodeTree[node.hash] = {
node: node,
rendered: renderedTemplate,
nodeTmp: this.circleSaveStringify(node)
};
this.updateParent(node);
return node.hash;
}
/**
* Updates parent node with child hashes.
*
* @param {NFQReflowComponent} node Component node.
*/
updateParent(node) {
let parent;
if (node.parentHash !== null) {
parent = this.find(node.parentHash);
parent.nodeTmp = this.circleSaveStringify(parent.node);
}
}
/**
* Checks if it must render or not.
*
* @param {NFQReflowComponent} node Component node.
*
* @return {Boolean} Same or not.
*/
checkNode(node) {
const nodeString = this.circleSaveStringify(node);
const tmpNode = this.find(node.hash);
return (tmpNode === null) ? true : !(nodeString === tmpNode.nodeTmp);
}
/**
* Removes Item from Tree.
*
* @param {String} hash Hash of item.
*/
removeNode(hash) {
this.unsetEvents(this.nodeTree[hash]);
delete this.nodeTree[hash];
this.numberOfValues--;
}
/**
* Unsets all Events for this Component
*
* @param {NFQReflowTreeComponent} node Node Branch.
*/
unsetEvents(node) {
let eventList = node.node.eventList, event;
for (event in eventList) {
node.node.off(event);
}
}
/**
* Finds a component with hash.
*
* @param {String} hash Hash of item.
*
* @returns {Object|NFQReflowComponent} Returns null or found item.
*/
find(hash) {
let ret;
if (this.nodeTree.hasOwnProperty(hash)) {
ret = this.nodeTree[hash];
} else {
ret = null;
}
return ret;
}
/**
* Shows length of Tree.
*
* @returns {Number} Returns number of items.
*/
length() {
return this.numberOfValues;
}
/**
* Cleans up hashmap tree.
*
* @param {NFQReflowComponent} node Component node.
* @param {mixed} usedChilds Childs used at the moment.
*/
clean(node, usedChilds) {
/* eslint-disable no-magic-numbers */
let index;
let children = this.findChildren(node.hash);
let nodeChilds = this.getChilds(node, usedChilds);
let diff = children.filter(
function(i) {
return nodeChilds.indexOf(i) < 0;
}
);
/* eslint-enable no-magic-numbers */
for (index in diff) {
this.killChildren(diff[index]);
}
}
/**
* Recursively deletes all Children.
*
* @param {string} hash Node Hash.
*/
killChildren(hash) {
let children = this.findChildren(hash);
let index;
NFQReflowStore.clean(hash);
this.removeNode(hash);
for (index in children) {
this.killChildren(children[index]);
}
}
/**
* Finds childs of an hash indexed parent node.
*
* @param {String} parentHash Parent component hash.
*
* @return {Array} Array of child hashes.
*/
findChildren(parentHash) {
let hash, branch, ret = [];
for ([hash, branch] of Object.entries(this.nodeTree)) {
if (branch.node.parentHash === parentHash) {
ret.push(hash);
}
}
return ret;
}
/**
* Gets all child hashes of the actual node.
*
* @param {NFQReflowComponent} node Component node.
* @param {mixed} usedChilds Childs used at the moment.
*
* @return {Array} Array of child hashes.
*/
getChilds(node, usedChilds) {
/* eslint-disable no-unused-vars */
let param, child, ret = [];
for ([param, child] of Object.entries(node.children)) {
if (usedChilds.indexOf(param) !== -1) {
ret.push(child.hash);
}
}
return ret;
/* eslint-enable no-unused-vars */
}
/**
* Generates an UUID for Hashing.
*
* @returns {Sting} An one in a million collision UUID.
*/
generateUUID() {
const Uint8ArrayKey = 1;
const Uint8ArraySalt = 15;
const StringBit = 16;
const divider = 4;
const replaceString = '10000000-1000-4000-8000-100000000000';
/* eslint-disable max-len */
return replaceString.replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(Uint8ArrayKey))[0] & Uint8ArraySalt >> c / divider).toString(StringBit)
);
/* eslint-enable max-len */
}
/**
* JSON Stringify Circular save.
*
* @param {NFQReflowComponent} obj Component.
*
* @returns {String} JSON of given Object.
*/
circleSaveStringify(obj) {
const cache = new Map();
return JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.get(value)) {
return;
}
cache.set(value, true);
}
return value;
});
}
}
const NFQReflowTree = new NFQReflowTreeClass();
export default NFQReflowTree;