@@ -65,6 +65,22 @@ public protocol VersionedPlugin {
65
65
// For internal platform-specific bits
66
66
internal protocol PlatformPlugin : Plugin { }
67
67
68
+ public typealias EnrichmentClosure = ( _ event: RawEvent ? ) -> RawEvent ?
69
+ public class ClosureEnrichment : Plugin {
70
+ public var type : PluginType = . enrichment
71
+ public var analytics : Analytics ? = nil
72
+
73
+ internal let closure : EnrichmentClosure
74
+
75
+ init ( closure: @escaping EnrichmentClosure ) {
76
+ self . closure = closure
77
+ }
78
+
79
+ public func execute< T: RawEvent > ( event: T ? ) -> T ? {
80
+ return closure ( event) as? T
81
+ }
82
+ }
83
+
68
84
69
85
// MARK: - Plugin instance helpers
70
86
extension Plugin {
@@ -98,7 +114,7 @@ extension DestinationPlugin {
98
114
Adds a new plugin to the currently loaded set.
99
115
100
116
- Parameter plugin: The plugin to be added.
101
- - Returns: Returns the name of the supplied plugin.
117
+ - Returns: Returns the supplied plugin.
102
118
103
119
*/
104
120
@discardableResult
@@ -110,6 +126,23 @@ extension DestinationPlugin {
110
126
return plugin
111
127
}
112
128
129
+ /**
130
+ Adds a new enrichment to the currently loaded set of plugins.
131
+
132
+ - Parameter enrichment: The enrichment closure to be added.
133
+ - Returns: Returns the the generated plugin.
134
+
135
+ */
136
+ @discardableResult
137
+ public func add( enrichment: @escaping EnrichmentClosure ) -> Plugin {
138
+ let plugin = ClosureEnrichment ( closure: enrichment)
139
+ if let analytics = self . analytics {
140
+ plugin. configure ( analytics: analytics)
141
+ }
142
+ timeline. add ( plugin: plugin)
143
+ return plugin
144
+ }
145
+
113
146
/**
114
147
Removes and unloads plugins with a matching name from the system.
115
148
@@ -147,6 +180,21 @@ extension Analytics {
147
180
return plugin
148
181
}
149
182
183
+ /**
184
+ Adds a new enrichment to the currently loaded set of plugins.
185
+
186
+ - Parameter enrichment: The enrichment closure to be added.
187
+ - Returns: Returns the the generated plugin.
188
+
189
+ */
190
+ @discardableResult
191
+ public func add( enrichment: @escaping EnrichmentClosure ) -> Plugin {
192
+ let plugin = ClosureEnrichment ( closure: enrichment)
193
+ plugin. configure ( analytics: self )
194
+ timeline. add ( plugin: plugin)
195
+ return plugin
196
+ }
197
+
150
198
/**
151
199
Removes and unloads plugins with a matching name from the system.
152
200
0 commit comments