@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
@@ -1,9 +1,9 @@
1
- import { InboxMessage } from "./InboxMessage";
1
+ import { EmitterSubscription } from "react-native";
2
2
  export declare class CourierInboxListener {
3
3
  readonly listenerId: string;
4
- onInitialLoad?: () => void;
5
- onError?: (error: string) => void;
6
- onMessagesChanged?: (messages: InboxMessage[], unreadMessageCount: number, totalMessageCount: number, canPaginate: boolean) => void;
4
+ onInitialLoad?: EmitterSubscription;
5
+ onError?: EmitterSubscription;
6
+ onMessagesChanged?: EmitterSubscription;
7
7
  constructor(id: string);
8
8
  remove(): void;
9
9
  }
@@ -1 +1 @@
1
- {"version":3,"file":"CourierInboxListener.d.ts","sourceRoot":"","sources":["../../../src/models/CourierInboxListener.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,qBAAa,oBAAoB;IAE/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IACpB,aAAa,CAAC,EAAE,MAAM,IAAI,CAAA;IAC1B,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IACjC,iBAAiB,CAAC,EAAE,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,kBAAkB,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,KAAK,IAAI,CAAA;gBAE9H,EAAE,EAAE,MAAM;IAIf,MAAM;CASd"}
1
+ {"version":3,"file":"CourierInboxListener.d.ts","sourceRoot":"","sources":["../../../src/models/CourierInboxListener.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEnD,qBAAa,oBAAoB;IAE/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IACpB,aAAa,CAAC,EAAE,mBAAmB,CAAA;IACnC,OAAO,CAAC,EAAE,mBAAmB,CAAA;IAC7B,iBAAiB,CAAC,EAAE,mBAAmB,CAAA;gBAElC,EAAE,EAAE,MAAM;IAIf,MAAM;CAId"}
@@ -0,0 +1,21 @@
1
+ import { EmitterSubscription, NativeEventEmitter } from "react-native";
2
+ export declare namespace Events {
3
+ namespace Log {
4
+ const DEBUG_LOG = "courierDebugEvent";
5
+ }
6
+ namespace Push {
7
+ const CLICKED = "pushNotificationClicked";
8
+ const DELIVERED = "pushNotificationDelivered";
9
+ }
10
+ }
11
+ export declare namespace Utils {
12
+ /**
13
+ * Creates an event listener for the native function
14
+ * @param key Key for the listener
15
+ * @param callback Value returned for the listener callback
16
+ * @returns Subscription
17
+ */
18
+ function addEventListener(key: string, emitter: NativeEventEmitter, callback: (value: any) => void): EmitterSubscription | undefined;
19
+ function generateUUID(): string;
20
+ }
21
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAgC,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAErG,yBAAiB,MAAM,CAAC;IAEtB,UAAiB,GAAG,CAAC;QACZ,MAAM,SAAS,sBAAsB,CAAC;KAC9C;IAED,UAAiB,IAAI,CAAC;QACb,MAAM,OAAO,4BAA4B,CAAC;QAC1C,MAAM,SAAS,8BAA8B,CAAC;KACtD;CAEF;AAED,yBAAiB,KAAK,CAAC;IAErB;;;;;MAKE;IACF,SAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,GAAG,mBAAmB,GAAG,SAAS,CAY1I;IAED,SAAgB,YAAY,IAAI,MAAM,CAQrC;CAEF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trycourier/courier-react-native",
3
- "version": "2.3.2",
3
+ "version": "2.4.0",
4
4
  "description": "Inbox & Push Notifications for React Native",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
package/src/index.tsx CHANGED
@@ -1,9 +1,8 @@
1
1
  import {
2
- NativeModules,
3
- NativeEventEmitter,
4
2
  EmitterSubscription,
5
3
  Platform,
6
- DeviceEventEmitter,
4
+ NativeModules,
5
+ NativeEventEmitter,
7
6
  } from 'react-native';
8
7
 
9
8
  // Imports
@@ -16,6 +15,7 @@ import { CourierUserPreferencesTopic } from './models/CourierUserPreferencesTopi
16
15
  import { CourierUserPreferencesChannel } from './models/CourierUserPreferencesChannel';
17
16
  import { CourierUserPreferencesStatus } from './models/CourierUserPreferencesStatus';
18
17
  import { CourierPushProvider } from './models/CourierPushProvider';
18
+ import { Events, Utils } from './utils';
19
19
 
20
20
  // Exports
21
21
  export { CourierInboxView } from './views/CourierInboxView';
@@ -51,12 +51,11 @@ const CourierEventEmitter = new NativeEventEmitter(
51
51
 
52
52
  class Courier {
53
53
 
54
- readonly PUSH_NOTIFICATION_CLICKED = 'pushNotificationClicked';
55
- readonly PUSH_NOTIFICATION_DELIVERED = 'pushNotificationDelivered';
56
-
57
54
  private static _sharedInstance: Courier;
58
55
  private _isDebugging = false;
59
56
  private debugListener: EmitterSubscription | undefined;
57
+
58
+ private authListeners: Map<string, CourierAuthenticationListener> = new Map<string, CourierAuthenticationListener>();
60
59
  private inboxListeners: Map<string, CourierInboxListener> = new Map<string, CourierInboxListener>();
61
60
 
62
61
  public constructor() {
@@ -66,9 +65,6 @@ class Courier {
66
65
  // and will show all foreground notification styles in iOS
67
66
  this.setDefaults();
68
67
 
69
- // Register for inbox listener changes
70
- this.registerInboxListenerEvents();
71
-
72
68
  }
73
69
 
74
70
  // Returns the public shared instance
@@ -99,7 +95,7 @@ class Courier {
99
95
  // Set a new listener
100
96
  // listener needs to be registered first to catch the event
101
97
  if (isDebugging) {
102
- this.debugListener = CourierEventEmitter.addListener('courierDebugEvent', event => {
98
+ this.debugListener = CourierEventEmitter.addListener(Events.Log.DEBUG_LOG, event => {
103
99
  console.log('\x1b[36m%s\x1b[0m', 'COURIER', event);
104
100
  });
105
101
  }
@@ -119,36 +115,6 @@ class Courier {
119
115
  return this._isDebugging;
120
116
  }
121
117
 
122
- /**
123
- * Register inbox listener events
124
- */
125
- private registerInboxListenerEvents() {
126
-
127
- CourierEventEmitter.addListener('inboxInitialLoad', () => {
128
- this.inboxListeners.forEach((value, _) => {
129
- value.onInitialLoad?.();
130
- });
131
- });
132
-
133
- CourierEventEmitter.addListener('inboxError', event => {
134
- this.inboxListeners.forEach((value, _) => {
135
- value.onError?.(event);
136
- });
137
- });
138
-
139
- CourierEventEmitter.addListener('inboxMessagesChanged', event => {
140
- this.inboxListeners.forEach((value, _) => {
141
- value.onMessagesChanged?.(
142
- event.messages,
143
- event.unreadMessageCount,
144
- event.totalMessageCount,
145
- event.canPaginate,
146
- );
147
- });
148
- });
149
-
150
- }
151
-
152
118
  /**
153
119
  * Sets the notification presentation options for iOS
154
120
  */
@@ -210,52 +176,24 @@ class Courier {
210
176
 
211
177
  const pushListener = new CourierPushListener();
212
178
 
213
- if (Platform.OS === 'android') {
214
-
215
- if (props.onPushNotificationClicked) {
216
- pushListener.onNotificationClickedListener = DeviceEventEmitter.addListener(this.PUSH_NOTIFICATION_CLICKED, (event: any) => {
217
- try {
218
- props.onPushNotificationClicked!(JSON.parse(event));
219
- } catch (error) {
220
- console.log(error);
221
- }
222
- });
223
- }
224
-
225
- if (props.onPushNotificationDelivered) {
226
- pushListener.onNotificationDeliveredListener = DeviceEventEmitter.addListener(this.PUSH_NOTIFICATION_DELIVERED, (event: any) => {
227
- try {
228
- props.onPushNotificationDelivered!(JSON.parse(event));
229
- } catch (error) {
230
- console.log(error);
231
- }
232
- });
233
- }
234
-
179
+ if (props.onPushNotificationClicked) {
180
+ pushListener.onNotificationClickedListener = Utils.addEventListener(Events.Push.CLICKED, CourierEventEmitter, (event) => {
181
+ try {
182
+ props.onPushNotificationClicked!(JSON.parse(event));
183
+ } catch (error) {
184
+ console.log(error);
185
+ }
186
+ });
235
187
  }
236
188
 
237
- if (Platform.OS === 'ios') {
238
-
239
- if (props.onPushNotificationClicked) {
240
- pushListener.onNotificationClickedListener = CourierEventEmitter.addListener(this.PUSH_NOTIFICATION_CLICKED, (event: any) => {
241
- try {
242
- props.onPushNotificationClicked!(JSON.parse(event));
243
- } catch (error) {
244
- console.log(error);
245
- }
246
- });
247
- }
248
-
249
- if (props.onPushNotificationDelivered) {
250
- pushListener.onNotificationDeliveredListener = CourierEventEmitter.addListener(this.PUSH_NOTIFICATION_DELIVERED, (event: any) => {
251
- try {
252
- props.onPushNotificationDelivered!(JSON.parse(event));
253
- } catch (error) {
254
- console.log(error);
255
- }
256
- });
257
- }
258
-
189
+ if (props.onPushNotificationDelivered) {
190
+ pushListener.onNotificationDeliveredListener = Utils.addEventListener(Events.Push.DELIVERED, CourierEventEmitter, (event) => {
191
+ try {
192
+ props.onPushNotificationDelivered!(JSON.parse(event));
193
+ } catch (error) {
194
+ console.log(error);
195
+ }
196
+ });
259
197
  }
260
198
 
261
199
  // When listener is registered
@@ -286,7 +224,7 @@ class Courier {
286
224
  /**
287
225
  * Removes the current user and credentials from the sdk
288
226
  */
289
- public signOut(): Promise<void> {
227
+ public signOut(): Promise<void> {
290
228
  return CourierReactNativeModules.signOut();
291
229
  }
292
230
 
@@ -295,15 +233,22 @@ class Courier {
295
233
  */
296
234
  public addAuthenticationListener(props: { onUserChanged: (userId?: string) => void }): CourierAuthenticationListener {
297
235
 
298
- const authListener = new CourierAuthenticationListener();
236
+ // Event listener id
237
+ const authId = Utils.generateUUID();
299
238
 
300
- authListener.onUserChanged = CourierEventEmitter.addListener('courierAuthUserChanged', event => {
301
- props.onUserChanged(event)
302
- });
239
+ // Get the id
240
+ const id = CourierReactNativeModules.addAuthenticationListener(authId);
303
241
 
304
- authListener.listenerId = CourierReactNativeModules.addAuthenticationListener();
242
+ // Create the listener
243
+ const listener = new CourierAuthenticationListener(id);
305
244
 
306
- return authListener;
245
+ // Add the event listener
246
+ listener.onUserChanged = Utils.addEventListener(authId, CourierEventEmitter, (event) => props.onUserChanged(event));
247
+
248
+ // Add listener to manager
249
+ this.authListeners.set(id, listener);
250
+
251
+ return listener;
307
252
 
308
253
  }
309
254
 
@@ -311,7 +256,24 @@ class Courier {
311
256
  * Removes an authentication listener
312
257
  */
313
258
  public removeAuthenticationListener(props: { listenerId: string }): string {
314
- return CourierReactNativeModules.removeAuthenticationListener(props.listenerId);
259
+
260
+ // Remove the native listener
261
+ CourierReactNativeModules.removeAuthenticationListener(props.listenerId);
262
+
263
+ // Remove the listener
264
+ if (this.authListeners.has(props.listenerId)) {
265
+
266
+ // Get the listener
267
+ const listener = this.authListeners.get(props.listenerId);
268
+ listener?.onUserChanged?.remove();
269
+
270
+ // Remove the listener
271
+ this.authListeners.delete(props.listenerId);
272
+
273
+ }
274
+
275
+ return props.listenerId;
276
+
315
277
  }
316
278
 
317
279
  /**
@@ -347,14 +309,44 @@ class Courier {
347
309
  */
348
310
  public addInboxListener(props: { onInitialLoad?: () => void, onError?: (error: string) => void, onMessagesChanged?: (messages: InboxMessage[], unreadMessageCount: number, totalMessageCount: number, canPaginate: boolean) => void }): CourierInboxListener {
349
311
 
312
+ const listenerIds = {
313
+ loading: Utils.generateUUID(),
314
+ error: Utils.generateUUID(),
315
+ messages: Utils.generateUUID()
316
+ }
317
+
350
318
  // Set the listener id
351
- const id = CourierReactNativeModules.addInboxListener();
319
+ const id = CourierReactNativeModules.addInboxListener(
320
+ listenerIds.loading,
321
+ listenerIds.error,
322
+ listenerIds.messages
323
+ );
352
324
 
353
325
  // Create the initial listeners
354
326
  const listener = new CourierInboxListener(id);
355
- listener.onInitialLoad = props.onInitialLoad;
356
- listener.onError = props.onError;
357
- listener.onMessagesChanged = props.onMessagesChanged;
327
+
328
+ if (props.onInitialLoad) {
329
+ listener.onInitialLoad = Utils.addEventListener(listenerIds.loading, CourierEventEmitter, (_: any) => {
330
+ props.onInitialLoad?.();
331
+ });
332
+ }
333
+
334
+ if (props.onError) {
335
+ listener.onError = Utils.addEventListener(listenerIds.error, CourierEventEmitter, (event: any) => {
336
+ props.onError?.(event);
337
+ });
338
+ }
339
+
340
+ if (props.onMessagesChanged) {
341
+ listener.onMessagesChanged = Utils.addEventListener(listenerIds.messages, CourierEventEmitter, (event: any) => {
342
+ props.onMessagesChanged?.(
343
+ event.messages,
344
+ event.unreadMessageCount,
345
+ event.totalMessageCount,
346
+ event.canPaginate,
347
+ );
348
+ });
349
+ }
358
350
 
359
351
  // Add listener to manager
360
352
  this.inboxListeners.set(id, listener);
@@ -371,9 +363,18 @@ class Courier {
371
363
  // Call native code
372
364
  CourierReactNativeModules.removeInboxListener(props.listenerId);
373
365
 
374
- // Remove the js listener
366
+ // Remove the listener
375
367
  if (this.inboxListeners.has(props.listenerId)) {
368
+
369
+ // Remove emitters
370
+ const listener = this.inboxListeners.get(props.listenerId);
371
+ listener?.onInitialLoad?.remove();
372
+ listener?.onError?.remove();
373
+ listener?.onMessagesChanged?.remove();
374
+
375
+ // Remove the listener
376
376
  this.inboxListeners.delete(props.listenerId);
377
+
377
378
  }
378
379
 
379
380
  return props.listenerId;
@@ -392,7 +393,7 @@ class Courier {
392
393
  * Fetches the next page of inbox messages
393
394
  * Returns the fetched inbox messages
394
395
  */
395
- public async fetchNextPageOfMessages(): Promise<InboxMessage[]> {
396
+ public async fetchNextPageOfMessages(): Promise<InboxMessage[]> {
396
397
  return CourierReactNativeModules.fetchNextPageOfMessages();
397
398
  }
398
399
 
@@ -401,7 +402,7 @@ class Courier {
401
402
  * Min = 1
402
403
  * Max = 100
403
404
  */
404
- public setInboxPaginationLimit(props: { limit: number }): void {
405
+ public setInboxPaginationLimit(props: { limit: number }): void {
405
406
  CourierReactNativeModules.setInboxPaginationLimit(props.limit);
406
407
  }
407
408
 
@@ -3,15 +3,19 @@ import Courier from "..";
3
3
 
4
4
  export class CourierAuthenticationListener {
5
5
 
6
- public listenerId?: string
6
+ readonly listenerId: string
7
7
  public onUserChanged?: EmitterSubscription
8
8
 
9
+ constructor(id: string) {
10
+ this.listenerId = id;
11
+ }
12
+
9
13
  public remove() {
10
14
 
11
- if (this.listenerId) {
12
- Courier.shared.removeInboxListener({ listenerId: this.listenerId });
13
- }
15
+ // Remove the native inbox listener
16
+ Courier.shared.removeInboxListener({ listenerId: this.listenerId });
14
17
 
18
+ // Remove the emitter
15
19
  this.onUserChanged?.remove();
16
20
 
17
21
  }
@@ -1,24 +1,19 @@
1
1
  import Courier from "@trycourier/courier-react-native";
2
- import { InboxMessage } from "./InboxMessage";
2
+ import { EmitterSubscription } from "react-native";
3
3
 
4
4
  export class CourierInboxListener {
5
5
 
6
6
  readonly listenerId: string
7
- public onInitialLoad?: () => void
8
- public onError?: (error: string) => void
9
- public onMessagesChanged?: (messages: InboxMessage[], unreadMessageCount: number, totalMessageCount: number, canPaginate: boolean) => void
7
+ public onInitialLoad?: EmitterSubscription
8
+ public onError?: EmitterSubscription
9
+ public onMessagesChanged?: EmitterSubscription
10
10
 
11
11
  constructor(id: string) {
12
12
  this.listenerId = id;
13
13
  }
14
14
 
15
15
  public remove() {
16
-
17
- // Remove the native inbox listener
18
- if (this.listenerId) {
19
- Courier.shared.removeInboxListener({ listenerId: this.listenerId });
20
- }
21
-
16
+ Courier.shared.removeInboxListener({ listenerId: this.listenerId });
22
17
  }
23
18
 
24
19
  }
package/src/utils.tsx ADDED
@@ -0,0 +1,48 @@
1
+ import { EmitterSubscription, Platform, DeviceEventEmitter, NativeEventEmitter } from "react-native";
2
+
3
+ export namespace Events {
4
+
5
+ export namespace Log {
6
+ export const DEBUG_LOG = 'courierDebugEvent';
7
+ }
8
+
9
+ export namespace Push {
10
+ export const CLICKED = 'pushNotificationClicked';
11
+ export const DELIVERED = 'pushNotificationDelivered';
12
+ }
13
+
14
+ }
15
+
16
+ export namespace Utils {
17
+
18
+ /**
19
+ * Creates an event listener for the native function
20
+ * @param key Key for the listener
21
+ * @param callback Value returned for the listener callback
22
+ * @returns Subscription
23
+ */
24
+ export function addEventListener(key: string, emitter: NativeEventEmitter, callback: (value: any) => void): EmitterSubscription | undefined {
25
+
26
+ if (Platform.OS === 'android') {
27
+ return DeviceEventEmitter.addListener(key, (event: any) => callback(event));
28
+ }
29
+
30
+ if (Platform.OS === 'ios') {
31
+ return emitter.addListener(key, (event: any) => callback(event));
32
+ }
33
+
34
+ return undefined;
35
+
36
+ }
37
+
38
+ export function generateUUID(): string {
39
+ let uuid = '';
40
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
41
+ const charactersLength = characters.length;
42
+ for (let i = 0; i < 16; i++) {
43
+ uuid += characters.charAt(Math.floor(Math.random() * charactersLength));
44
+ }
45
+ return uuid;
46
+ }
47
+
48
+ }