* Sets up event handlers for various Iterable events. * * This method performs the following actions: * - Removes all existing listeners to avoid duplicate listeners. * - Adds listeners for URL handling, custom actions, in-app messages, and authentication. * * Event Handlers: *
()
| 979 | * @internal |
| 980 | */ |
| 981 | private static setupEventHandlers() { |
| 982 | // Remove all listeners to avoid duplicate listeners |
| 983 | Iterable.removeAllEventListeners(); |
| 984 | |
| 985 | if (Iterable.savedConfig.urlHandler) { |
| 986 | RNEventEmitter.addListener(IterableEventName.handleUrlCalled, (dict) => { |
| 987 | const url = dict.url; |
| 988 | const context = IterableActionContext.fromDict(dict.context); |
| 989 | Iterable.wakeApp(); |
| 990 | |
| 991 | if (Platform.OS === 'android') { |
| 992 | //Give enough time for Activity to wake up. |
| 993 | setTimeout(() => { |
| 994 | callUrlHandler(Iterable.savedConfig, url, context); |
| 995 | }, 1000); |
| 996 | } else { |
| 997 | callUrlHandler(Iterable.savedConfig, url, context); |
| 998 | } |
| 999 | }); |
| 1000 | } |
| 1001 | |
| 1002 | if (Iterable.savedConfig.customActionHandler) { |
| 1003 | RNEventEmitter.addListener( |
| 1004 | IterableEventName.handleCustomActionCalled, |
| 1005 | (dict) => { |
| 1006 | const action = IterableAction.fromDict(dict.action); |
| 1007 | const context = IterableActionContext.fromDict(dict.context); |
| 1008 | Iterable.savedConfig.customActionHandler!(action, context); |
| 1009 | } |
| 1010 | ); |
| 1011 | } |
| 1012 | |
| 1013 | if (Iterable.savedConfig.inAppHandler) { |
| 1014 | RNEventEmitter.addListener( |
| 1015 | IterableEventName.handleInAppCalled, |
| 1016 | (messageDict) => { |
| 1017 | const message = IterableInAppMessage.fromDict(messageDict); |
| 1018 | // MOB-10423: Check if we can use chain operator (?.) here instead |
| 1019 | const result = Iterable.savedConfig.inAppHandler!(message); |
| 1020 | IterableApi.setInAppShowResponse(result); |
| 1021 | } |
| 1022 | ); |
| 1023 | } |
| 1024 | |
| 1025 | if (Iterable.savedConfig.authHandler) { |
| 1026 | let authResponseCallback: IterableAuthResponseResult; |
| 1027 | RNEventEmitter.addListener(IterableEventName.handleAuthCalled, () => { |
| 1028 | // MOB-10423: Check if we can use chain operator (?.) here instead |
| 1029 | // Asks frontend of the client/app to pass authToken |
| 1030 | Iterable.savedConfig.authHandler!() |
| 1031 | .then((promiseResult) => { |
| 1032 | // Promise result can be either just String OR of type AuthResponse. |
| 1033 | // If type AuthReponse, authToken will be parsed looking for `authToken` within promised object. Two additional listeners will be registered for success and failure callbacks sent by native bridge layer. |
| 1034 | // Else it will be looked for as a String. |
| 1035 | if (isIterableAuthResponse(promiseResult)) { |
| 1036 | Iterable.authManager.passAlongAuthToken(promiseResult.authToken); |
| 1037 | |
| 1038 | setTimeout(() => { |
no test coverage detected