-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper-adapter-track.ts
32 lines (31 loc) · 1.11 KB
/
helper-adapter-track.ts
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
import type { TrackContext } from '../types/types-create.js';
import type {
TrackAdapterMap,
TrackEventDataBase,
} from '../types/types-track.js';
/**
* Executes the track function of each adapter in the adapterMap for the given eventType and result.
*
* @template Context - The type of the track context.
* @template EventData - The type of the track event data.
* @param {Context} ctx - The track context.
* @param {TrackAdapterMap<Context, EventData>} adapterMap - The map of adapters.
* @param {keyof EventData} eventType - The event type.
* @param {EventData} result - The event data.
* @returns {Promise<EventData>} - The updated event data.
*/
export const executeAdapterTrack = async <
Context extends TrackContext<any>,
EventData extends TrackEventDataBase,
EventType extends keyof EventData,
>(
ctx: Context,
adapterMap: TrackAdapterMap<Context, EventData>,
eventType: EventType,
result: EventData[EventType]
): Promise<EventData> => {
for (const [adapterName, adapter] of Object.entries(adapterMap)) {
await adapter.track<EventType>(adapterName, ctx, eventType, result);
}
return result;
};