import { EmitterSubscription , NativeEventEmitter , NativeModule } from 'react-native' ; import NotificationReceivedEvent from './NotificationReceivedEvent' ; import { isMultipleInstancesPossible } from '../helpers' ; import { PERMISSION_CHANGED , SUBSCRIPTION_CHANGED , NOTIFICATION_WILL_SHOW , NOTIFICATION_OPENED , IN_APP_MESSAGE_CLICKED , EMAIL_SUBSCRIPTION_CHANGED , SMS_SUBSCRIPTION_CHANGED , IN_APP_MESSAGE_WILL_DISPLAY , IN_APP_MESSAGE_WILL_DISMISS , IN_APP_MESSAGE_DID_DISMISS , IN_APP_MESSAGE_DID_DISPLAY , } from './events' ; import { ChangeEvent } from '../models/Subscription' ; import OSNotification from '../OSNotification' ; const eventList = [ PERMISSION_CHANGED , SUBSCRIPTION_CHANGED , NOTIFICATION_WILL_SHOW , NOTIFICATION_OPENED , IN_APP_MESSAGE_CLICKED , EMAIL_SUBSCRIPTION_CHANGED , SMS_SUBSCRIPTION_CHANGED , IN_APP_MESSAGE_WILL_DISPLAY , IN_APP_MESSAGE_WILL_DISMISS , IN_APP_MESSAGE_DID_DISMISS , IN_APP_MESSAGE_DID_DISPLAY , ] ; export default class EventManager { private RNOneSignal : NativeModule ; private oneSignalEventEmitter : NativeEventEmitter ; private eventHandlerMap : Map < string , ( event : any ) => void > ; private eventHandlerArrayMap : Map < string , Array < ( event : any ) => void > > ; private listeners : { [ key ] : EmitterSubscription ; } ; constructor ( RNOneSignal ) { this . RNOneSignal = RNOneSignal ; this . oneSignalEventEmitter = new NativeEventEmitter ( RNOneSignal ) ; this . eventHandlerMap = new Map ( ) ; this . eventHandlerArrayMap = new Map ( ) ; this . listeners = { } ; this . setupListeners ( ) ; } setupListeners ( ) { if ( this . RNOneSignal != null ) { for ( let i = 0 ; i < eventList . length ; i ++ ) { let eventName = eventList [ i ] ; this . listeners [ eventName ] = this . generateEventListener ( eventName ) ; } } } clearHandlers ( ) { this . eventHandlerMap = new Map ( ) ; this . eventHandlerArrayMap = new Map ( ) ; } param { string } eventName param { function } handler returns setEventHandler < T > ( eventName , handler ) { this . eventHandlerMap . set ( eventName , handler ) ; } param { string } eventName param { function } handler returns addEventHandler < T > ( eventName , handler ) { let handlerArray = this . eventHandlerArrayMap . get ( eventName ) ; handlerArray && handlerArray . length > 0 ? handlerArray . push ( handler ) : this . eventHandlerArrayMap . set ( eventName , [ handler ] ) ; } param { string } eventName returns clearEventHandler ( eventName ) { this . eventHandlerArrayMap . delete ( eventName ) ; } generateEventListener ( eventName ) { const addListenerCallback = ( payload ) => { if ( isMultipleInstancesPossible ( eventName ) ) { let handlerArray = this . eventHandlerArrayMap . get ( eventName ) ; if ( handlerArray ) { handlerArray . forEach ( handler => { handler ( payload ) ; } ) ; } } else { let handler = this . eventHandlerMap . get ( eventName ) ; payload = this . getFinalPayload ( eventName , payload ) ; if ( handler ) { handler ( payload ) ; } } } ; return this . oneSignalEventEmitter . addListener ( eventName , addListenerCallback ) ; } getFinalPayload ( eventName , payload ) { switch ( eventName ) { case NOTIFICATION_WILL_SHOW : return new NotificationReceivedEvent ( payload as OSNotification ) ; default : return payload ; } } }