-
Notifications
You must be signed in to change notification settings - Fork 3
/
fifobytimeout.js
32 lines (27 loc) · 1 KB
/
fifobytimeout.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
function apply(entries, asyncObjects, relations) {
let obj = asyncObjects.getAll().filter((o) => o.entry.type === 'Timeout');
//group timeouts registered in the same tick
let g = {};
obj.forEach(e => {
let tick = relations.registeredIn(e.id);
if (!g[tick])
g[tick] = [];
//interval - only the first one is included
if(! hasSomeWithAsyncId(g[tick], e.entry.id))
g[tick].push(e);
});
for (let tick in g) {
let t = g[tick]; //timeout objects in the same tick
for (let i = 0; i < t.length - 1; i++) {
for (let j = i + 1; j < t.length; j++) {
//if timeout is less or equal, registration order is followed
if (t[i].entry.timeout <= t[j].entry.timeout)
relations.add(t[i].id, t[j].id, 'timeout-l');
}
}
}
}
function hasSomeWithAsyncId(arr, asyncId) {
return arr.find((e) => e.entry.id === asyncId);
}
module.exports = { apply }