@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
@@ -1,5 +1,6 @@
1
1
  import { NativeModule } from "react-native";
2
2
  import { NativeEventEmitter, EmitterSubscription, Platform, DeviceEventEmitter } from "react-native";
3
+ import { Modules } from "./Modules";
3
4
 
4
5
  export default class Broadcaster {
5
6
 
@@ -15,7 +16,9 @@ export default class Broadcaster {
15
16
  * @param callback Value returned for the listener callback
16
17
  * @returns Subscription
17
18
  */
18
- addListener(id: string, callback: (value: any) => void): EmitterSubscription | undefined {
19
+ async addListener(id: string, callback: (value: any) => void): Promise<EmitterSubscription | undefined> {
20
+
21
+ await Modules.Shared.attachEmitter(id);
19
22
 
20
23
  if (Platform.OS === 'android') {
21
24
  return DeviceEventEmitter.addListener(id, (event: any) => callback(event));
package/src/index.tsx CHANGED
@@ -96,7 +96,7 @@ class Courier {
96
96
 
97
97
  // System (Static)
98
98
 
99
- private attachPushNotificationListeners() {
99
+ private async attachPushNotificationListeners() {
100
100
 
101
101
  // Remove existing listeners
102
102
  // Only allows one subscription to be active
@@ -104,7 +104,7 @@ class Courier {
104
104
  this.pushNotificationDeliveredEmitter?.remove();
105
105
 
106
106
  // When a push notification is clicked
107
- this.pushNotificationClickedEmitter = this.systemBroadcaster.addListener(Events.Push.CLICKED, (event) => {
107
+ this.pushNotificationClickedEmitter = await this.systemBroadcaster.addListener(Events.Push.CLICKED, (event) => {
108
108
  try {
109
109
  const message = JSON.parse(event);
110
110
  this.pushListeners.forEach(listener => {
@@ -118,7 +118,7 @@ class Courier {
118
118
  });
119
119
 
120
120
  // When a push notification is delivered
121
- this.pushNotificationDeliveredEmitter = this.systemBroadcaster.addListener(Events.Push.DELIVERED, (event) => {
121
+ this.pushNotificationDeliveredEmitter = await this.systemBroadcaster.addListener(Events.Push.DELIVERED, (event) => {
122
122
  try {
123
123
  const message = JSON.parse(event);
124
124
  this.pushListeners.forEach(listener => {
@@ -178,11 +178,11 @@ class Courier {
178
178
 
179
179
  /**
180
180
  * Gets the current CourierClient instance.
181
- * @returns {CourierClient | undefined} The current CourierClient instance, or undefined if not initialized.
181
+ * @returns {Promise<CourierClient | undefined>} The current CourierClient instance, or undefined if not initialized.
182
182
  */
183
- public get client(): CourierClient | undefined {
183
+ public async getClient(): Promise<CourierClient | undefined> {
184
184
 
185
- const client = Modules.Shared.getClient() ?? undefined;
185
+ const client = await Modules.Shared.getClient() ?? undefined;
186
186
 
187
187
  if (!client) {
188
188
  return undefined;
@@ -205,26 +205,26 @@ class Courier {
205
205
 
206
206
  /**
207
207
  * Gets the current user ID.
208
- * @returns {string | undefined} The current user ID, or undefined if not set.
208
+ * @returns {Promise<string | undefined>} The current user ID, or undefined if not set.
209
209
  */
210
- public get userId(): string | undefined {
211
- return Modules.Shared.getUserId() ?? undefined;
210
+ public async getUserId(): Promise<string | undefined> {
211
+ return await Modules.Shared.getUserId() ?? undefined;
212
212
  }
213
213
 
214
214
  /**
215
215
  * Gets the current tenant ID.
216
- * @returns {string | undefined} The current tenant ID, or undefined if not set.
216
+ * @returns {Promise<string | undefined>} The current tenant ID, or undefined if not set.
217
217
  */
218
- public get tenantId(): string | undefined {
219
- return Modules.Shared.getTenantId() ?? undefined;
218
+ public async getTenantId(): Promise<string | undefined> {
219
+ return await Modules.Shared.getTenantId() ?? undefined;
220
220
  }
221
221
 
222
222
  /**
223
223
  * Checks if a user is currently signed in.
224
- * @returns {boolean} True if a user is signed in, false otherwise.
224
+ * @returns {Promise<boolean>} True if a user is signed in, false otherwise.
225
225
  */
226
- public get isUserSignedIn(): boolean {
227
- const isSignedIn: string = Modules.Shared.getIsUserSignedIn() ?? 'false';
226
+ public async isUserSignedIn(): Promise<boolean> {
227
+ const isSignedIn: string = await Modules.Shared.getIsUserSignedIn() ?? 'false';
228
228
  return isSignedIn.toLowerCase() === 'true';
229
229
  }
230
230
 
@@ -263,15 +263,15 @@ class Courier {
263
263
  * @param {function} props.onUserChanged - Callback function triggered when the user changes.
264
264
  * @returns {CourierAuthenticationListener} The created authentication listener.
265
265
  */
266
- public addAuthenticationListener(props: { onUserChanged: (userId?: string) => void }): CourierAuthenticationListener {
266
+ public async addAuthenticationListener(props: { onUserChanged: (userId?: string) => void }): Promise<CourierAuthenticationListener> {
267
267
 
268
268
  // Create a listener
269
269
  const listenerId = `authentication_${Utils.generateUUID()}`;
270
- const id = Modules.Shared.addAuthenticationListener(listenerId);
271
270
 
272
271
  // Attach the listener
273
- const listener = new CourierAuthenticationListener(id);
274
- listener.onUserChanged = this.sharedBroadcaster.addListener(listenerId, (event) => props.onUserChanged(event));
272
+ const listener = new CourierAuthenticationListener(listenerId);
273
+ listener.onUserChanged = await this.sharedBroadcaster.addListener(listenerId, (event) => props.onUserChanged(event));
274
+ const id = await Modules.Shared.addAuthenticationListener(listenerId);
275
275
  this.authenticationListeners.set(id, listener);
276
276
 
277
277
  return listener;
@@ -282,12 +282,12 @@ class Courier {
282
282
  * Removes a specific authentication listener.
283
283
  * @param {Object} props - The removal properties.
284
284
  * @param {string} props.listenerId - The ID of the listener to remove.
285
- * @returns {string} The ID of the removed listener.
285
+ * @returns {Promise<string>} A promise that resolves to the ID of the removed listener.
286
286
  */
287
- public removeAuthenticationListener(props: { listenerId: string }): string {
287
+ public async removeAuthenticationListener(props: { listenerId: string }): Promise<string> {
288
288
 
289
289
  // Remove the native listener
290
- Modules.Shared.removeAuthenticationListener(props.listenerId);
290
+ await Modules.Shared.removeAuthenticationListener(props.listenerId);
291
291
 
292
292
  // Remove the listener
293
293
  if (this.authenticationListeners.has(props.listenerId)) {
@@ -304,10 +304,10 @@ class Courier {
304
304
  * Removes all authentication listeners.
305
305
  * This method clears all registered authentication listeners, both native and JavaScript.
306
306
  */
307
- public removeAllAuthenticationListeners() {
307
+ public async removeAllAuthenticationListeners() {
308
308
 
309
309
  // Remove all native listeners
310
- Modules.Shared.removeAllAuthenticationListeners();
310
+ await Modules.Shared.removeAllAuthenticationListeners();
311
311
 
312
312
  // Iterate through all authentication listeners
313
313
  this.authenticationListeners.forEach((listener) => {
@@ -412,7 +412,7 @@ class Courier {
412
412
  * @param {string} props.listenerId - The ID of the listener to remove.
413
413
  * @returns {string} The ID of the removed listener.
414
414
  */
415
- public removePushNotificationListener(props: { listenerId: string }): string {
415
+ public async removePushNotificationListener(props: { listenerId: string }): Promise<string> {
416
416
  if (this.pushListeners.has(props.listenerId)) {
417
417
  this.pushListeners.delete(props.listenerId);
418
418
  }
@@ -422,7 +422,7 @@ class Courier {
422
422
  /**
423
423
  * Removes all push notification listeners.
424
424
  */
425
- public removeAllPushNotificationListeners() {
425
+ public async removeAllPushNotificationListeners() {
426
426
  this.pushListeners.forEach((listener) => {
427
427
  listener.remove();
428
428
  });
@@ -433,18 +433,19 @@ class Courier {
433
433
 
434
434
  /**
435
435
  * Gets the current pagination limit for inbox messages.
436
- * @returns {number} The current pagination limit.
436
+ * @returns {Promise<number>} A promise that resolves with the current pagination limit.
437
437
  */
438
- public get inboxPaginationLimit(): number {
439
- return Modules.Shared.getInboxPaginationLimit();
438
+ public async getInboxPaginationLimit(): Promise<number> {
439
+ return await Modules.Shared.getInboxPaginationLimit();
440
440
  }
441
441
 
442
442
  /**
443
443
  * Sets the pagination limit for inbox messages.
444
444
  * @param {number} limit - The new pagination limit to set.
445
+ * @returns {Promise<void>} A promise that resolves when the limit is set.
445
446
  */
446
- public set inboxPaginationLimit(limit: number) {
447
- Modules.Shared.setInboxPaginationLimit(limit);
447
+ public async setInboxPaginationLimit(limit: number): Promise<void> {
448
+ await Modules.Shared.setInboxPaginationLimit(limit);
448
449
  }
449
450
 
450
451
  /**
@@ -519,7 +520,7 @@ class Courier {
519
520
  * @param {Function} [props.onMessageRemoved] - Callback function called when a message is removed.
520
521
  * @returns {CourierInboxListener} A listener object that can be used to remove the listener later.
521
522
  */
522
- public addInboxListener(props: {
523
+ public async addInboxListener(props: {
523
524
  onInitialLoad?: () => void,
524
525
  onError?: (error: string) => void,
525
526
  onUnreadCountChanged?: (unreadCount: number) => void,
@@ -529,7 +530,9 @@ class Courier {
529
530
  onMessageChanged?: (feed: InboxMessageFeed, index: number, message: InboxMessage) => void,
530
531
  onMessageAdded?: (feed: InboxMessageFeed, index: number, message: InboxMessage) => void,
531
532
  onMessageRemoved?: (feed: InboxMessageFeed, index: number, message: InboxMessage) => void
532
- }): CourierInboxListener {
533
+ }): Promise<CourierInboxListener> {
534
+
535
+ const listenerId = `inbox_${Utils.generateUUID()}`;
533
536
 
534
537
  const listenerIds = {
535
538
  loading: `inbox_loading_${Utils.generateUUID()}`,
@@ -541,37 +544,24 @@ class Courier {
541
544
  messageChanged: `inbox_message_changed_${Utils.generateUUID()}`,
542
545
  messageAdded: `inbox_message_added_${Utils.generateUUID()}`,
543
546
  messageRemoved: `inbox_message_removed_${Utils.generateUUID()}`
544
- }
545
-
546
- // Set the listener id
547
- const id = Modules.Shared.addInboxListener(
548
- listenerIds.loading,
549
- listenerIds.error,
550
- listenerIds.unreadCount,
551
- listenerIds.feed,
552
- listenerIds.archive,
553
- listenerIds.pageAdded,
554
- listenerIds.messageChanged,
555
- listenerIds.messageAdded,
556
- listenerIds.messageRemoved
557
- );
547
+ };
558
548
 
559
549
  // Create the initial listeners
560
- const listener = new CourierInboxListener(id);
550
+ const listener = new CourierInboxListener(listenerId);
561
551
 
562
- listener.onInitialLoad = this.sharedBroadcaster.addListener(listenerIds.loading, (_: any) => {
552
+ listener.onInitialLoad = await this.sharedBroadcaster.addListener(listenerIds.loading, (_: any) => {
563
553
  props.onInitialLoad?.();
564
554
  });
565
555
 
566
- listener.onError = this.sharedBroadcaster.addListener(listenerIds.error, (event: any) => {
556
+ listener.onError = await this.sharedBroadcaster.addListener(listenerIds.error, (event: any) => {
567
557
  props.onError?.(event);
568
558
  });
569
559
 
570
- listener.onUnreadCountChanged = this.sharedBroadcaster.addListener(listenerIds.unreadCount, (event: any) => {
560
+ listener.onUnreadCountChanged = await this.sharedBroadcaster.addListener(listenerIds.unreadCount, (event: any) => {
571
561
  props.onUnreadCountChanged?.(event);
572
562
  });
573
563
 
574
- listener.onFeedChanged = this.sharedBroadcaster.addListener(listenerIds.feed, (event: any) => {
564
+ listener.onFeedChanged = await this.sharedBroadcaster.addListener(listenerIds.feed, (event: any) => {
575
565
  const convertedMessages = this.convertMessages(event.messages);
576
566
  const messageSet: InboxMessageSet = {
577
567
  messages: convertedMessages,
@@ -581,7 +571,7 @@ class Courier {
581
571
  props.onFeedChanged?.(messageSet);
582
572
  });
583
573
 
584
- listener.onArchiveChanged = this.sharedBroadcaster.addListener(listenerIds.archive, (event: any) => {
574
+ listener.onArchiveChanged = await this.sharedBroadcaster.addListener(listenerIds.archive, (event: any) => {
585
575
  const convertedMessages = this.convertMessages(event.messages);
586
576
  const messageSet: InboxMessageSet = {
587
577
  messages: convertedMessages,
@@ -591,7 +581,7 @@ class Courier {
591
581
  props.onArchiveChanged?.(messageSet);
592
582
  });
593
583
 
594
- listener.onPageAdded = this.sharedBroadcaster.addListener(listenerIds.pageAdded, (event: any) => {
584
+ listener.onPageAdded = await this.sharedBroadcaster.addListener(listenerIds.pageAdded, (event: any) => {
595
585
  const convertedMessages = this.convertMessages(event.messages);
596
586
  const messageSet: InboxMessageSet = {
597
587
  messages: convertedMessages,
@@ -601,25 +591,40 @@ class Courier {
601
591
  props.onPageAdded?.(event.feed === 'archived' ? 'archived' : 'feed', messageSet);
602
592
  });
603
593
 
604
- listener.onMessageChanged = this.sharedBroadcaster.addListener(listenerIds.messageChanged, (event: any) => {
594
+ listener.onMessageChanged = await this.sharedBroadcaster.addListener(listenerIds.messageChanged, (event: any) => {
605
595
  const convertedMessage = this.convertMessage(event.message);
606
596
  props.onMessageChanged?.(event.feed === 'archived' ? 'archived' : 'feed', event.index, convertedMessage);
607
597
  });
608
598
 
609
- listener.onMessageAdded = this.sharedBroadcaster.addListener(listenerIds.messageAdded, (event: any) => {
599
+ listener.onMessageAdded = await this.sharedBroadcaster.addListener(listenerIds.messageAdded, (event: any) => {
610
600
  const convertedMessage = this.convertMessage(event.message);
611
601
  props.onMessageAdded?.(event.feed === 'archived' ? 'archived' : 'feed', event.index, convertedMessage);
612
602
  });
613
603
 
614
- listener.onMessageRemoved = this.sharedBroadcaster.addListener(listenerIds.messageRemoved, (event: any) => {
604
+ listener.onMessageRemoved = await this.sharedBroadcaster.addListener(listenerIds.messageRemoved, (event: any) => {
615
605
  const convertedMessage = this.convertMessage(event.message);
616
606
  props.onMessageRemoved?.(event.feed === 'archived' ? 'archived' : 'feed', event.index, convertedMessage);
617
607
  });
618
608
 
609
+ // Attach listener to native code
610
+ const id = await Modules.Shared.addInboxListener(
611
+ listenerId,
612
+ listenerIds.loading,
613
+ listenerIds.error,
614
+ listenerIds.unreadCount,
615
+ listenerIds.feed,
616
+ listenerIds.archive,
617
+ listenerIds.pageAdded,
618
+ listenerIds.messageChanged,
619
+ listenerIds.messageAdded,
620
+ listenerIds.messageRemoved
621
+ );
622
+
619
623
  // Add listener to manager
620
624
  this.inboxListeners.set(id, listener);
621
625
 
622
626
  return listener;
627
+
623
628
  }
624
629
 
625
630
  private convertMessages(messages: string[]): InboxMessage[] {
@@ -648,10 +653,10 @@ class Courier {
648
653
  * @param {string} props.listenerId - The ID of the listener to remove.
649
654
  * @returns {string} The ID of the removed listener.
650
655
  */
651
- public removeInboxListener(props: { listenerId: string }): string {
656
+ public async removeInboxListener(props: { listenerId: string }): Promise<string> {
652
657
 
653
658
  // Call native code
654
- Modules.Shared.removeInboxListener(props.listenerId);
659
+ await Modules.Shared.removeInboxListener(props.listenerId);
655
660
 
656
661
  // Remove the listener
657
662
  if (this.inboxListeners.has(props.listenerId)) {
@@ -680,10 +685,10 @@ class Courier {
680
685
  /**
681
686
  * Removes all inbox listeners.
682
687
  */
683
- public removeAllInboxListeners() {
688
+ public async removeAllInboxListeners() {
684
689
 
685
690
  // Call native code
686
- Modules.Shared.removeAllInboxListeners();
691
+ await Modules.Shared.removeAllInboxListeners();
687
692
 
688
693
  // Remove all items from inboxListeners
689
694
  this.inboxListeners.forEach((listener) => {
@@ -697,6 +702,7 @@ class Courier {
697
702
  listener?.onMessageAdded?.remove();
698
703
  listener?.onMessageRemoved?.remove();
699
704
  });
705
+
700
706
  this.inboxListeners.clear();
701
707
 
702
708
  }
@@ -707,7 +713,7 @@ class Courier {
707
713
  * @returns {Promise<void>} A promise that resolves when the inbox is refreshed.
708
714
  */
709
715
  public async refreshInbox(): Promise<void> {
710
- return Modules.Shared.refreshInbox();
716
+ return await Modules.Shared.refreshInbox();
711
717
  }
712
718
 
713
719
  /**
@@ -10,10 +10,10 @@ export class CourierAuthenticationListener {
10
10
  this.listenerId = id;
11
11
  }
12
12
 
13
- public remove() {
13
+ public async remove() {
14
14
 
15
15
  // Remove the native inbox listener
16
- Courier.shared.removeInboxListener({ listenerId: this.listenerId });
16
+ await Courier.shared.removeInboxListener({ listenerId: this.listenerId });
17
17
 
18
18
  // Remove the emitter
19
19
  this.onUserChanged?.remove();
@@ -19,8 +19,8 @@ export class CourierInboxListener {
19
19
  this.listenerId = id;
20
20
  }
21
21
 
22
- public remove() {
23
- Courier.shared.removeInboxListener({ listenerId: this.listenerId });
22
+ public async remove() {
23
+ await Courier.shared.removeInboxListener({ listenerId: this.listenerId });
24
24
  }
25
25
 
26
26
  }