index/classes/PayloadWalkerWithDeduplication.md
Payload Walker with Deduplication
Recursively walks through object payloads to extract events while deduplicating by a key (e.g., transactionId, eventId). Prevents duplicate processing of the same event across multiple payload traversals.
Used to find events nested at various depths in WebSocket payloads.
Features:
- Recursive depth limiting (prevents DoS from circular structures)
- WeakSet tracking to prevent infinite loops on circular references
- Configurable deduplication key
- Callback-based event processing
Example
// Test case: Recursive payload walking with deduplication
const walker = new PayloadWalkerWithDeduplication<{ id: string; value: number }>(
(obj) => {
// Extraction predicate: return event if found
return obj.id && obj.value ? { id: obj.id, value: obj.value } : null;
},
(event) => event.id // Deduplication key
);
// First walk - extracts new events
const events1 = walker.walk({
Data: { id: 'e1', value: 100 },
nested: { id: 'e2', value: 200 }
});
console.assert(events1.length === 2);
// Second walk - skips already seen events
const events2 = walker.walk({
Data: { id: 'e1', value: 999 }, // Already seen, skipped
other: { id: 'e3', value: 300 } // New, included
});
console.assert(events2.length === 1);
console.assert(events2[0].id === 'e3');
// Reset deduplication to reprocess all events
walker.reset();
const events3 = walker.walk({ Data: { id: 'e1', value: 100 } });
console.assert(events3.length === 1); // e1 is processed again
Since
6.6.0
Type Parameters
| Type Parameter | Description |
|---|---|
T | Event type being extracted |
Constructors
Constructor
new PayloadWalkerWithDeduplication<
T>(extractor,dedupeKeyFn,maxDepth?):PayloadWalkerWithDeduplication<T>
Parameters
| Parameter | Type |
|---|---|
extractor | (obj) => T | null |
dedupeKeyFn | (event) => string |
maxDepth? | number |
Returns
PayloadWalkerWithDeduplication<T>
dedupe key fn max depth payload walker with deduplication
Methods
getSeenCount()
getSeenCount():
number
Get count of deduplicated events
Returns
number
Number of unique events seen so far
get seen count
reset()
reset():
void
Reset deduplication state to reprocess all events
Returns
void
walk()
walk(
payload):T[]
Walk through a payload and extract events
Parameters
| Parameter | Type | Description |
|---|---|---|
payload | unknown | Root object to traverse |
Returns
T[]
Array of extracted events not previously seen