@@ -7,6 +7,13 @@ type TimelinePlugins = {
7
7
[ key in PluginType ] ?: Plugin [ ] ;
8
8
} ;
9
9
10
+ const PLUGIN_ORDER = [
11
+ PluginType . before ,
12
+ PluginType . enrichment ,
13
+ PluginType . destination ,
14
+ PluginType . after ,
15
+ ] ;
16
+
10
17
export class Timeline {
11
18
plugins : TimelinePlugins = { } ;
12
19
@@ -52,40 +59,24 @@ export class Timeline {
52
59
async process (
53
60
incomingEvent : SegmentEvent
54
61
) : 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 ;
60
63
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
+ } ) ;
69
69
70
- if ( enrichmentResult === undefined ) {
71
- return ;
70
+ if ( key !== PluginType . destination ) {
71
+ if ( result === undefined ) {
72
+ return ;
73
+ } else {
74
+ result = pluginResult ;
75
+ }
76
+ }
72
77
}
73
78
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 ;
89
80
}
90
81
91
82
async applyPlugins ( {
@@ -106,6 +97,9 @@ export class Timeline {
106
97
// Each destination is independent from each other, so we don't roll over changes caused internally in each one of their processing
107
98
if ( type !== PluginType . destination ) {
108
99
result = await pluginResult ;
100
+ if ( result === undefined ) {
101
+ break ;
102
+ }
109
103
}
110
104
} catch ( error ) {
111
105
plugin . analytics ?. reportInternalError (
0 commit comments