Skip to content

Commit 7981efa

Browse files
authored
chore: refactor timeline processing to simplify code (#749)
1 parent 2777bea commit 7981efa

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

packages/core/src/timeline.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ type TimelinePlugins = {
77
[key in PluginType]?: Plugin[];
88
};
99

10+
const PLUGIN_ORDER = [
11+
PluginType.before,
12+
PluginType.enrichment,
13+
PluginType.destination,
14+
PluginType.after,
15+
];
16+
1017
export class Timeline {
1118
plugins: TimelinePlugins = {};
1219

@@ -52,40 +59,24 @@ export class Timeline {
5259
async process(
5360
incomingEvent: SegmentEvent
5461
): Promise<SegmentEvent | undefined> {
55-
// apply .before and .enrichment types first ...
56-
const beforeResult = await this.applyPlugins({
57-
type: PluginType.before,
58-
event: incomingEvent,
59-
});
62+
let result: SegmentEvent | undefined = incomingEvent;
6063

61-
if (beforeResult === undefined) {
62-
return;
63-
}
64-
// .enrichment here is akin to source middleware in the old analytics-ios.
65-
const enrichmentResult = await this.applyPlugins({
66-
type: PluginType.enrichment,
67-
event: beforeResult,
68-
});
64+
for (const key of PLUGIN_ORDER) {
65+
let pluginResult: SegmentEvent | undefined = await this.applyPlugins({
66+
type: key,
67+
event: result!,
68+
});
6969

70-
if (enrichmentResult === undefined) {
71-
return;
70+
if (key !== PluginType.destination) {
71+
if (result === undefined) {
72+
return;
73+
} else {
74+
result = pluginResult;
75+
}
76+
}
7277
}
7378

74-
// once the event enters a destination, we don't want
75-
// to know about changes that happen there. those changes
76-
// are to only be received by the destination.
77-
await this.applyPlugins({
78-
type: PluginType.destination,
79-
event: enrichmentResult,
80-
});
81-
82-
// apply .after plugins ...
83-
let afterResult = await this.applyPlugins({
84-
type: PluginType.after,
85-
event: enrichmentResult,
86-
});
87-
88-
return afterResult;
79+
return result;
8980
}
9081

9182
async applyPlugins({
@@ -106,6 +97,9 @@ export class Timeline {
10697
// Each destination is independent from each other, so we don't roll over changes caused internally in each one of their processing
10798
if (type !== PluginType.destination) {
10899
result = await pluginResult;
100+
if (result === undefined) {
101+
break;
102+
}
109103
}
110104
} catch (error) {
111105
plugin.analytics?.reportInternalError(

0 commit comments

Comments
 (0)