Skip to content

Commit

Permalink
prevent cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Feb 4, 2025
1 parent f9afe92 commit 98896d1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions core/gather/gatherers/trace-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,27 @@ class TraceElements extends BaseGatherer {
* Execute `cb(obj, key)` on every object property (non-objects only), recursively.
* @param {any} obj
* @param {(obj: Record<string, string>, key: string) => void} cb
* @param {Set<object>} seen
*/
function recursiveObjectEnumerate(obj, cb) {
function recursiveObjectEnumerate(obj, cb, seen) {
if (seen.has(seen)) {
return;
}

seen.add(obj);

if (obj && typeof obj === 'object' && !Array.isArray(obj)) {
Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'object') {
recursiveObjectEnumerate(obj[key], cb);
recursiveObjectEnumerate(obj[key], cb, seen);
} else {
cb(obj, key);
}
});
} else if (Array.isArray(obj)) {
obj.forEach(item => {
if (typeof item === 'object' || Array.isArray(item)) {
recursiveObjectEnumerate(item, cb);
recursiveObjectEnumerate(item, cb, seen);
}
});
}
Expand All @@ -108,7 +115,7 @@ class TraceElements extends BaseGatherer {
if (typeof obj[key] === 'number' && (key === 'nodeId' || key === 'node_id')) {
nodeIds.push(obj[key]);
}
});
}, new Set());

return [...new Set(nodeIds)].map(id => ({nodeId: id}));
}
Expand Down

0 comments on commit 98896d1

Please sign in to comment.