@@ -8,6 +8,7 @@ public class UnleashClientBase {
8
8
var poller : Poller
9
9
var metrics : Metrics
10
10
var connectionId : UUID
11
+ private let queue = DispatchQueue ( label: " com.unleash.clientbase " , attributes: . concurrent)
11
12
12
13
public init (
13
14
unleashUrl: String ,
@@ -61,7 +62,7 @@ public class UnleashClientBase {
61
62
}
62
63
self . metrics = Metrics ( appName: appName, metricsInterval: Double ( metricsInterval) , clock: { return Date ( ) } , disableMetrics: disableMetrics, poster: urlSessionPoster, url: url, clientKey: clientKey, customHeaders: customHeaders, connectionId: connectionId)
63
64
}
64
-
65
+
65
66
self . context = Context ( appName: appName, environment: environment, sessionId: String ( Int . random ( in: 0 ..< 1_000_000_000 ) ) )
66
67
if let providedContext = context {
67
68
self . context = self . calculateContext ( context: providedContext)
@@ -74,13 +75,13 @@ public class UnleashClientBase {
74
75
completionHandler: ( ( PollerError ? ) -> Void ) ? = nil
75
76
) -> Void {
76
77
Printer . showPrintStatements = printToConsole
77
- self . stopPolling ( )
78
- poller. start (
79
- bootstrapping: bootstrap. toggles,
80
- context: context,
81
- completionHandler: completionHandler
82
- )
83
- metrics. start ( )
78
+ self . stopPolling ( )
79
+ poller. start (
80
+ bootstrapping: bootstrap. toggles,
81
+ context: context,
82
+ completionHandler: completionHandler
83
+ )
84
+ metrics. start ( )
84
85
}
85
86
86
87
private func stopPolling( ) -> Void {
@@ -94,40 +95,50 @@ public class UnleashClientBase {
94
95
}
95
96
96
97
public func isEnabled( name: String ) -> Bool {
97
- let toggle = poller. getFeature ( name: name)
98
- let enabled = toggle? . enabled ?? false
99
-
100
- metrics. count ( name: name, enabled: enabled)
101
-
102
- if let toggle = toggle, toggle. impressionData {
103
- SwiftEventBus . post ( " impression " , sender: ImpressionEvent (
104
- toggleName: name,
105
- enabled: enabled,
106
- context: context
107
- ) )
98
+ return queue. sync {
99
+ let toggle = poller. getFeature ( name: name)
100
+ let enabled = toggle? . enabled ?? false
101
+
102
+ metrics. count ( name: name, enabled: enabled)
103
+
104
+ if let toggle = toggle, toggle. impressionData {
105
+ // Dispatch impression event to background queue
106
+ DispatchQueue . global ( ) . async {
107
+ SwiftEventBus . post ( " impression " , sender: ImpressionEvent (
108
+ toggleName: name,
109
+ enabled: enabled,
110
+ context: self . context
111
+ ) )
112
+ }
113
+ }
114
+
115
+ return enabled
108
116
}
109
-
110
- return enabled
111
117
}
112
118
113
119
public func getVariant( name: String ) -> Variant {
114
- let toggle = poller. getFeature ( name: name)
115
- let variant = toggle? . variant ?? . defaultDisabled
116
- let enabled = toggle? . enabled ?? false
120
+ return queue. sync {
121
+ let toggle = poller. getFeature ( name: name)
122
+ let variant = toggle? . variant ?? . defaultDisabled
123
+ let enabled = toggle? . enabled ?? false
117
124
118
- metrics. count ( name: name, enabled: enabled)
119
- metrics. countVariant ( name: name, variant: variant. name)
120
-
121
- if let toggle = toggle, toggle. impressionData {
122
- SwiftEventBus . post ( " impression " , sender: ImpressionEvent (
123
- toggleName: name,
124
- enabled: enabled,
125
- variant: variant,
126
- context: context
127
- ) )
125
+ metrics. count ( name: name, enabled: enabled)
126
+ metrics. countVariant ( name: name, variant: variant. name)
127
+
128
+ if let toggle = toggle, toggle. impressionData {
129
+ // Dispatch impression event to background queue
130
+ DispatchQueue . global ( ) . async {
131
+ SwiftEventBus . post ( " impression " , sender: ImpressionEvent (
132
+ toggleName: name,
133
+ enabled: enabled,
134
+ variant: variant,
135
+ context: self . context
136
+ ) )
137
+ }
138
+ }
139
+
140
+ return variant
128
141
}
129
-
130
- return variant
131
142
}
132
143
133
144
public func subscribe( name: String , callback: @escaping ( ) -> Void ) {
@@ -143,7 +154,7 @@ public class UnleashClientBase {
143
154
}
144
155
}
145
156
}
146
-
157
+
147
158
public func subscribe( _ event: UnleashEvent , callback: @escaping ( ) -> Void ) {
148
159
subscribe ( name: event. rawValue, callback: callback)
149
160
}
@@ -156,7 +167,7 @@ public class UnleashClientBase {
156
167
let handler : ( Notification ? ) -> Void = { notification in
157
168
callback ( notification? . object)
158
169
}
159
-
170
+
160
171
if Thread . isMainThread {
161
172
print ( " Subscribing to \( name) on main thread with object " )
162
173
SwiftEventBus . onMainThread ( self , name: name, handler: handler)
@@ -169,14 +180,21 @@ public class UnleashClientBase {
169
180
public func unsubscribe( name: String ) {
170
181
SwiftEventBus . unregister ( self , name: name)
171
182
}
172
-
183
+
173
184
public func unsubscribe( _ event: UnleashEvent ) {
174
185
unsubscribe ( name: event. rawValue)
175
186
}
176
-
187
+
177
188
public func updateContext( context: [ String : String ] , properties: [ String : String ] ? = nil , completionHandler: ( ( PollerError ? ) -> Void ) ? = nil ) {
178
- self . context = self . calculateContext ( context: context, properties: properties)
179
- self . start ( Printer . showPrintStatements, completionHandler: completionHandler)
189
+ if Thread . isMainThread {
190
+ self . context = self . calculateContext ( context: context, properties: properties)
191
+ self . start ( Printer . showPrintStatements, completionHandler: completionHandler)
192
+ } else {
193
+ DispatchQueue . main. async {
194
+ self . context = self . calculateContext ( context: context, properties: properties)
195
+ self . start ( Printer . showPrintStatements, completionHandler: completionHandler)
196
+ }
197
+ }
180
198
}
181
199
182
200
func calculateContext( context: [ String : String ] , properties: [ String : String ] ? = nil ) -> Context {
0 commit comments