-
Notifications
You must be signed in to change notification settings - Fork 3
/
promiserace.js
37 lines (35 loc) · 1.12 KB
/
promiserace.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
/**
* Promise.race(n) creates n+1 promises
* Promise 1 waits for some of promises 2..n+1 to resolve
*/
function apply(entries, asyncObjects, relations) {
let praceindex = 0;
findPromiseRace(entries).forEach((p) => {
praceindex++;
let firstPromise = asyncObjects.getByAsyncId(p.shift())[0];
relations.removeIncomingTo(firstPromise.id); //remove incoming relations to firstPromise
p.forEach((i) => {
let iPromise = asyncObjects.getByAsyncId(i)[0];
relations.hasPromiseRace = true;
relations.add(iPromise.id, firstPromise.id, 'promise-race-' + praceindex);
});
});
}
function findPromiseRace(entries) {
let ret = [];
let curr = null;
entries.forEach(logObj => {
if (logObj.e === 'promise-race-begin') {
curr = [];
}
else if (curr && logObj.e === 'AsyncHook-init' && logObj.type === 'PROMISE') {
curr.push(logObj.id);
}
else if (logObj.e === 'promise-race-end') {
ret.push(curr);
curr = null;
}
});
return ret;
}
module.exports = { apply }