Repository navigation
Expand file tree
/
Copy patheventProcessors.ts
More file actions
46 lines (39 loc) · 1.41 KB
/
Copy patheventProcessors.ts
File metadata and controls
46 lines (39 loc) · 1.41 KB
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
38
39
40
41
42
43
44
45
46
import { DEBUG_BUILD } from './debug-build';
import type { Event, EventHint } from './types/event';
import type { EventProcessor } from './types/eventprocessor';
import { debug } from './utils/debug-logger';
import { isThenable } from './utils/is';
import { rejectedSyncPromise, resolvedSyncPromise } from './utils/syncpromise';
/**
* Process an array of event processors, returning the processed event (or `null` if the event was dropped).
*/
export function notifyEventProcessors(
processors: EventProcessor[],
event: Event | null,
hint: EventHint,
index: number = 0,
): PromiseLike<Event | null> {
try {
const result = _notifyEventProcessors(event, hint, processors, index);
return isThenable(result) ? result : resolvedSyncPromise(result);
} catch (error) {
return rejectedSyncPromise(error);
}
}
function _notifyEventProcessors(
event: Event | null,
hint: EventHint,
processors: EventProcessor[],
index: number,
): Event | null | PromiseLike<Event | null> {
const processor = processors[index];
if (!event || !processor) {
return event;
}
const result = processor({ ...event }, hint);
DEBUG_BUILD && result === null && debug.log(`Event processor "${processor.id || '?'}" dropped event`);
if (isThenable(result)) {
return result.then(final => _notifyEventProcessors(final, hint, processors, index + 1));
}
return _notifyEventProcessors(result, hint, processors, index + 1);
}