-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
base-node.js
323 lines (277 loc) · 9.51 KB
/
base-node.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
/**
* A union of all types derived from BaseNode, allowing type check discrimination
* based on `node.type`. If a new node type is created, it should be added here.
* @typedef {import('./cpu-node.js') | import('./network-node.js')} Node
*/
/**
* @fileoverview This class encapsulates logic for handling resources and tasks used to model the
* execution dependency graph of the page. A node has a unique identifier and can depend on other
* nodes/be depended on. The construction of the graph maintains some important invariants that are
* inherent to the model:
*
* 1. The graph is a DAG, there are no cycles.
* 2. There is always a root node upon which all other nodes eventually depend.
*
* This allows particular optimizations in this class so that we do no need to check for cycles as
* these methods are called and we can always start traversal at the root node.
*/
class BaseNode {
/**
* @param {string} id
*/
constructor(id) {
this._id = id;
this._isMainDocument = false;
/** @type {Node[]} */
this._dependents = [];
/** @type {Node[]} */
this._dependencies = [];
}
/**
* @return {string}
*/
get id() {
return this._id;
}
/**
* @return {typeof BaseNode.TYPES[keyof typeof BaseNode.TYPES]}
*/
get type() {
throw new Error('Unimplemented');
}
/**
* @return {number}
*/
get startTime() {
throw new Error('Unimplemented');
}
/**
* @return {number}
*/
get endTime() {
throw new Error('Unimplemented');
}
/**
* @param {boolean} value
*/
setIsMainDocument(value) {
this._isMainDocument = value;
}
/**
* @return {boolean}
*/
isMainDocument() {
return this._isMainDocument;
}
/**
* @return {Node[]}
*/
getDependents() {
return this._dependents.slice();
}
/**
* @return {number}
*/
getNumberOfDependents() {
return this._dependents.length;
}
/**
* @return {Node[]}
*/
getDependencies() {
return this._dependencies.slice();
}
/**
* @return {number}
*/
getNumberOfDependencies() {
return this._dependencies.length;
}
/**
* @return {Node}
*/
getRootNode() {
let rootNode = /** @type {Node} */ (/** @type {BaseNode} */ (this));
while (rootNode._dependencies.length) {
rootNode = rootNode._dependencies[0];
}
return rootNode;
}
/**
* @param {Node} node
*/
addDependent(node) {
node.addDependency(/** @type {Node} */ (/** @type {BaseNode} */ (this)));
}
/**
* @param {Node} node
*/
addDependency(node) {
// @ts-ignore - in checkJs, ts doesn't know that CPUNode and NetworkNode *are* BaseNodes.
if (node === this) throw new Error('Cannot add dependency on itself');
if (this._dependencies.includes(node)) {
return;
}
node._dependents.push(/** @type {Node} */ (/** @type {BaseNode} */ (this)));
this._dependencies.push(node);
}
/**
* @param {Node} node
*/
removeDependent(node) {
node.removeDependency(/** @type {Node} */ (/** @type {BaseNode} */ (this)));
}
/**
* @param {Node} node
*/
removeDependency(node) {
if (!this._dependencies.includes(node)) {
return;
}
const thisIndex = node._dependents.indexOf(/** @type {Node} */ (/** @type {BaseNode} */(this)));
node._dependents.splice(thisIndex, 1);
this._dependencies.splice(this._dependencies.indexOf(node), 1);
}
removeAllDependencies() {
for (const node of this._dependencies.slice()) {
this.removeDependency(node);
}
}
/**
* Clones the node's information without adding any dependencies/dependents.
* @return {Node}
*/
cloneWithoutRelationships() {
const node = /** @type {Node} */ (new BaseNode(this.id));
node.setIsMainDocument(this._isMainDocument);
return node;
}
/**
* Clones the entire graph connected to this node filtered by the optional predicate. If a node is
* included by the predicate, all nodes along the paths between the node and the root will be included. If the
* node this was called on is not included in the resulting filtered graph, the method will throw.
* @param {function(Node):boolean} [predicate]
* @return {Node}
*/
cloneWithRelationships(predicate) {
const rootNode = this.getRootNode();
/** @type {Map<string, Node>} */
const idsToIncludedClones = new Map();
// Walk down dependents.
rootNode.traverse(node => {
if (idsToIncludedClones.has(node.id)) return;
if (predicate === undefined) {
// No condition for entry, so clone every node.
idsToIncludedClones.set(node.id, node.cloneWithoutRelationships());
return;
}
if (predicate(node)) {
// Node included, so walk back up dependencies, cloning nodes from here back to the root.
node.traverse(
node => idsToIncludedClones.set(node.id, node.cloneWithoutRelationships()),
// Dependencies already cloned have already cloned ancestors, so no need to visit again.
node => node._dependencies.filter(parent => !idsToIncludedClones.has(parent.id))
);
}
});
// Copy dependencies between nodes.
rootNode.traverse(originalNode => {
const clonedNode = idsToIncludedClones.get(originalNode.id);
if (!clonedNode) return;
for (const dependency of originalNode._dependencies) {
const clonedDependency = idsToIncludedClones.get(dependency.id);
if (!clonedDependency) throw new Error('Dependency somehow not cloned');
clonedNode.addDependency(clonedDependency);
}
});
const clonedThisNode = idsToIncludedClones.get(this.id);
if (!clonedThisNode) throw new Error('Cloned graph missing node');
return clonedThisNode;
}
/**
* Traverses all connected nodes in BFS order, calling `callback` exactly once
* on each. `traversalPath` is the shortest (though not necessarily unique)
* path from `node` to the root of the iteration.
*
* The `getNextNodes` function takes a visited node and returns which nodes to
* visit next. It defaults to returning the node's dependents.
* @param {(node: Node, traversalPath: Node[]) => void} callback
* @param {function(Node): Node[]} [getNextNodes]
*/
traverse(callback, getNextNodes) {
if (!getNextNodes) {
getNextNodes = node => node.getDependents();
}
/** @type {Node[][]} */
// @ts-ignore - only traverses graphs of Node, so force tsc to treat `this` as one
const queue = [[this]];
const visited = new Set([this.id]);
while (queue.length) {
/** @type {Node[]} */
// @ts-ignore - queue has length so it's guaranteed to have an item
const traversalPath = queue.shift();
const node = traversalPath[0];
callback(node, traversalPath);
for (const nextNode of getNextNodes(node)) {
if (visited.has(nextNode.id)) continue;
visited.add(nextNode.id);
queue.push([nextNode, ...traversalPath]);
}
}
}
/**
* Returns whether the given node has a cycle in its dependent graph by performing a DFS.
* @param {Node} node
* @param {'dependents'|'dependencies'|'both'} [direction]
* @return {boolean}
*/
static hasCycle(node, direction = 'both') {
// Checking 'both' is the default entrypoint to recursively check both directions
if (direction === 'both') {
return BaseNode.hasCycle(node, 'dependents') || BaseNode.hasCycle(node, 'dependencies');
}
const visited = new Set();
/** @type {Node[]} */
const currentPath = [];
const toVisit = [node];
const depthAdded = new Map([[node, 0]]);
// Keep going while we have nodes to visit in the stack
while (toVisit.length) {
// Get the last node in the stack (DFS uses stack, not queue)
/** @type {Node} */
// @ts-ignore - toVisit has length so it's guaranteed to have an item
const currentNode = toVisit.pop();
// We've hit a cycle if the node we're visiting is in our current dependency path
if (currentPath.includes(currentNode)) return true;
// If we've already visited the node, no need to revisit it
if (visited.has(currentNode)) continue;
// Since we're visiting this node, clear out any nodes in our path that we had to backtrack
// @ts-ignore
while (currentPath.length > depthAdded.get(currentNode)) currentPath.pop();
// Update our data structures to reflect that we're adding this node to our path
visited.add(currentNode);
currentPath.push(currentNode);
// Add all of its dependents to our toVisit stack
const nodesToExplore = direction === 'dependents' ?
currentNode._dependents :
currentNode._dependencies;
for (const nextNode of nodesToExplore) {
if (toVisit.includes(nextNode)) continue;
toVisit.push(nextNode);
depthAdded.set(nextNode, currentPath.length);
}
}
return false;
}
}
BaseNode.TYPES = /** @type {{NETWORK: 'network', CPU: 'cpu'}} */({
NETWORK: 'network',
CPU: 'cpu',
});
module.exports = BaseNode;