@trycourier/courier-react-native 5.2.3 → 5.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/android/build.gradle +8 -2
  2. package/android/src/main/java/com/courierreactnative/CourierSharedModule.kt +43 -40
  3. package/android/src/main/java/com/courierreactnative/Utils.kt +1 -1
  4. package/courier-react-native.podspec +2 -2
  5. package/ios/CourierReactNativeDelegate.m +2 -6
  6. package/ios/CourierReactNativeEventEmitter.swift +1 -1
  7. package/ios/CourierReactNativeModule.m +53 -19
  8. package/ios/CourierSharedModule.swift +298 -267
  9. package/ios/Utils.swift +3 -1
  10. package/lib/commonjs/Broadcaster.js +3 -1
  11. package/lib/commonjs/Broadcaster.js.map +1 -1
  12. package/lib/commonjs/index.js +52 -50
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/models/CourierAuthenticationListener.js +2 -2
  15. package/lib/commonjs/models/CourierAuthenticationListener.js.map +1 -1
  16. package/lib/commonjs/models/CourierInboxListener.js +2 -2
  17. package/lib/commonjs/models/CourierInboxListener.js.map +1 -1
  18. package/lib/module/Broadcaster.js +3 -1
  19. package/lib/module/Broadcaster.js.map +1 -1
  20. package/lib/module/index.js +52 -50
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/module/models/CourierAuthenticationListener.js +2 -2
  23. package/lib/module/models/CourierAuthenticationListener.js.map +1 -1
  24. package/lib/module/models/CourierInboxListener.js +2 -2
  25. package/lib/module/models/CourierInboxListener.js.map +1 -1
  26. package/lib/typescript/src/Broadcaster.d.ts +1 -1
  27. package/lib/typescript/src/Broadcaster.d.ts.map +1 -1
  28. package/lib/typescript/src/index.d.ts +21 -20
  29. package/lib/typescript/src/index.d.ts.map +1 -1
  30. package/lib/typescript/src/models/CourierAuthenticationListener.d.ts +1 -1
  31. package/lib/typescript/src/models/CourierAuthenticationListener.d.ts.map +1 -1
  32. package/lib/typescript/src/models/CourierInboxListener.d.ts +1 -1
  33. package/lib/typescript/src/models/CourierInboxListener.d.ts.map +1 -1
  34. package/package.json +1 -1
  35. package/src/Broadcaster.tsx +4 -1
  36. package/src/index.tsx +68 -62
  37. package/src/models/CourierAuthenticationListener.tsx +2 -2
  38. package/src/models/CourierInboxListener.tsx +2 -2
@@ -8,68 +8,90 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
8
8
  private var inboxListeners: [String: CourierInboxListener] = [:]
9
9
 
10
10
  override func stopObserving() {
11
- removeAllAuthenticationListeners()
12
- removeAllInboxListeners()
11
+ removeAuthListeners()
12
+ removeInboxListeners()
13
+ }
14
+
15
+ @objc(attachEmitter:withResolver:withRejecter:)
16
+ func attachEmitter(emitterId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
17
+
18
+ nativeEmitters.append(emitterId)
19
+ resolve(emitterId)
20
+
13
21
  }
14
22
 
15
23
  // MARK: Client
16
-
17
- @objc func getClient() -> String? {
24
+
25
+ @objc(getClient:withRejecter:)
26
+ func getClient(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
18
27
 
19
- guard let options = Courier.shared.client?.options else {
20
- return nil
21
- }
28
+ Task {
22
29
 
23
- do {
24
-
25
- let dictionary = [
26
- "jwt": options.jwt as Any,
27
- "clientKey": options.clientKey as Any,
28
- "userId": options.userId as Any,
29
- "connectionId": options.connectionId as Any,
30
- "tenantId": options.tenantId as Any,
31
- "showLogs": options.showLogs as Any
32
- ]
33
- .compactMapValues { $0 }
34
-
35
- let jsonData = try JSONSerialization.data(
36
- withJSONObject: dictionary,
37
- options: .prettyPrinted
38
- )
39
-
40
- guard let jsonString = String(data: jsonData, encoding: .utf8) else {
41
- return nil
30
+ guard let options = await Courier.shared.client?.options else {
31
+ resolve(nil)
32
+ return
42
33
  }
43
-
44
- return jsonString
45
-
46
- } catch {
47
- return nil
34
+
35
+ do {
36
+
37
+ let dictionary = [
38
+ "jwt": options.jwt as Any,
39
+ "clientKey": options.clientKey as Any,
40
+ "userId": options.userId as Any,
41
+ "connectionId": options.connectionId as Any,
42
+ "tenantId": options.tenantId as Any,
43
+ "showLogs": options.showLogs as Any
44
+ ]
45
+ .compactMapValues { $0 }
46
+
47
+ let jsonData = try JSONSerialization.data(
48
+ withJSONObject: dictionary,
49
+ options: .prettyPrinted
50
+ )
51
+
52
+ guard let jsonString = String(data: jsonData, encoding: .utf8) else {
53
+ resolve(nil)
54
+ return
55
+ }
56
+
57
+ resolve(jsonString)
58
+
59
+ } catch {
60
+ resolve(nil)
61
+ }
62
+
48
63
  }
49
64
 
50
65
  }
51
66
 
52
67
  // MARK: Authentication
53
68
 
54
- @objc func getUserId() -> String? {
55
- return Courier.shared.userId
69
+ @objc(getUserId:withRejecter:)
70
+ func getUserId(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
71
+ Task {
72
+ let userId = await Courier.shared.userId
73
+ resolve(userId)
74
+ }
56
75
  }
57
76
 
58
- @objc func getTenantId() -> String? {
59
- return Courier.shared.tenantId
77
+ @objc(getTenantId:withRejecter:)
78
+ func getTenantId(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
79
+ Task {
80
+ let tenantId = await Courier.shared.tenantId
81
+ resolve(tenantId)
82
+ }
60
83
  }
61
84
 
62
- @objc func getIsUserSignedIn() -> String {
63
- return String(Courier.shared.isUserSignedIn)
85
+ @objc(getIsUserSignedIn:withRejecter:)
86
+ func getIsUserSignedIn(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
87
+ Task {
88
+ let isUserSignedIn = await String(Courier.shared.isUserSignedIn)
89
+ resolve(isUserSignedIn)
90
+ }
64
91
  }
65
92
 
66
93
  @objc(signIn:withClientKey:withUserId:withTenantId:withShowLogs:withResolver:withRejecter:)
67
- func signIn(accessToken: NSString, clientKey: NSString?, userId: NSString, tenantId: NSString?, showLogs: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
68
-
69
- let userId = userId as String
70
- let tenantId = tenantId as? String
71
- let accessToken = accessToken as String
72
- let clientKey = clientKey as? String
94
+ func signIn(accessToken: String, clientKey: String?, userId: String, tenantId: String?, showLogs: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
73
95
 
74
96
  Task {
75
97
 
@@ -89,94 +111,91 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
89
111
 
90
112
  @objc(signOut:withRejecter:)
91
113
  func signOut(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
92
-
93
114
  Task {
94
115
  await Courier.shared.signOut()
95
116
  resolve(nil)
96
117
  }
97
-
98
118
  }
99
119
 
100
- @objc(addAuthenticationListener:)
101
- func addAuthenticationListener(listenerId: String) -> String {
120
+ @objc(addAuthenticationListener:withResolver:withRejecter:)
121
+ func addAuthenticationListener(listenerId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
102
122
 
103
- nativeEmitters.append(listenerId)
104
-
105
- let listener = Courier.shared.addAuthenticationListener { [weak self] userId in
106
- self?.broadcast(
107
- name: listenerId,
108
- body: userId
109
- )
123
+ Task {
124
+
125
+ let listener = await Courier.shared.addAuthenticationListener { [weak self] userId in
126
+ self?.broadcast(
127
+ name: listenerId,
128
+ body: userId
129
+ )
130
+ }
131
+
132
+ authenticationListeners[listenerId] = listener
133
+
134
+ resolve(listenerId)
135
+
110
136
  }
111
137
 
112
- authenticationListeners[listenerId] = listener
113
-
114
- return listenerId
115
-
116
138
  }
117
139
 
118
- @objc(removeAuthenticationListener:)
119
- func removeAuthenticationListener(listenerId: NSString) -> String {
120
-
121
- let id = listenerId as String
122
-
123
- let listener = authenticationListeners[id]
140
+ @objc(removeAuthenticationListener:withResolver:withRejecter:)
141
+ func removeAuthenticationListener(listenerId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
124
142
 
125
- // Disable the listener
126
- listener?.remove()
127
-
128
- // Remove the id from the map
129
- authenticationListeners.removeValue(forKey: id)
130
-
131
- return id
143
+ Task {
144
+
145
+ let listener = authenticationListeners[listenerId]
146
+
147
+ // Disable the listener
148
+ listener?.remove()
149
+
150
+ // Remove the id from the map
151
+ authenticationListeners.removeValue(forKey: listenerId)
152
+
153
+ resolve(listenerId)
154
+
155
+ }
132
156
 
133
157
  }
134
158
 
135
- @discardableResult @objc func removeAllAuthenticationListeners() -> String? {
136
-
159
+ @objc(removeAllAuthenticationListeners:withRejecter:)
160
+ func removeAllAuthenticationListeners(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
161
+ Task {
162
+ removeAuthListeners()
163
+ resolve(nil)
164
+ }
165
+ }
166
+
167
+ private func removeAuthListeners() {
168
+
137
169
  for value in authenticationListeners.values {
138
170
  value.remove()
139
171
  }
140
-
172
+
141
173
  authenticationListeners.removeAll()
142
-
143
- return nil
144
-
174
+
145
175
  }
146
176
 
147
177
  // MARK: Push
148
178
 
149
179
  @objc(getAllTokens:withRejecter:)
150
180
  func getAllTokens(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
151
-
152
181
  Task {
153
-
154
182
  let tokens = await Courier.shared.tokens
155
183
  resolve(tokens)
156
-
157
184
  }
158
-
159
185
  }
160
186
 
161
187
  @objc(getToken:withResolver:withRejecter:)
162
- func getToken(provider: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
163
-
164
- let provider = provider as String
188
+ func getToken(provider: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
165
189
 
166
190
  Task {
167
-
168
191
  let token = await Courier.shared.getToken(for: provider)
169
192
  resolve(token)
170
-
171
193
  }
172
194
 
173
195
  }
174
196
 
175
197
  @objc(setToken:withToken:withResolver:withRejecter:)
176
- func setToken(provider: NSString, token: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
177
-
178
- let provider = provider as String
179
- let token = token as String
198
+ func setToken(provider: String, token: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
180
199
 
181
200
  Task {
182
201
  do {
@@ -194,24 +213,29 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
194
213
 
195
214
  // MARK: Inbox
196
215
 
197
- @objc func getInboxPaginationLimit() -> String {
198
- return String(describing: Courier.shared.inboxPaginationLimit)
216
+ @objc(getInboxPaginationLimit:withRejecter:)
217
+ func getInboxPaginationLimit(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
218
+ Task {
219
+ let limit = await String(describing: Courier.shared.inboxPaginationLimit)
220
+ resolve(limit)
221
+ }
199
222
  }
200
223
 
201
- @objc(setInboxPaginationLimit:)
202
- func setInboxPaginationLimit(limit: Double) -> String {
203
- Courier.shared.inboxPaginationLimit = Int(limit)
204
- return String(describing: Courier.shared.inboxPaginationLimit)
224
+ @objc(setInboxPaginationLimit:withResolver:withRejecter:)
225
+ func setInboxPaginationLimit(limit: Double, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
226
+ Task {
227
+ await Courier.shared.setPaginationLimit(Int(limit))
228
+ let limit = await String(describing: Courier.shared.inboxPaginationLimit)
229
+ resolve(limit)
230
+ }
205
231
  }
206
232
 
207
233
  @objc(openMessage:withResolver:withRejecter:)
208
- func openMessage(messageId: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
209
-
210
- let id = messageId as String
234
+ func openMessage(messageId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
211
235
 
212
236
  Task {
213
237
  do {
214
- try await Courier.shared.openMessage(id)
238
+ try await Courier.shared.openMessage(messageId)
215
239
  resolve(nil)
216
240
  } catch {
217
241
  Rejections.sharedError(reject, error: error)
@@ -221,13 +245,11 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
221
245
  }
222
246
 
223
247
  @objc(archiveMessage:withResolver:withRejecter:)
224
- func archiveMessage(messageId: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
225
-
226
- let id = messageId as String
248
+ func archiveMessage(messageId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
227
249
 
228
250
  Task {
229
251
  do {
230
- try await Courier.shared.archiveMessage(id)
252
+ try await Courier.shared.archiveMessage(messageId)
231
253
  resolve(nil)
232
254
  } catch {
233
255
  Rejections.sharedError(reject, error: error)
@@ -237,13 +259,11 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
237
259
  }
238
260
 
239
261
  @objc(clickMessage:withResolver:withRejecter:)
240
- func clickMessage(messageId: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
241
-
242
- let id = messageId as String
262
+ func clickMessage(messageId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
243
263
 
244
264
  Task {
245
265
  do {
246
- try await Courier.shared.clickMessage(id)
266
+ try await Courier.shared.clickMessage(messageId)
247
267
  resolve(nil)
248
268
  } catch {
249
269
  Rejections.sharedError(reject, error: error)
@@ -253,13 +273,11 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
253
273
  }
254
274
 
255
275
  @objc(readMessage:withResolver:withRejecter:)
256
- func readMessage(messageId: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
257
-
258
- let id = messageId as String
276
+ func readMessage(messageId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
259
277
 
260
278
  Task {
261
279
  do {
262
- try await Courier.shared.readMessage(id)
280
+ try await Courier.shared.readMessage(messageId)
263
281
  resolve(nil)
264
282
  } catch {
265
283
  Rejections.sharedError(reject, error: error)
@@ -269,13 +287,11 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
269
287
  }
270
288
 
271
289
  @objc(unreadMessage:withResolver:withRejecter:)
272
- func unreadMessage(messageId: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
273
-
274
- let id = messageId as String
290
+ func unreadMessage(messageId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
275
291
 
276
292
  Task {
277
293
  do {
278
- try await Courier.shared.unreadMessage(id)
294
+ try await Courier.shared.unreadMessage(messageId)
279
295
  resolve(nil)
280
296
  } catch {
281
297
  Rejections.sharedError(reject, error: error)
@@ -298,182 +314,197 @@ class CourierSharedModule: CourierReactNativeEventEmitter {
298
314
 
299
315
  }
300
316
 
301
- @objc(addInboxListener:withErrorId:withUnreadCountId:withFeedId:withArchiveId:withPageAddedId:withMessageChangedId:withMessageAddedId:withMessageRemovedId:)
302
- func addInboxListener(loadingId: String, errorId: String, unreadCountId: String, feedId: String, archiveId: String, pageAddedId: String, messageChangedId: String, messageAddedId: String, messageRemovedId: String) -> String {
317
+ @objc(addInboxListener:withLoadingId:withErrorId:withUnreadCountId:withFeedId:withArchiveId:withPageAddedId:withMessageChangedId:withMessageAddedId:withMessageRemovedId:withResolver:withRejecter:)
318
+ func addInboxListener(listenerId: String, loadingId: String, errorId: String, unreadCountId: String, feedId: String, archiveId: String, pageAddedId: String, messageChangedId: String, messageAddedId: String, messageRemovedId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
303
319
 
304
- // Add the events
305
- nativeEmitters.append(contentsOf: [loadingId, errorId, unreadCountId, feedId, archiveId, pageAddedId, messageChangedId, messageAddedId, messageRemovedId])
306
-
307
- // Create the new listener
308
- let listener = Courier.shared.addInboxListener(
309
- onLoading: { [weak self] in
310
- self?.broadcast(
311
- name: loadingId,
312
- body: nil
313
- )
314
- },
315
- onError: { [weak self] error in
316
- self?.broadcast(
317
- name: errorId,
318
- body: String(describing: error)
319
- )
320
- },
321
- onUnreadCountChanged: { [weak self] unreadCount in
322
- self?.broadcast(
323
- name: unreadCountId,
324
- body: unreadCount
320
+ Task {
321
+
322
+ // Create the new listener
323
+ let listener = await Courier.shared.addInboxListener(
324
+ onLoading: { [weak self] in
325
+ self?.broadcast(
326
+ name: loadingId,
327
+ body: nil
328
+ )
329
+ },
330
+ onError: { [weak self] error in
331
+ self?.broadcast(
332
+ name: errorId,
333
+ body: String(describing: error)
334
+ )
335
+ },
336
+ onUnreadCountChanged: { [weak self] unreadCount in
337
+ self?.broadcast(
338
+ name: unreadCountId,
339
+ body: unreadCount
340
+ )
341
+ },
342
+ onFeedChanged: { [weak self] set in
343
+ do {
344
+ let json: [String: Any] = [
345
+ "messages": try set.messages.map { try $0.toJson() ?? "" },
346
+ "totalMessageCount": set.totalCount,
347
+ "canPaginate": set.canPaginate
348
+ ]
349
+ self?.broadcast(
350
+ name: feedId,
351
+ body: json
352
+ )
353
+ } catch {
354
+ Task {
355
+ await Courier.shared.client?.error(error.localizedDescription)
356
+ }
357
+ }
358
+ },
359
+ onArchiveChanged: { [weak self] set in
360
+ do {
361
+ let json: [String: Any] = [
362
+ "messages": try set.messages.map { try $0.toJson() ?? "" },
363
+ "totalMessageCount": set.totalCount,
364
+ "canPaginate": set.canPaginate
365
+ ]
366
+ self?.broadcast(
367
+ name: archiveId,
368
+ body: json
369
+ )
370
+ } catch {
371
+ Task {
372
+ await Courier.shared.client?.error(error.localizedDescription)
373
+ }
374
+ }
375
+ },
376
+ onPageAdded: { [weak self] feed, set in
377
+ do {
378
+ let json: [String: Any] = [
379
+ "feed": feed == .archived ? "archived" : "feed",
380
+ "messages": try set.messages.map { try $0.toJson() ?? "" },
381
+ "totalMessageCount": set.totalCount,
382
+ "canPaginate": set.canPaginate
383
+ ]
384
+ self?.broadcast(
385
+ name: pageAddedId,
386
+ body: json
387
+ )
388
+ } catch {
389
+ Task {
390
+ await Courier.shared.client?.error(error.localizedDescription)
391
+ }
392
+ }
393
+ },
394
+ onMessageChanged: { [weak self] feed, index, message in
395
+ do {
396
+ let json: [String: Any] = [
397
+ "feed": feed == .archived ? "archived" : "feed",
398
+ "index": index,
399
+ "message": try message.toJson() ?? "",
400
+ ]
401
+ self?.broadcast(
402
+ name: messageChangedId,
403
+ body: json
404
+ )
405
+ } catch {
406
+ Task {
407
+ await Courier.shared.client?.error(error.localizedDescription)
408
+ }
409
+ }
410
+ },
411
+ onMessageAdded: { [weak self] feed, index, message in
412
+ do {
413
+ let json: [String: Any] = [
414
+ "feed": feed == .archived ? "archived" : "feed",
415
+ "index": index,
416
+ "message": try message.toJson() ?? "",
417
+ ]
418
+ self?.broadcast(
419
+ name: messageAddedId,
420
+ body: json
421
+ )
422
+ } catch {
423
+ Task {
424
+ await Courier.shared.client?.error(error.localizedDescription)
425
+ }
426
+ }
427
+ },
428
+ onMessageRemoved: { [weak self] feed, index, message in
429
+ do {
430
+ let json: [String: Any] = [
431
+ "feed": feed == .archived ? "archived" : "feed",
432
+ "index": index,
433
+ "message": try message.toJson() ?? "",
434
+ ]
435
+ self?.broadcast(
436
+ name: messageRemovedId,
437
+ body: json
438
+ )
439
+ } catch {
440
+ Task {
441
+ await Courier.shared.client?.error(error.localizedDescription)
442
+ }
443
+ }
444
+ }
325
445
  )
326
- },
327
- onFeedChanged: { [weak self] set in
328
- do {
329
- let json: [String: Any] = [
330
- "messages": try set.messages.map { try $0.toJson() ?? "" },
331
- "totalMessageCount": set.totalCount,
332
- "canPaginate": set.canPaginate
333
- ]
334
- self?.broadcast(
335
- name: feedId,
336
- body: json
337
- )
338
- } catch {
339
- Courier.shared.client?.error(error.localizedDescription)
340
- }
341
- },
342
- onArchiveChanged: { [weak self] set in
343
- do {
344
- let json: [String: Any] = [
345
- "messages": try set.messages.map { try $0.toJson() ?? "" },
346
- "totalMessageCount": set.totalCount,
347
- "canPaginate": set.canPaginate
348
- ]
349
- self?.broadcast(
350
- name: archiveId,
351
- body: json
352
- )
353
- } catch {
354
- Courier.shared.client?.error(error.localizedDescription)
355
- }
356
- },
357
- onPageAdded: { [weak self] feed, set in
358
- do {
359
- let json: [String: Any] = [
360
- "feed": feed == .archived ? "archived" : "feed",
361
- "messages": try set.messages.map { try $0.toJson() ?? "" },
362
- "totalMessageCount": set.totalCount,
363
- "canPaginate": set.canPaginate
364
- ]
365
- self?.broadcast(
366
- name: pageAddedId,
367
- body: json
368
- )
369
- } catch {
370
- Courier.shared.client?.error(error.localizedDescription)
371
- }
372
- },
373
- onMessageChanged: { [weak self] feed, index, message in
374
- do {
375
- let json: [String: Any] = [
376
- "feed": feed == .archived ? "archived" : "feed",
377
- "index": index,
378
- "message": try message.toJson() ?? "",
379
- ]
380
- self?.broadcast(
381
- name: messageChangedId,
382
- body: json
383
- )
384
- } catch {
385
- Courier.shared.client?.error(error.localizedDescription)
386
- }
387
- },
388
- onMessageAdded: { [weak self] feed, index, message in
389
- do {
390
- let json: [String: Any] = [
391
- "feed": feed == .archived ? "archived" : "feed",
392
- "index": index,
393
- "message": try message.toJson() ?? "",
394
- ]
395
- self?.broadcast(
396
- name: messageAddedId,
397
- body: json
398
- )
399
- } catch {
400
- Courier.shared.client?.error(error.localizedDescription)
401
- }
402
- },
403
- onMessageRemoved: { [weak self] feed, index, message in
404
- do {
405
- let json: [String: Any] = [
406
- "feed": feed == .archived ? "archived" : "feed",
407
- "index": index,
408
- "message": try message.toJson() ?? "",
409
- ]
410
- self?.broadcast(
411
- name: messageRemovedId,
412
- body: json
413
- )
414
- } catch {
415
- Courier.shared.client?.error(error.localizedDescription)
416
- }
417
- }
418
- )
419
-
420
- let id = UUID().uuidString
421
- inboxListeners[id] = listener
422
-
423
- return id
446
+
447
+ inboxListeners[listenerId] = listener
448
+
449
+ resolve(listenerId)
450
+
451
+ }
424
452
 
425
453
  }
426
454
 
427
- @objc(removeInboxListener:)
428
- func removeInboxListener(listenerId: NSString) -> String {
429
-
430
- let id = listenerId as String
455
+ @objc(removeInboxListener:withResolver:withRejecter:)
456
+ func removeInboxListener(listenerId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
431
457
 
432
- let listener = inboxListeners[id]
433
-
434
- // Disable the listener
435
- listener?.remove()
436
-
437
- // Remove the id from the map
438
- inboxListeners.removeValue(forKey: id)
439
-
440
- return id
458
+ Task {
459
+
460
+ let listener = inboxListeners[listenerId]
461
+
462
+ // Disable the listener
463
+ listener?.remove()
464
+
465
+ // Remove the id from the map
466
+ inboxListeners.removeValue(forKey: listenerId)
467
+
468
+ resolve(listenerId)
469
+
470
+ }
441
471
 
442
472
  }
443
473
 
444
- @discardableResult @objc func removeAllInboxListeners() -> String? {
445
-
474
+ @objc(removeAllInboxListeners:withRejecter:)
475
+ func removeAllInboxListeners(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
476
+ Task {
477
+ removeInboxListeners()
478
+ resolve(nil)
479
+ }
480
+ }
481
+
482
+ private func removeInboxListeners() {
446
483
  for value in inboxListeners.values {
447
484
  value.remove()
448
485
  }
449
-
450
486
  inboxListeners.removeAll()
451
-
452
- return nil
453
-
454
487
  }
455
488
 
456
489
  @objc(refreshInbox: withRejecter:)
457
490
  func refreshInbox(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
458
-
459
491
  Task {
460
492
  await Courier.shared.refreshInbox()
461
493
  resolve(nil)
462
494
  }
463
-
464
495
  }
465
496
 
466
497
  @objc(fetchNextPageOfMessages:withResolver:withRejecter:)
467
- func fetchNextPageOfMessages(inboxMessageFeed: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
468
- Task {
469
- do {
470
- let value: InboxMessageFeed = inboxMessageFeed == "archived" ? .feed : .archived
471
- let messages = try await Courier.shared.fetchNextInboxPage(value)
472
- resolve(try messages.map { try $0.toJson() ?? "" })
473
- } catch {
474
- Rejections.sharedError(reject, error: error)
498
+ func fetchNextPageOfMessages(inboxMessageFeed: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
499
+ Task {
500
+ do {
501
+ let value: InboxMessageFeed = inboxMessageFeed == "archived" ? .feed : .archived
502
+ let messages = try await Courier.shared.fetchNextInboxPage(value)
503
+ resolve(try messages.map { try $0.toJson() ?? "" })
504
+ } catch {
505
+ Rejections.sharedError(reject, error: error)
506
+ }
475
507
  }
476
- }
477
508
  }
478
509
 
479
510
  override func supportedEvents() -> [String]! {