|
1 | | -import type { Destination, Mapping, WalkerOS } from '@elbwalker/types'; |
2 | 1 | import type { SourceNode, DestinationNode } from '../types'; |
3 | | -import { |
4 | | - debounce, |
5 | | - getMappingEvent, |
6 | | - getId, |
7 | | - isSameType, |
8 | | - useHooks, |
9 | | - getMappingValue, |
10 | | - isDefined, |
11 | | - isObject, |
12 | | - assign, |
13 | | -} from '@elbwalker/utils'; |
| 2 | +import { getId, isSameType } from '@elbwalker/utils'; |
14 | 3 | import { pushToDestinations } from './push'; |
15 | 4 |
|
16 | 5 | export async function addDestination( |
@@ -66,80 +55,3 @@ export async function destinationInit( |
66 | 55 |
|
67 | 56 | return true; // Destination is ready to push |
68 | 57 | } |
69 | | - |
70 | | -function resolveMappingData( |
71 | | - event: WalkerOS.Event, |
72 | | - data?: Mapping.Data, |
73 | | -): Destination.Data { |
74 | | - if (!data) return; |
75 | | - |
76 | | - return getMappingValue(event, data); |
77 | | -} |
78 | | - |
79 | | -export async function destinationPush( |
80 | | - instance: SourceNode.Instance, |
81 | | - destination: DestinationNode.Destination, |
82 | | - event: WalkerOS.Event, |
83 | | -): Promise<boolean> { |
84 | | - const { config } = destination; |
85 | | - const { eventMapping, mappingKey } = getMappingEvent(event, config.mapping); |
86 | | - |
87 | | - let data = resolveMappingData(event, config.data); |
88 | | - |
89 | | - if (eventMapping) { |
90 | | - // Check if event should be processed or ignored |
91 | | - if (eventMapping.ignore) return false; |
92 | | - |
93 | | - // Check to use specific event names |
94 | | - if (eventMapping.name) event.event = eventMapping.name; |
95 | | - |
96 | | - // Transform event to a custom data |
97 | | - if (eventMapping.data) { |
98 | | - const dataEvent = resolveMappingData(event, eventMapping.data); |
99 | | - data = |
100 | | - isObject(data) && isObject(dataEvent) // Only merge objects |
101 | | - ? assign(data, dataEvent) |
102 | | - : dataEvent; |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - const options = { data, instance }; |
107 | | - |
108 | | - if (eventMapping?.batch && destination.pushBatch) { |
109 | | - const batched = eventMapping.batched || { |
110 | | - key: mappingKey || '', |
111 | | - events: [], |
112 | | - data: [], |
113 | | - }; |
114 | | - batched.events.push(event); |
115 | | - if (isDefined(data)) batched.data.push(data); |
116 | | - |
117 | | - eventMapping.batchFn = |
118 | | - eventMapping.batchFn || |
119 | | - debounce((destination, instance) => { |
120 | | - useHooks( |
121 | | - destination.pushBatch!, |
122 | | - 'DestinationPushBatch', |
123 | | - instance.hooks, |
124 | | - )(batched, destination.config, options); |
125 | | - |
126 | | - // Reset the batched queues |
127 | | - // pushBatch isn't async yet, may cause trouble |
128 | | - batched.events = []; |
129 | | - batched.data = []; |
130 | | - }, eventMapping.batch); |
131 | | - |
132 | | - eventMapping.batched = batched; |
133 | | - eventMapping.batchFn(destination, instance); |
134 | | - } else { |
135 | | - // It's time to go to the destination's side now |
136 | | - await useHooks(destination.push, 'DestinationPush', instance.hooks)( |
137 | | - event, |
138 | | - destination.config, |
139 | | - eventMapping, |
140 | | - options, |
141 | | - ); |
142 | | - } |
143 | | - |
144 | | - return true; |
145 | | -} |
0 commit comments