-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector.js
More file actions
54 lines (39 loc) · 1.46 KB
/
connector.js
File metadata and controls
54 lines (39 loc) · 1.46 KB
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
export function waitForChildren (element, callback) {
let children = Array.from(element.children)
.filter(child => !child._isReady);
if (children.length === 0) {
runCallback(element, callback);
} else {
element.addEventListener("elementReady", handleChildReady);
console.log(`${element._id}: waiting for ${children.length} children`);
} // if
function handleChildReady (e) {
if (!children.includes(e.target)) return;
// remove this child and we're done if no more children left to process
children = children.filter(x => x !== e.target);
//console.debug(`${element._id}: child ${e.target._id} is ready; ${children.length} remaining`);
if (children.length > 0) return;
// no more children left, so remove this handler and signal ready on this element
element.removeEventListener("elementReady", handleChildReady);
//setTimeout(() => {
runCallback(element, callback);
//}, 0);
} // handleChildReady
function runCallback (element, callback) {
//console.debug(`${element._id}: all children ready; executing callback`);
try {
//callback(Array.from(element.children));
//setTimeout(() => {
callback.call(element, Array.from(element.children));
//}, 0);
signalReady(element);
} catch (e) {
console.error(`abort: ${e}\n${e.stack}\n`);
} // catch
} // runCallback
} // waitForChildren
export function signalReady (element) {
element.dispatchEvent(new CustomEvent("elementReady", {bubbles: true}));
element._isReady = true;
//console.debug (`${element._id} signaling ready`);
} // signalReady