@@ -89,10 +89,10 @@ class RequestHandler: RequestHandlerProtocol {
89
89
func updateCart( items: [ CommerceItem ] ,
90
90
onSuccess: OnSuccessHandler ? ,
91
91
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
92
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
93
- self . chooseRequestProcessor ( ) . updateCart ( items: items,
94
- onSuccess: onSuccess,
95
- onFailure: onFailure)
92
+ sendUsingRequestProcessor { processor in
93
+ processor . updateCart ( items: items,
94
+ onSuccess: onSuccess,
95
+ onFailure: onFailure)
96
96
}
97
97
}
98
98
@@ -104,14 +104,14 @@ class RequestHandler: RequestHandlerProtocol {
104
104
templateId: NSNumber ? ,
105
105
onSuccess: OnSuccessHandler ? ,
106
106
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
107
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
108
- self . chooseRequestProcessor ( ) . trackPurchase ( total,
109
- items: items,
110
- dataFields: dataFields,
111
- campaignId: campaignId,
112
- templateId: templateId,
113
- onSuccess: onSuccess,
114
- onFailure: onFailure)
107
+ sendUsingRequestProcessor { processor in
108
+ processor . trackPurchase ( total,
109
+ items: items,
110
+ dataFields: dataFields,
111
+ campaignId: campaignId,
112
+ templateId: templateId,
113
+ onSuccess: onSuccess,
114
+ onFailure: onFailure)
115
115
}
116
116
}
117
117
@@ -123,14 +123,14 @@ class RequestHandler: RequestHandlerProtocol {
123
123
dataFields: [ AnyHashable : Any ] ? ,
124
124
onSuccess: OnSuccessHandler ? ,
125
125
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
126
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
127
- self . chooseRequestProcessor ( ) . trackPushOpen ( campaignId,
128
- templateId: templateId,
129
- messageId: messageId,
130
- appAlreadyRunning: appAlreadyRunning,
131
- dataFields: dataFields,
132
- onSuccess: onSuccess,
133
- onFailure: onFailure)
126
+ sendUsingRequestProcessor { processor in
127
+ processor . trackPushOpen ( campaignId,
128
+ templateId: templateId,
129
+ messageId: messageId,
130
+ appAlreadyRunning: appAlreadyRunning,
131
+ dataFields: dataFields,
132
+ onSuccess: onSuccess,
133
+ onFailure: onFailure)
134
134
}
135
135
}
136
136
@@ -139,11 +139,11 @@ class RequestHandler: RequestHandlerProtocol {
139
139
dataFields: [ AnyHashable : Any ] ? ,
140
140
onSuccess: OnSuccessHandler ? ,
141
141
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
142
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
143
- self . chooseRequestProcessor ( ) . track ( event: event,
144
- dataFields: dataFields,
145
- onSuccess: onSuccess,
146
- onFailure: onFailure)
142
+ sendUsingRequestProcessor { processor in
143
+ processor . track ( event: event,
144
+ dataFields: dataFields,
145
+ onSuccess: onSuccess,
146
+ onFailure: onFailure)
147
147
}
148
148
}
149
149
@@ -162,12 +162,12 @@ class RequestHandler: RequestHandlerProtocol {
162
162
inboxSessionId: String ? ,
163
163
onSuccess: OnSuccessHandler ? ,
164
164
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
165
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
166
- self . chooseRequestProcessor ( ) . trackInAppOpen ( message,
167
- location: location,
168
- inboxSessionId: inboxSessionId,
169
- onSuccess: onSuccess,
170
- onFailure: onFailure)
165
+ sendUsingRequestProcessor { processor in
166
+ processor . trackInAppOpen ( message,
167
+ location: location,
168
+ inboxSessionId: inboxSessionId,
169
+ onSuccess: onSuccess,
170
+ onFailure: onFailure)
171
171
}
172
172
}
173
173
@@ -178,13 +178,13 @@ class RequestHandler: RequestHandlerProtocol {
178
178
clickedUrl: String ,
179
179
onSuccess: OnSuccessHandler ? ,
180
180
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
181
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
182
- self . chooseRequestProcessor ( ) . trackInAppClick ( message,
183
- location: location,
184
- inboxSessionId: inboxSessionId,
185
- clickedUrl: clickedUrl,
186
- onSuccess: onSuccess,
187
- onFailure: onFailure)
181
+ sendUsingRequestProcessor { processor in
182
+ processor . trackInAppClick ( message,
183
+ location: location,
184
+ inboxSessionId: inboxSessionId,
185
+ clickedUrl: clickedUrl,
186
+ onSuccess: onSuccess,
187
+ onFailure: onFailure)
188
188
}
189
189
}
190
190
@@ -196,47 +196,47 @@ class RequestHandler: RequestHandlerProtocol {
196
196
clickedUrl: String ? ,
197
197
onSuccess: OnSuccessHandler ? ,
198
198
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
199
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
200
- self . chooseRequestProcessor ( ) . trackInAppClose ( message,
201
- location: location,
202
- inboxSessionId: inboxSessionId,
203
- source: source,
204
- clickedUrl: clickedUrl,
205
- onSuccess: onSuccess,
206
- onFailure: onFailure)
199
+ sendUsingRequestProcessor { processor in
200
+ processor . trackInAppClose ( message,
201
+ location: location,
202
+ inboxSessionId: inboxSessionId,
203
+ source: source,
204
+ clickedUrl: clickedUrl,
205
+ onSuccess: onSuccess,
206
+ onFailure: onFailure)
207
207
}
208
208
}
209
209
210
210
@discardableResult
211
211
func track( inboxSession: IterableInboxSession ,
212
212
onSuccess: OnSuccessHandler ? ,
213
213
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
214
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
215
- self . chooseRequestProcessor ( ) . track ( inboxSession: inboxSession,
216
- onSuccess: onSuccess,
217
- onFailure: onFailure)
214
+ sendUsingRequestProcessor { processor in
215
+ processor . track ( inboxSession: inboxSession,
216
+ onSuccess: onSuccess,
217
+ onFailure: onFailure)
218
218
}
219
219
}
220
220
221
221
@discardableResult
222
222
func track( inAppDelivery message: IterableInAppMessage ,
223
223
onSuccess: OnSuccessHandler ? ,
224
224
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
225
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
226
- self . chooseRequestProcessor ( ) . track ( inAppDelivery: message,
227
- onSuccess: onSuccess,
228
- onFailure: onFailure)
225
+ sendUsingRequestProcessor { processor in
226
+ processor . track ( inAppDelivery: message,
227
+ onSuccess: onSuccess,
228
+ onFailure: onFailure)
229
229
}
230
230
}
231
231
232
232
@discardableResult
233
233
func inAppConsume( _ messageId: String ,
234
234
onSuccess: OnSuccessHandler ? ,
235
235
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
236
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
237
- self . chooseRequestProcessor ( ) . inAppConsume ( messageId,
238
- onSuccess: onSuccess,
239
- onFailure: onFailure)
236
+ sendUsingRequestProcessor { processor in
237
+ processor . inAppConsume ( messageId,
238
+ onSuccess: onSuccess,
239
+ onFailure: onFailure)
240
240
}
241
241
}
242
242
@@ -247,13 +247,13 @@ class RequestHandler: RequestHandlerProtocol {
247
247
inboxSessionId: String ? ,
248
248
onSuccess: OnSuccessHandler ? ,
249
249
onFailure: OnFailureHandler ? ) -> Pending < SendRequestValue , SendRequestError > {
250
- Pending < SendRequestValue , SendRequestError > . inBackgroundThread {
251
- self . chooseRequestProcessor ( ) . inAppConsume ( message: message,
252
- location: location,
253
- source: source,
254
- inboxSessionId: inboxSessionId,
255
- onSuccess: onSuccess,
256
- onFailure: onFailure)
250
+ sendUsingRequestProcessor { processor in
251
+ processor . inAppConsume ( message: message,
252
+ location: location,
253
+ source: source,
254
+ inboxSessionId: inboxSessionId,
255
+ onSuccess: onSuccess,
256
+ onFailure: onFailure)
257
257
}
258
258
}
259
259
@@ -272,19 +272,27 @@ class RequestHandler: RequestHandlerProtocol {
272
272
private let offlineProcessor : OfflineRequestProcessor ?
273
273
private let healthMonitor : HealthMonitor ?
274
274
private let onlineProcessor : OnlineRequestProcessor
275
-
276
- private func chooseRequestProcessor( ) -> RequestProcessorProtocol {
275
+
276
+ private func sendUsingRequestProcessor( closure: @escaping ( RequestProcessorProtocol ) -> Pending < SendRequestValue , SendRequestError > ) -> Pending < SendRequestValue , SendRequestError > {
277
+ chooseRequestProcessor ( ) . flatMap { processor in
278
+ closure ( processor)
279
+ }
280
+ }
281
+
282
+ private func chooseRequestProcessor( ) -> Pending < RequestProcessorProtocol , Never > {
277
283
guard offlineMode else {
278
- return onlineProcessor
284
+ return Fulfill < RequestProcessorProtocol , Never > ( value : onlineProcessor)
279
285
}
280
286
guard
281
287
let offlineProcessor = offlineProcessor,
282
288
let healthMonitor = healthMonitor
283
289
else {
284
- return onlineProcessor
290
+ return Fulfill < RequestProcessorProtocol , Never > ( value: onlineProcessor)
291
+ }
292
+
293
+ return healthMonitor. canSchedule ( ) . map { value -> RequestProcessorProtocol in
294
+ value ? offlineProcessor : self . onlineProcessor
285
295
}
286
-
287
- return healthMonitor. canSchedule ( ) ? offlineProcessor : onlineProcessor
288
296
}
289
297
}
290
298
0 commit comments