@trycourier/courier-react-native 2.3.2 → 2.4.0

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 (33) hide show
  1. package/android/src/main/java/com/courierreactnative/CourierEvents.kt +0 -10
  2. package/android/src/main/java/com/courierreactnative/CourierReactNativeModule.kt +34 -41
  3. package/ios/CourierReactNativeModule.m +4 -2
  4. package/ios/CourierReactNativeModule.swift +71 -69
  5. package/lib/commonjs/index.js +82 -80
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/commonjs/models/CourierAuthenticationListener.js +9 -5
  8. package/lib/commonjs/models/CourierAuthenticationListener.js.map +1 -1
  9. package/lib/commonjs/models/CourierInboxListener.js +3 -6
  10. package/lib/commonjs/models/CourierInboxListener.js.map +1 -1
  11. package/lib/commonjs/utils.js +45 -0
  12. package/lib/commonjs/utils.js.map +1 -0
  13. package/lib/module/index.js +84 -81
  14. package/lib/module/index.js.map +1 -1
  15. package/lib/module/models/CourierAuthenticationListener.js +9 -5
  16. package/lib/module/models/CourierAuthenticationListener.js.map +1 -1
  17. package/lib/module/models/CourierInboxListener.js +3 -6
  18. package/lib/module/models/CourierInboxListener.js.map +1 -1
  19. package/lib/module/utils.js +37 -0
  20. package/lib/module/utils.js.map +1 -0
  21. package/lib/typescript/index.d.ts +1 -6
  22. package/lib/typescript/index.d.ts.map +1 -1
  23. package/lib/typescript/models/CourierAuthenticationListener.d.ts +2 -1
  24. package/lib/typescript/models/CourierAuthenticationListener.d.ts.map +1 -1
  25. package/lib/typescript/models/CourierInboxListener.d.ts +4 -4
  26. package/lib/typescript/models/CourierInboxListener.d.ts.map +1 -1
  27. package/lib/typescript/utils.d.ts +21 -0
  28. package/lib/typescript/utils.d.ts.map +1 -0
  29. package/package.json +1 -1
  30. package/src/index.tsx +100 -99
  31. package/src/models/CourierAuthenticationListener.tsx +8 -4
  32. package/src/models/CourierInboxListener.tsx +5 -10
  33. package/src/utils.tsx +48 -0
@@ -10,19 +10,9 @@ internal class CourierEvents {
10
10
  const val DEBUG_LOG = "courierDebugEvent"
11
11
  }
12
12
 
13
- object Auth {
14
- const val USER_CHANGED = "courierAuthUserChanged"
15
- }
16
-
17
13
  object Push {
18
14
  const val CLICKED_EVENT = "pushNotificationClicked"
19
15
  const val DELIVERED_EVENT = "pushNotificationDelivered"
20
16
  }
21
17
 
22
- object Inbox {
23
- const val INITIAL_LOADING = "inboxInitialLoad"
24
- const val ERROR = "inboxError"
25
- const val MESSAGES_CHANGED = "inboxMessagesChanged"
26
- }
27
-
28
18
  }
@@ -18,13 +18,9 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
18
18
  override fun getName() = "CourierReactNativeModule"
19
19
  private val reactActivity: ReactActivity? get() = currentActivity as? ReactActivity
20
20
 
21
- // Auth Listeners
22
- private var authListener: CourierAuthenticationListener? = null
23
- private var authListeners: MutableList<String> = mutableListOf()
24
-
25
- // Inbox Listeners
26
- private var inboxListener: CourierInboxListener? = null
27
- private var inboxListeners: MutableList<String> = mutableListOf()
21
+ // Listeners
22
+ private var authListeners = mutableMapOf<String, CourierAuthenticationListener>()
23
+ private var inboxListeners = mutableMapOf<String, CourierInboxListener>()
28
24
 
29
25
  init {
30
26
 
@@ -135,19 +131,21 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
135
131
  }
136
132
 
137
133
  @ReactMethod(isBlockingSynchronousMethod = true)
138
- fun addAuthenticationListener(): String {
134
+ fun addAuthenticationListener(authId: String): String {
139
135
 
140
- authListener?.remove()
136
+ // Create the listener
137
+ val listener = Courier.shared.addAuthenticationListener { userId ->
141
138
 
142
- authListener = Courier.shared.addAuthenticationListener { userId ->
143
139
  reactApplicationContext.sendEvent(
144
- eventName = CourierEvents.Auth.USER_CHANGED,
140
+ eventName = authId,
145
141
  value = userId
146
142
  )
143
+
147
144
  }
148
145
 
146
+ // Add the listener to the map
149
147
  val id = UUID.randomUUID().toString()
150
- authListeners.add(id)
148
+ authListeners[id] = listener
151
149
 
152
150
  return id
153
151
 
@@ -156,17 +154,14 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
156
154
  @ReactMethod(isBlockingSynchronousMethod = true)
157
155
  fun removeAuthenticationListener(listenerId: String): String {
158
156
 
159
- // Remove the item from the array
160
- val index = authListeners.indexOf(listenerId)
161
- if (index != -1) {
162
- authListeners.removeAt(index)
163
- }
157
+ // Get the listener
158
+ val listener = authListeners[listenerId]
164
159
 
165
- // Check the length
166
- if (authListeners.isEmpty()) {
167
- authListener?.remove()
168
- authListener = null
169
- }
160
+ // Remove the listener
161
+ listener?.remove()
162
+
163
+ // Remove the listener
164
+ authListeners.remove(listenerId)
170
165
 
171
166
  return listenerId
172
167
 
@@ -223,23 +218,24 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
223
218
  }
224
219
 
225
220
  @ReactMethod(isBlockingSynchronousMethod = true)
226
- fun addInboxListener(): String {
227
-
228
- // Remove the old listener
229
- inboxListener?.remove()
221
+ fun addInboxListener(loadingId: String, errorId: String, messagesId: String): String {
230
222
 
231
- inboxListener = Courier.shared.addInboxListener(
223
+ val listener = Courier.shared.addInboxListener(
232
224
  onInitialLoad = {
225
+
233
226
  reactApplicationContext.sendEvent(
234
- eventName = CourierEvents.Inbox.INITIAL_LOADING,
227
+ eventName = loadingId,
235
228
  value = null
236
229
  )
230
+
237
231
  },
238
232
  onError = { e ->
233
+
239
234
  reactApplicationContext.sendEvent(
240
- eventName = CourierEvents.Inbox.ERROR,
235
+ eventName = errorId,
241
236
  value = e.message ?: "Courier Inbox Error"
242
237
  )
238
+
243
239
  },
244
240
  onMessagesChanged = { messages: List<InboxMessage>, unreadMessageCount: Int, totalMessageCount: Int, canPaginate: Boolean ->
245
241
 
@@ -250,7 +246,7 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
250
246
  json.putBoolean("canPaginate", canPaginate)
251
247
 
252
248
  reactApplicationContext.sendEvent(
253
- eventName = CourierEvents.Inbox.MESSAGES_CHANGED,
249
+ eventName = messagesId,
254
250
  value = json
255
251
  )
256
252
 
@@ -259,7 +255,7 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
259
255
 
260
256
  // Add listener
261
257
  val id = UUID.randomUUID().toString()
262
- inboxListeners.add(id)
258
+ inboxListeners[id] = listener
263
259
 
264
260
  return id
265
261
 
@@ -268,17 +264,14 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
268
264
  @ReactMethod(isBlockingSynchronousMethod = true)
269
265
  fun removeInboxListener(listenerId: String): String {
270
266
 
271
- // Remove the item from the array
272
- val index = inboxListeners.indexOf(listenerId)
273
- if (index != -1) {
274
- inboxListeners.removeAt(index)
275
- }
267
+ // Get the listener
268
+ val listener = inboxListeners[listenerId]
276
269
 
277
- // Check the length
278
- if (inboxListeners.isEmpty()) {
279
- inboxListener?.remove()
280
- inboxListener = null
281
- }
270
+ // Remove the listener
271
+ listener?.remove()
272
+
273
+ // Remove the listener
274
+ inboxListeners.remove(listenerId)
282
275
 
283
276
  return listenerId
284
277
 
@@ -28,7 +28,7 @@ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
28
28
  )
29
29
 
30
30
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
31
- addAuthenticationListener
31
+ addAuthenticationListener: (NSString*)authId
32
32
  )
33
33
 
34
34
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
@@ -76,7 +76,9 @@ RCT_EXTERN_METHOD(
76
76
  )
77
77
 
78
78
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
79
- addInboxListener
79
+ addInboxListener: (NSString*)loadingId
80
+ withErrorId: (NSString*)errorId
81
+ withMessagesId: (NSString*)messagesId
80
82
  )
81
83
 
82
84
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
@@ -9,28 +9,14 @@ class CourierReactNativeModule: RCTEventEmitter {
9
9
  internal static let DEBUG_LOG = "courierDebugEvent"
10
10
  }
11
11
 
12
- class AuthEvents {
13
- internal static let USER_CHANGED = "courierAuthUserChanged"
14
- }
15
-
16
12
  class PushEvents {
17
13
  internal static let CLICKED_EVENT = "pushNotificationClicked"
18
14
  internal static let DELIVERED_EVENT = "pushNotificationDelivered"
19
15
  }
20
16
 
21
- class InboxEvents {
22
- internal static let INITIAL_LOADING = "inboxInitialLoad"
23
- internal static let ERROR = "inboxError"
24
- internal static let MESSAGES_CHANGED = "inboxMessagesChanged"
25
- }
26
-
27
- // Auth Listeners
28
- private var authListener: CourierAuthenticationListener? = nil
29
- private var authListeners: [String] = []
30
-
31
- // Inbox Listeners
32
- private var inboxListener: CourierInboxListener? = nil
33
- private var inboxListeners: [String] = []
17
+ // Listeners
18
+ private var authListeners: [String: CourierAuthenticationListenerWrapper] = [:]
19
+ private var inboxListeners: [String: CourierInboxListenerWrapper] = [:]
34
20
 
35
21
  private var hasDebuggingListeners = false
36
22
 
@@ -105,8 +91,8 @@ class CourierReactNativeModule: RCTEventEmitter {
105
91
  }
106
92
 
107
93
  do {
108
- sendEvent(
109
- withName: name,
94
+ broadcastEvent(
95
+ name: name,
110
96
  body: try message.toString()
111
97
  )
112
98
  } catch {
@@ -114,6 +100,19 @@ class CourierReactNativeModule: RCTEventEmitter {
114
100
  }
115
101
 
116
102
  }
103
+
104
+ private func broadcastEvent(name: String, body: Any?) {
105
+
106
+ if (!supportedEvents().contains(name)) {
107
+ return
108
+ }
109
+
110
+ sendEvent(
111
+ withName: name,
112
+ body: body
113
+ )
114
+
115
+ }
117
116
 
118
117
  @objc private func pushNotificationClicked(notification: Notification) {
119
118
 
@@ -196,24 +195,22 @@ class CourierReactNativeModule: RCTEventEmitter {
196
195
  return Courier.shared.userId
197
196
  }
198
197
 
199
- @objc func addAuthenticationListener() -> String {
198
+ @objc(addAuthenticationListener:)
199
+ func addAuthenticationListener(authId: String) -> String {
200
200
 
201
- // Remove the listener
202
- authListener?.remove()
203
-
204
- // Set the new listener
205
- authListener = Courier.shared.addAuthenticationListener { [weak self] userId in
206
-
207
- self?.sendEvent(
208
- withName: CourierReactNativeModule.AuthEvents.USER_CHANGED,
201
+ let listener = Courier.shared.addAuthenticationListener { [weak self] userId in
202
+ self?.broadcastEvent(
203
+ name: authId,
209
204
  body: userId
210
205
  )
211
-
212
206
  }
213
207
 
214
- // Add the listener to the arrau
215
208
  let id = UUID().uuidString
216
- authListeners.append(id)
209
+
210
+ authListeners[id] = CourierAuthenticationListenerWrapper(
211
+ authId: authId,
212
+ listener: listener
213
+ )
217
214
 
218
215
  return id
219
216
 
@@ -224,16 +221,13 @@ class CourierReactNativeModule: RCTEventEmitter {
224
221
 
225
222
  let id = listenerId as String
226
223
 
227
- // Remove the item from the array
228
- if let index = authListeners.firstIndex(of: id) {
229
- authListeners.remove(at: index)
230
- }
224
+ let wrapper = authListeners[id]
231
225
 
232
- // Check the length
233
- if (authListeners.isEmpty) {
234
- authListener?.remove()
235
- authListener = nil
236
- }
226
+ // Disable the listener
227
+ wrapper?.listener.remove()
228
+
229
+ // Remove the id from the map
230
+ authListeners.removeValue(forKey: id)
237
231
 
238
232
  return id
239
233
 
@@ -318,25 +312,23 @@ class CourierReactNativeModule: RCTEventEmitter {
318
312
 
319
313
  }
320
314
 
321
- @objc func addInboxListener() -> String {
322
-
323
- // Remove the old listener
324
- inboxListener?.remove()
315
+ @objc(addInboxListener: withErrorId: withMessagesId:)
316
+ func addInboxListener(loadingId: String, errorId: String, messagesId: String) -> String {
325
317
 
326
318
  // Create the new listener
327
- inboxListener = Courier.shared.addInboxListener(
319
+ let listener = Courier.shared.addInboxListener(
328
320
  onInitialLoad: { [weak self] in
329
321
 
330
- self?.sendEvent(
331
- withName: CourierReactNativeModule.InboxEvents.INITIAL_LOADING,
322
+ self?.broadcastEvent(
323
+ name: loadingId,
332
324
  body: nil
333
325
  )
334
326
 
335
327
  },
336
328
  onError: { [weak self] error in
337
329
 
338
- self?.sendEvent(
339
- withName: CourierReactNativeModule.InboxEvents.ERROR,
330
+ self?.broadcastEvent(
331
+ name: errorId,
340
332
  body: String(describing: error)
341
333
  )
342
334
 
@@ -350,17 +342,23 @@ class CourierReactNativeModule: RCTEventEmitter {
350
342
  "canPaginate": canPaginate
351
343
  ]
352
344
 
353
- self?.sendEvent(
354
- withName: CourierReactNativeModule.InboxEvents.MESSAGES_CHANGED,
345
+ self?.broadcastEvent(
346
+ name: messagesId,
355
347
  body: json
356
348
  )
357
349
 
358
350
  }
359
351
  )
360
352
 
361
- // Track listener id
353
+ let wrapper = CourierInboxListenerWrapper(
354
+ loadingId: loadingId,
355
+ errorId: errorId,
356
+ messagesId: messagesId,
357
+ listener: listener
358
+ )
359
+
362
360
  let id = UUID().uuidString
363
- inboxListeners.append(id)
361
+ inboxListeners[id] = wrapper
364
362
 
365
363
  return id
366
364
 
@@ -371,16 +369,13 @@ class CourierReactNativeModule: RCTEventEmitter {
371
369
 
372
370
  let id = listenerId as String
373
371
 
374
- // Remove the item from the array
375
- if let index = inboxListeners.firstIndex(of: id) {
376
- inboxListeners.remove(at: index)
377
- }
372
+ let wrapper = inboxListeners[id]
378
373
 
379
- // Check the length
380
- if (inboxListeners.isEmpty) {
381
- inboxListener?.remove()
382
- inboxListener = nil
383
- }
374
+ // Disable the listener
375
+ wrapper?.listener.remove()
376
+
377
+ // Remove the id from the map
378
+ inboxListeners.removeValue(forKey: id)
384
379
 
385
380
  return id
386
381
 
@@ -450,7 +445,6 @@ class CourierReactNativeModule: RCTEventEmitter {
450
445
  @objc(putUserPreferencesTopic: withStatus: withHasCustomRouting: withCustomRouting: withResolver: withRejecter:)
451
446
  func putUserPreferencesTopic(topicId: NSString, status: NSString, hasCustomRouting: Bool, customRouting: [NSString], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
452
447
 
453
-
454
448
  Courier.shared.putUserPreferencesTopic(
455
449
  topicId: topicId as String,
456
450
  status: CourierUserPreferencesStatus(rawValue: status as String) ?? .unknown,
@@ -467,15 +461,23 @@ class CourierReactNativeModule: RCTEventEmitter {
467
461
  }
468
462
 
469
463
  override func supportedEvents() -> [String]! {
470
- return [
464
+
465
+ // Built in events
466
+ var events = [
471
467
  CourierReactNativeModule.LogEvents.DEBUG_LOG,
472
- CourierReactNativeModule.AuthEvents.USER_CHANGED,
473
468
  CourierReactNativeModule.PushEvents.CLICKED_EVENT,
474
- CourierReactNativeModule.PushEvents.DELIVERED_EVENT,
475
- CourierReactNativeModule.InboxEvents.INITIAL_LOADING,
476
- CourierReactNativeModule.InboxEvents.ERROR,
477
- CourierReactNativeModule.InboxEvents.MESSAGES_CHANGED
469
+ CourierReactNativeModule.PushEvents.DELIVERED_EVENT
478
470
  ]
471
+
472
+ // Add the custom events
473
+ let inboxIds = inboxListeners.flatMap { [$0.value.loadingId, $0.value.errorId, $0.value.messagesId] }
474
+ events.append(contentsOf: inboxIds)
475
+
476
+ let authIds = authListeners.flatMap { [$0.value.authId] }
477
+ events.append(contentsOf: authIds)
478
+
479
+ return events
480
+
479
481
  }
480
482
 
481
483
  @objc override static func requiresMainQueueSetup() -> Bool {
@@ -92,6 +92,7 @@ var _reactNative = require("react-native");
92
92
  var _CourierInboxListener = require("./models/CourierInboxListener");
93
93
  var _CourierPushListener = require("./models/CourierPushListener");
94
94
  var _CourierAuthenticationListener = require("./models/CourierAuthenticationListener");
95
+ var _utils = require("./utils");
95
96
  var _CourierInboxView = require("./views/CourierInboxView");
96
97
  var _CourierUserPreferencesChannel = require("./models/CourierUserPreferencesChannel");
97
98
  var _CourierUserPreferencesStatus = require("./models/CourierUserPreferencesStatus");
@@ -112,18 +113,14 @@ const CourierReactNativeModules = _reactNative.NativeModules.CourierReactNativeM
112
113
  });
113
114
  const CourierEventEmitter = new _reactNative.NativeEventEmitter(_reactNative.NativeModules.CourierReactNativeModule);
114
115
  class Courier {
115
- PUSH_NOTIFICATION_CLICKED = 'pushNotificationClicked';
116
- PUSH_NOTIFICATION_DELIVERED = 'pushNotificationDelivered';
117
116
  _isDebugging = false;
117
+ authListeners = new Map();
118
118
  inboxListeners = new Map();
119
119
  constructor() {
120
120
  // Sets the initial SDK values
121
121
  // Defaults to React Native level debugging
122
122
  // and will show all foreground notification styles in iOS
123
123
  this.setDefaults();
124
-
125
- // Register for inbox listener changes
126
- this.registerInboxListenerEvents();
127
124
  }
128
125
 
129
126
  // Returns the public shared instance
@@ -152,7 +149,7 @@ class Courier {
152
149
  // Set a new listener
153
150
  // listener needs to be registered first to catch the event
154
151
  if (isDebugging) {
155
- this.debugListener = CourierEventEmitter.addListener('courierDebugEvent', event => {
152
+ this.debugListener = CourierEventEmitter.addListener(_utils.Events.Log.DEBUG_LOG, event => {
156
153
  console.log('\x1b[36m%s\x1b[0m', 'COURIER', event);
157
154
  });
158
155
  }
@@ -168,30 +165,6 @@ class Courier {
168
165
  return this._isDebugging;
169
166
  }
170
167
 
171
- /**
172
- * Register inbox listener events
173
- */
174
- registerInboxListenerEvents() {
175
- CourierEventEmitter.addListener('inboxInitialLoad', () => {
176
- this.inboxListeners.forEach((value, _) => {
177
- var _value$onInitialLoad;
178
- (_value$onInitialLoad = value.onInitialLoad) === null || _value$onInitialLoad === void 0 ? void 0 : _value$onInitialLoad.call(value);
179
- });
180
- });
181
- CourierEventEmitter.addListener('inboxError', event => {
182
- this.inboxListeners.forEach((value, _) => {
183
- var _value$onError;
184
- (_value$onError = value.onError) === null || _value$onError === void 0 ? void 0 : _value$onError.call(value, event);
185
- });
186
- });
187
- CourierEventEmitter.addListener('inboxMessagesChanged', event => {
188
- this.inboxListeners.forEach((value, _) => {
189
- var _value$onMessagesChan;
190
- (_value$onMessagesChan = value.onMessagesChanged) === null || _value$onMessagesChan === void 0 ? void 0 : _value$onMessagesChan.call(value, event.messages, event.unreadMessageCount, event.totalMessageCount, event.canPaginate);
191
- });
192
- });
193
- }
194
-
195
168
  /**
196
169
  * Sets the notification presentation options for iOS
197
170
  */
@@ -246,45 +219,23 @@ class Courier {
246
219
  */
247
220
  addPushNotificationListener(props) {
248
221
  const pushListener = new _CourierPushListener.CourierPushListener();
249
- if (_reactNative.Platform.OS === 'android') {
250
- if (props.onPushNotificationClicked) {
251
- pushListener.onNotificationClickedListener = _reactNative.DeviceEventEmitter.addListener(this.PUSH_NOTIFICATION_CLICKED, event => {
252
- try {
253
- props.onPushNotificationClicked(JSON.parse(event));
254
- } catch (error) {
255
- console.log(error);
256
- }
257
- });
258
- }
259
- if (props.onPushNotificationDelivered) {
260
- pushListener.onNotificationDeliveredListener = _reactNative.DeviceEventEmitter.addListener(this.PUSH_NOTIFICATION_DELIVERED, event => {
261
- try {
262
- props.onPushNotificationDelivered(JSON.parse(event));
263
- } catch (error) {
264
- console.log(error);
265
- }
266
- });
267
- }
222
+ if (props.onPushNotificationClicked) {
223
+ pushListener.onNotificationClickedListener = _utils.Utils.addEventListener(_utils.Events.Push.CLICKED, CourierEventEmitter, event => {
224
+ try {
225
+ props.onPushNotificationClicked(JSON.parse(event));
226
+ } catch (error) {
227
+ console.log(error);
228
+ }
229
+ });
268
230
  }
269
- if (_reactNative.Platform.OS === 'ios') {
270
- if (props.onPushNotificationClicked) {
271
- pushListener.onNotificationClickedListener = CourierEventEmitter.addListener(this.PUSH_NOTIFICATION_CLICKED, event => {
272
- try {
273
- props.onPushNotificationClicked(JSON.parse(event));
274
- } catch (error) {
275
- console.log(error);
276
- }
277
- });
278
- }
279
- if (props.onPushNotificationDelivered) {
280
- pushListener.onNotificationDeliveredListener = CourierEventEmitter.addListener(this.PUSH_NOTIFICATION_DELIVERED, event => {
281
- try {
282
- props.onPushNotificationDelivered(JSON.parse(event));
283
- } catch (error) {
284
- console.log(error);
285
- }
286
- });
287
- }
231
+ if (props.onPushNotificationDelivered) {
232
+ pushListener.onNotificationDeliveredListener = _utils.Utils.addEventListener(_utils.Events.Push.DELIVERED, CourierEventEmitter, event => {
233
+ try {
234
+ props.onPushNotificationDelivered(JSON.parse(event));
235
+ } catch (error) {
236
+ console.log(error);
237
+ }
238
+ });
288
239
  }
289
240
 
290
241
  // When listener is registered
@@ -321,19 +272,41 @@ class Courier {
321
272
  * Listens to authentication changes for the current user
322
273
  */
323
274
  addAuthenticationListener(props) {
324
- const authListener = new _CourierAuthenticationListener.CourierAuthenticationListener();
325
- authListener.onUserChanged = CourierEventEmitter.addListener('courierAuthUserChanged', event => {
326
- props.onUserChanged(event);
327
- });
328
- authListener.listenerId = CourierReactNativeModules.addAuthenticationListener();
329
- return authListener;
275
+ // Event listener id
276
+ const authId = _utils.Utils.generateUUID();
277
+
278
+ // Get the id
279
+ const id = CourierReactNativeModules.addAuthenticationListener(authId);
280
+
281
+ // Create the listener
282
+ const listener = new _CourierAuthenticationListener.CourierAuthenticationListener(id);
283
+
284
+ // Add the event listener
285
+ listener.onUserChanged = _utils.Utils.addEventListener(authId, CourierEventEmitter, event => props.onUserChanged(event));
286
+
287
+ // Add listener to manager
288
+ this.authListeners.set(id, listener);
289
+ return listener;
330
290
  }
331
291
 
332
292
  /**
333
293
  * Removes an authentication listener
334
294
  */
335
295
  removeAuthenticationListener(props) {
336
- return CourierReactNativeModules.removeAuthenticationListener(props.listenerId);
296
+ // Remove the native listener
297
+ CourierReactNativeModules.removeAuthenticationListener(props.listenerId);
298
+
299
+ // Remove the listener
300
+ if (this.authListeners.has(props.listenerId)) {
301
+ var _listener$onUserChang;
302
+ // Get the listener
303
+ const listener = this.authListeners.get(props.listenerId);
304
+ listener === null || listener === void 0 || (_listener$onUserChang = listener.onUserChanged) === null || _listener$onUserChang === void 0 ? void 0 : _listener$onUserChang.remove();
305
+
306
+ // Remove the listener
307
+ this.authListeners.delete(props.listenerId);
308
+ }
309
+ return props.listenerId;
337
310
  }
338
311
 
339
312
  /**
@@ -368,14 +341,35 @@ class Courier {
368
341
  * Listens to changes for the inbox itself
369
342
  */
370
343
  addInboxListener(props) {
344
+ const listenerIds = {
345
+ loading: _utils.Utils.generateUUID(),
346
+ error: _utils.Utils.generateUUID(),
347
+ messages: _utils.Utils.generateUUID()
348
+ };
349
+
371
350
  // Set the listener id
372
- const id = CourierReactNativeModules.addInboxListener();
351
+ const id = CourierReactNativeModules.addInboxListener(listenerIds.loading, listenerIds.error, listenerIds.messages);
373
352
 
374
353
  // Create the initial listeners
375
354
  const listener = new _CourierInboxListener.CourierInboxListener(id);
376
- listener.onInitialLoad = props.onInitialLoad;
377
- listener.onError = props.onError;
378
- listener.onMessagesChanged = props.onMessagesChanged;
355
+ if (props.onInitialLoad) {
356
+ listener.onInitialLoad = _utils.Utils.addEventListener(listenerIds.loading, CourierEventEmitter, _ => {
357
+ var _props$onInitialLoad;
358
+ (_props$onInitialLoad = props.onInitialLoad) === null || _props$onInitialLoad === void 0 ? void 0 : _props$onInitialLoad.call(props);
359
+ });
360
+ }
361
+ if (props.onError) {
362
+ listener.onError = _utils.Utils.addEventListener(listenerIds.error, CourierEventEmitter, event => {
363
+ var _props$onError;
364
+ (_props$onError = props.onError) === null || _props$onError === void 0 ? void 0 : _props$onError.call(props, event);
365
+ });
366
+ }
367
+ if (props.onMessagesChanged) {
368
+ listener.onMessagesChanged = _utils.Utils.addEventListener(listenerIds.messages, CourierEventEmitter, event => {
369
+ var _props$onMessagesChan;
370
+ (_props$onMessagesChan = props.onMessagesChanged) === null || _props$onMessagesChan === void 0 ? void 0 : _props$onMessagesChan.call(props, event.messages, event.unreadMessageCount, event.totalMessageCount, event.canPaginate);
371
+ });
372
+ }
379
373
 
380
374
  // Add listener to manager
381
375
  this.inboxListeners.set(id, listener);
@@ -389,8 +383,16 @@ class Courier {
389
383
  // Call native code
390
384
  CourierReactNativeModules.removeInboxListener(props.listenerId);
391
385
 
392
- // Remove the js listener
386
+ // Remove the listener
393
387
  if (this.inboxListeners.has(props.listenerId)) {
388
+ var _listener$onInitialLo, _listener$onError, _listener$onMessagesC;
389
+ // Remove emitters
390
+ const listener = this.inboxListeners.get(props.listenerId);
391
+ listener === null || listener === void 0 || (_listener$onInitialLo = listener.onInitialLoad) === null || _listener$onInitialLo === void 0 ? void 0 : _listener$onInitialLo.remove();
392
+ listener === null || listener === void 0 || (_listener$onError = listener.onError) === null || _listener$onError === void 0 ? void 0 : _listener$onError.remove();
393
+ listener === null || listener === void 0 || (_listener$onMessagesC = listener.onMessagesChanged) === null || _listener$onMessagesC === void 0 ? void 0 : _listener$onMessagesC.remove();
394
+
395
+ // Remove the listener
394
396
  this.inboxListeners.delete(props.listenerId);
395
397
  }
396
398
  return props.listenerId;