Skip to content

Commit

Permalink
test: refactor tick objects prune function
Browse files Browse the repository at this point in the history
PR-URL: #26163
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
asattelmaier authored and addaleax committed Mar 1, 2019
1 parent 6f9ab5e commit 69154e4
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions test/async-hooks/verify-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,37 @@ function pruneTickObjects(activities) {
// Remove one TickObject on each pass until none is left anymore
// not super efficient, but simplest especially to handle
// multiple TickObjects in a row
let foundTickObject = true;
const tickObject = {
found: true,
index: null,
data: null
};

while (foundTickObject) {
foundTickObject = false;
let tickObjectIdx = -1;
while (tickObject.found) {
for (let i = 0; i < activities.length; i++) {
if (activities[i].type !== 'TickObject') continue;
tickObjectIdx = i;
break;
if (activities[i].type === 'TickObject') {
tickObject.index = i;
break;
} else if (i + 1 === activities.length) {
tickObject.found = false;
}
}

if (tickObjectIdx >= 0) {
foundTickObject = true;

if (tickObject.found) {
// Point all triggerAsyncIds that point to the tickObject
// to its triggerAsyncId and finally remove it from the activities
const tickObject = activities[tickObjectIdx];
const newTriggerId = tickObject.triggerAsyncId;
const oldTriggerId = tickObject.uid;
tickObject.data = activities[tickObject.index];
const triggerId = {
new: tickObject.data.triggerAsyncId,
old: tickObject.data.uid
};

activities.forEach(function repointTriggerId(x) {
if (x.triggerAsyncId === oldTriggerId) x.triggerAsyncId = newTriggerId;
if (x.triggerAsyncId === triggerId.old)
x.triggerAsyncId = triggerId.new;
});
activities.splice(tickObjectIdx, 1);

activities.splice(tickObject.index, 1);
}
}
return activities;
Expand Down

0 comments on commit 69154e4

Please sign in to comment.