import { v4 } from 'uuid' ; import { EventBus } from './core/modules/EventBus' ; import { Api } from './modules/Api/Api' ; import { Data } from './modules/Data/Data' ; import { ApiClient } from './modules/ApiClient/ApiClient' ; import { PushServiceDefault , PushServiceSafari } from './services/PushService/PushService' ; import { Popup } from './features/Popup/Popup' ; import { SubscriptionSegmentsWidget } from './features/SubscriptionSegmentsWidget/SubscriptionSegmentsWidget' ; import { SubscriptionPromptWidget } from './features/SubscriptionPromptWidget/SubscriptionPromptWidget' ; import { ISubscriptionPromptWidgetParams } from './features/SubscriptionPromptWidget/SubscriptionPromptWidget.types' ; import * as CONSTANTS from './constants' ; import { IMapResponse } from './modules/ApiClient/ApiClient.types' ; import { clearLocationHash } from './functions' ; import { PlatformChecker } from './modules/PlatformChecker' ; import { Logger } from './logger' ; import FacebookModule from './modules/FacebookModule' ; import { InApps } from './modules/InApps/InApps' ; import { keyValue , log as logStorage , message as messageStorage } from './storage' ; import InboxMessagesModel from './models/InboxMessages' ; import InboxMessagesPublic from './modules/InboxMessagesPublic' ; import { IPushService } from './services/PushService/PushService.types' ; export default class Pushwoosh { public ready : boolean = false ; private readonly eventBus : EventBus ; private readonly data : Data ; private readonly apiClient : ApiClient ; private isCommunicationDisabled ? : boolean ; public readonly api : Api ; public readonly subscriptionSegmentWidget : SubscriptionSegmentsWidget ; public readonly subscriptionPromptWidget : SubscriptionPromptWidget ; public driver : IPushService ; private platformChecker : PlatformChecker ; public subscribeWidgetConfig : ISubscribeWidget ; public inboxWidgetConfig : IInboxWidget ; public subscribePopupConfig : any ; public InApps : InApps ; public pwinbox : InboxMessagesPublic ; private inboxModel : InboxMessagesModel ; constructor ( ) { this . eventBus = new EventBus ( ) ; this . data = new Data ( ) ; this . apiClient = new ApiClient ( this . data ) ; this . api = new Api ( this . eventBus , this . data , this . apiClient ) ; this . platformChecker = new PlatformChecker ( ) ; this . inboxModel = new InboxMessagesModel ( this . eventBus , this . data , this . api ) ; this . pwinbox = new InboxMessagesPublic ( this . data , this . api , this . inboxModel ) ; this . onServiceWorkerMessage = this . onServiceWorkerMessage . bind ( this ) ; const popup = new Popup ( 'subscription-segments' , ( ) => this . eventBus . dispatchEvent ( 'hide-subscription-widget' , { } ) , { position : 'top' } ) ; this . subscriptionSegmentWidget = new SubscriptionSegmentsWidget ( this . eventBus , this . data , this . apiClient , this . api , popup , this ) ; this . subscriptionPromptWidget = new SubscriptionPromptWidget ( this . eventBus , this ) ; } public readonly param { string } name param { function } handler returns { void } public addEventHandler = < Name extends EventName > ( name , handler ) : void => this . eventBus . addEventHandler ( name , handler ) ; public readonly param { string } name param { function } handler returns { void } public removeEventHandler = < Name extends EventName > ( name , handler ) : void => this . eventBus . removeEventHandler ( name , handler ) ; public readonly param { string } name param { object } payload returns { string } public dispatchEvent = < Name extends EventName > ( name , payload ) : string => this . eventBus . dispatchEvent ( name , payload ) ; type { { showLog : ( ( ) => Promise < any > ) ; showKeyValues : ( ( ) => Promise < any > ) ; showMessages : ( ( ) => Promise < any > ) } } public debug = { async showLog ( ) { const items = await logStorage . getAll ( ) ; console . log ( items ) ; } , async showKeyValues ( ) { const items = await keyValue . getAll ( ) ; console . log ( items ) ; } , async showMessages ( ) { const items = await messageStorage . getAll ( ) ; items . forEach ( ( i ) => console . log ( i ) ) ; } } ; param command public push ( command ) { if ( typeof command === 'function' ) { this . subscribeToLegacyEvents ( 'onReady' , command ) ; return ; } if ( ! Array . isArray ( command ) ) { throw new Error ( 'Invalid command!' ) ; } switch ( command [ 0 ] ) { case 'init' : this . initialize ( command [ 1 ] ) ; break ; case CONSTANTS . EVENT_ON_LOAD : case CONSTANTS . EVENT_ON_READY : case CONSTANTS . EVENT_ON_REGISTER : case CONSTANTS . EVENT_ON_SUBSCRIBE : case CONSTANTS . EVENT_ON_UNSUBSCRIBE : case CONSTANTS . EVENT_ON_SW_INIT_ERROR : case CONSTANTS . EVENT_ON_PUSH_DELIVERY : case CONSTANTS . EVENT_ON_NOTIFICATION_CLICK : case CONSTANTS . EVENT_ON_NOTIFICATION_CLOSE : case CONSTANTS . EVENT_ON_CHANGE_COMMUNICATION_ENABLED : case CONSTANTS . EVENT_ON_PUT_NEW_MESSAGE_TO_INBOX_STORE : case CONSTANTS . EVENT_ON_UPDATE_INBOX_MESSAGES : case CONSTANTS . EVENT_ON_SHOW_NOTIFICATION_PERMISSION_DIALOG : case CONSTANTS . EVENT_ON_HIDE_NOTIFICATION_PERMISSION_DIALOG : case CONSTANTS . EVENT_ON_SHOW_SUBSCRIPTION_WIDGET : case CONSTANTS . EVENT_ON_HIDE_SUBSCRIPTION_WIDGET : case CONSTANTS . EVENT_ON_PERMISSION_DENIED : case CONSTANTS . EVENT_ON_PERMISSION_PROMPT : case CONSTANTS . EVENT_ON_PERMISSION_GRANTED : this . subscribeToLegacyEvents ( command [ 0 ] , command [ 1 ] ) ; break ; default : throw new Error ( 'Unknown command!' ) ; } } returns { Promise < void > } public async subscribe ( isForceSubscribe = true ) { if ( this . isCommunicationDisabled ) { Logger . error ( 'Communication is disabled!' ) ; } const isPermissionDefault = this . driver . checkIsPermissionDefault ( ) ; if ( isPermissionDefault ) { this . eventBus . dispatchEvent ( 'show-notification-permission-dialog' , { } ) ; await this . driver . askPermission ( ) ; const permission = this . driver . getPermission ( ) ; this . eventBus . dispatchEvent ( 'hide-notification-permission-dialog' , { permission } ) ; } const permission = this . driver . getPermission ( ) ; const isManualUnsubscribed = await this . data . getStatusManualUnsubscribed ( ) ; const isDeviceRegister = await this . api . checkDeviceSubscribeForPushNotifications ( false ) ; if ( permission === CONSTANTS . PERMISSION_GRANTED ) { this . eventBus . dispatchEvent ( 'permission-granted' , { } ) ; const needSubscribe = isForceSubscribe || ! isManualUnsubscribed ; if ( needSubscribe && ! isDeviceRegister ) { await this . driver . subscribe ( ) ; } this . eventBus . dispatchEvent ( 'subscribe' , { } ) ; return ; } if ( permission === CONSTANTS . PERMISSION_DENIED ) { this . eventBus . dispatchEvent ( 'permission-denied' , { } ) ; if ( isDeviceRegister ) { await this . driver . unsubscribe ( ) ; } return ; } } returns { Promise < void > } public async unsubscribe ( ) { try { await this . driver . unsubscribe ( ) ; } catch ( error ) { Logger . error ( error , 'Error occurred during the unsubscribe' ) ; } } returns { Promise < void > } public async forceSubscribe ( ) { await this . subscribe ( true ) ; } returns { boolean } public isDeviceRegistered ( ) { return localStorage . getItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS ) === CONSTANTS . DEVICE_REGISTRATION_STATUS_REGISTERED ; } public isDeviceUnregistered ( ) { return localStorage . getItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS ) === CONSTANTS . DEVICE_REGISTRATION_STATUS_UNREGISTERED ; } returns { Promise < boolean > } public async isSubscribed ( ) { return this . api . checkDeviceSubscribeForPushNotifications ( ) ; } returns { Promise < boolean > } public async isCommunicationEnabled ( ) { const isCommunicationDisabled = await this . data . getStatusCommunicationDisabled ( ) ; return ! isCommunicationDisabled ; } param { boolean } isEnabled returns { Promise < void > } public async setCommunicationEnabled ( isEnabled = true ) { const deviceType = await this . data . getDeviceType ( ) ; const isPermissionGranted = this . driver . checkIsPermissionGranted ( ) ; await this . data . setStatusCommunicationDisabled ( ! isEnabled ) ; if ( isEnabled ) { await this . data . setStatusDropAllData ( false ) ; if ( isPermissionGranted ) { await this . api . registerDevice ( ) ; } } else { await this . api . unregisterDevice ( ) ; } this . eventBus . dispatchEvent ( 'change-enabled-communication' , { isEnabled } ) ; await this . api . postEvent ( CONSTANTS . EVENT_GDPR_CONSENT , { channel : isEnabled , device_type : deviceType , } ) ; } returns { Promise < void > } public async removeAllDeviceData ( ) { const deviceType = await this . data . getDeviceType ( ) ; await this . api . postEvent ( CONSTANTS . EVENT_GDPR_DELETE , { status : true , device_type : deviceType } ) ; await this . api . deleteDevice ( ) ; await this . data . clearAll ( ) ; await this . data . setStatusDropAllData ( true ) ; } returns { Promise < string > } public async getHWID ( ) { return await this . data . getHwid ( ) ; } returns { Promise < string > } public async getPushToken ( ) { const { pushToken } = await this . data . getTokens ( ) ; return pushToken ; } returns { Promise < string | null > } public async getUserId ( ) { return await this . data . getUserId ( ) ; } returns { Promise < IPWParams > } public async getParams ( ) { return await this . api . getParams ( ) ; } returns { boolean } public isAvailableNotifications ( ) { return this . platformChecker . isAvailableNotifications ; } public async sendStatisticsVisitedPage ( ) { const { document : { title } , location : { origin , pathname , href } } = window ; await this . api . pageVisit ( { title , url_path : `${ origin }${ pathname }` , url : href } ) ; } public async isEnableChannels ( ) { const features = await this . data . getFeatures ( ) ; const channels = features && features [ 'channels' ] ; return Array . isArray ( channels ) && ! ! channels . length ; } private async initialize ( params ) { const manualDebug = localStorage . getItem ( CONSTANTS . MANUAL_SET_LOGGER_LEVEL ) ; Logger . setLevel ( manualDebug || params . logLevel || 'error' ) ; if ( ! this . platformChecker . isAvailableNotifications ) { return ; } const applicationCode = await this . data . getApplicationCode ( ) ; if ( ! params . applicationCode ) { throw new Error ( 'Can\'t find application code!' ) ; } const notSavedApplicationCode = ! applicationCode ; const isChangeApplicationCode = applicationCode && applicationCode !== params . applicationCode ; if ( notSavedApplicationCode || isChangeApplicationCode ) { await this . data . clearAll ( ) ; await this . data . setApplicationCode ( params . applicationCode ) ; } const hwid = await this . data . getHwid ( ) ; if ( ! hwid ) { const id = params . applicationCode + '_' + v4 ( ) ; await this . data . setHwid ( id ) ; } else { await this . api . checkDevice ( ) ; } await this . data . setDeviceType ( this . platformChecker . getPlatformType ( ) ) ; await this . data . setDeviceModel ( this . platformChecker . getBrowserVersion ( ) ) ; await this . data . setLanguage ( params . tags && params . tags . Language || navigator . language ) ; if ( params . pushwooshUrl ) { await this . data . setApiEntrypoint ( params . pushwooshUrl ) ; } await this . data . setSdkVersion ( __VERSION__ ) ; const config = await this . api . getConfig ( [ 'page_visit' , 'vapid_key' , 'web_in_apps' , 'events' , 'subscription_prompt' ] ) ; this . onGetConfig ( config && config . features ) ; this . subscribeWidgetConfig = { enable : false , ... params . subscribeWidget } ; this . inboxWidgetConfig = { enable : false , ... params . inboxWidget } ; this . subscribePopupConfig = { enable : false , ... params . subscribePopup } ; this . isCommunicationDisabled = await this . data . getStatusCommunicationDisabled ( ) ; await this . open ( ) ; const userIdWasChange = await this . data . getStatusUserIdWasChanged ( ) ; if ( params . userId && params . userId !== 'user_id' && ! userIdWasChange ) { await this . api . registerUser ( params . userId ) ; } if ( params . tags ) { this . api . setTags ( params . tags ) ; } const inAppsConfig = { enable : config . features . web_in_apps && config . features . web_in_apps . enabled , ... params . inApps , } ; await this . initInApp ( inAppsConfig ) ; if ( this . platformChecker . isAvailableNotifications ) { await this . initPushNotifications ( params ) ; } try { await this . inboxModel . updateMessages ( ) ; } catch ( error ) { Logger . write ( 'error' , error ) ; } try { await this . initFacebook ( params ) ; } catch ( error ) { Logger . write ( 'error' , error ) ; } this . ready = true ; this . eventBus . dispatchEvent ( 'ready' , { } ) ; const delayedEvent = await this . data . getDelayedEvent ( ) ; if ( delayedEvent ) { const { type , payload } = delayedEvent ; await this . emitLegacyEventsFromServiceWorker ( type , payload ) ; await this . data . setDelayedEvent ( null ) ; } if ( 'serviceWorker' in navigator ) { navigator . serviceWorker . onmessage = this . onServiceWorkerMessage ; } localStorage . setItem ( 'pushwoosh-websdk-status' , 'init' ) ; document . dispatchEvent ( new CustomEvent ( 'pushwoosh.initialized' , { detail : { pw : this } } ) ) ; if ( this . platformChecker . isSafari ) { const hashReg = /#P(.*)/ ; const hash = decodeURIComponent ( document . location . hash ) ; if ( hashReg . test ( hash ) ) { this . api . pushStat ( hashReg . exec ( hash ) [ 1 ] ) . then ( clearLocationHash ) ; } } } returns { Promise < void > } private async defaultProcess ( initParams ) { const permission = this . driver . getPermission ( ) ; if ( permission === 'granted' ) { await this . data . setLastPermissionStatus ( permission ) ; } const isCommunicationDisabled = await this . data . getStatusCommunicationDisabled ( ) ; const isDropAllData = await this . data . getStatusDropAllData ( ) ; const isNeedResubscribe = await this . driver . checkIsNeedResubscribe ( ) ; const features = await this . data . getFeatures ( ) ; const currentPromptUseCase = features [ 'subscription_prompt' ] && features [ 'subscription_prompt' ] [ 'use_case' ] ; if ( isCommunicationDisabled || isDropAllData ) { await this . unsubscribe ( ) ; return ; } if ( isNeedResubscribe ) { await this . unsubscribe ( ) ; await this . data . setStatusManualUnsubscribed ( false ) ; } const { autoSubscribe } = initParams ; const isManualUnsubscribed = await this . data . getStatusManualUnsubscribed ( ) ; const isRegister = await this . api . checkDeviceSubscribeForPushNotifications ( false ) ; switch ( permission ) { case CONSTANTS . PERMISSION_PROMPT : this . eventBus . dispatchEvent ( 'permission-default' , { } ) ; if ( isRegister ) { await this . unsubscribe ( ) ; } const widgetConfig = await this . getWidgetConfig ( ) ; const canShowByCapping = await this . checkCanShowByCapping ( widgetConfig ) ; if ( ! canShowByCapping ) { break ; } const isTopicBasedUseCase = currentPromptUseCase === CONSTANTS . SUBSCRIPTION_WIDGET_USE_CASE_TOPIC_BASED ; if ( isTopicBasedUseCase ) { await this . subscriptionSegmentWidget . init ( ) ; } const isDefaultUseCase = currentPromptUseCase === CONSTANTS . SUBSCRIPTION_WIDGET_USE_CASE_DEFAULT ; const isNotSetUseCase = currentPromptUseCase === CONSTANTS . SUBSCRIPTION_WIDGET_USE_CASE_NOT_SET && autoSubscribe ; if ( isDefaultUseCase || isNotSetUseCase ) { this . subscriptionPromptWidget . init ( widgetConfig ) ; this . subscriptionPromptWidget . show ( ) ; } await this . updateCappingParams ( ) ; break ; case CONSTANTS . PERMISSION_DENIED : this . eventBus . dispatchEvent ( 'permission-denied' , { } ) ; if ( isRegister ) { await this . unsubscribe ( ) ; } break ; case CONSTANTS . PERMISSION_GRANTED : this . eventBus . dispatchEvent ( 'permission-granted' , { } ) ; if ( isManualUnsubscribed && isRegister ) { await this . unsubscribe ( ) ; } if ( ! isRegister && ! isManualUnsubscribed || isNeedResubscribe ) { await this . subscribe ( true ) ; const isTopicBasedUseCase = currentPromptUseCase === CONSTANTS . SUBSCRIPTION_WIDGET_USE_CASE_TOPIC_BASED ; if ( isTopicBasedUseCase ) { const result = features . channels . map ( ( channel ) => channel . code ) ; await this . api . setTags ( { 'Subscription Segments' : result } ) ; } } break ; } } private async getWidgetConfig ( ) { const features = await this . data . getFeatures ( ) ; const currentConfig = features [ 'subscription_prompt_widget' ] && features [ 'subscription_prompt_widget' ] . params ; const configWithDefaultCapping = { cappingCount : CONSTANTS . SUBSCRIPTION_PROMPT_WIDGET_DEFAULT_CONFIG . cappingCount , cappingDelay : CONSTANTS . SUBSCRIPTION_PROMPT_WIDGET_DEFAULT_CONFIG . cappingDelay , ... currentConfig } ; return currentConfig ? configWithDefaultCapping : CONSTANTS . SUBSCRIPTION_PROMPT_WIDGET_DEFAULT_CONFIG ; } private async checkCanShowByCapping ( widgetConfig ) { const currentTime = new Date ( ) . getTime ( ) ; const displayCount = await this . data . getPromptDisplayCount ( ) ; const lastSeenTime = await this . data . getPromptLastSeenTime ( ) ; const canShowByCapping = widgetConfig . cappingCount > displayCount ; const canShowByLastTime = currentTime - lastSeenTime > widgetConfig . cappingDelay ; return canShowByCapping && canShowByLastTime ; } private async updateCappingParams ( ) { const displayCount = await this . data . getPromptDisplayCount ( ) ; const currentTime = new Date ( ) . getTime ( ) ; await this . data . setPromptDisplayCount ( displayCount + 1 ) ; await this . data . setPromptLastSeenTime ( currentTime ) ; } param { IInitParams } initParams returns { Promise < void > } private initFacebook ( initParams ) { const facebook = { enable : false , pageId : '' , containerClass : '' , ... initParams . facebook } ; if ( facebook && facebook . enable ) { try { new FacebookModule ( { pageId : facebook . pageId , containerClass : facebook . containerClass , applicationCode : initParams . applicationCode , userId : initParams . userId || '' } ) ; } catch ( error ) { Logger . error ( error , 'facebook module initialization failed' ) ; } } } param { IInitParams } params return { Promise < void > } private async initInApp ( params ) { if ( params && params . enable ) { try { this . InApps = new InApps ( params , this , this . eventBus , this . api ) ; await this . InApps . init ( ) . then ( ( ) => { Logger . info ( 'InApps module has been initialized' ) ; this . eventBus . dispatchEvent ( 'initialize-in-apps-module' , { } ) ; } ) . catch ( ( error ) => { Logger . error ( error , 'InApps module initialization has been failed' ) ; } ) ; } catch ( error ) { Logger . error ( error , 'InApp module initialization has been failed' ) ; } } } param { MessageEvent } event private onServiceWorkerMessage ( event ) { const { data = { } } = event || { } ; const { type = '' , payload = { } } = data || { } ; this . emitLegacyEventsFromServiceWorker ( type , payload ) ; } param { boolean } isForce returns { Promise < void > } private async open ( isForce ? ) { let lastApplicationOpenTime = await this . data . getLastOpenApplicationTime ( ) ; const currentTime = Date . now ( ) ; if ( ! lastApplicationOpenTime ) { lastApplicationOpenTime = 0 ; } const isSendingPeriodExceeded = currentTime - lastApplicationOpenTime < CONSTANTS . PERIOD_SEND_APP_OPEN ; const needSendOpenStatistics = isForce || ! isSendingPeriodExceeded ; if ( ! needSendOpenStatistics ) { return ; } await this . data . setLastOpenApplicationTime ( currentTime ) ; await this . api . applicationOpen ( ) ; } private async onGetConfig ( features ) { await this . data . setFeatures ( features ) ; if ( features ) { if ( features . page_visit && features . page_visit . enabled ) { await keyValue . set ( CONSTANTS . PAGE_VISITED_URL , features . page_visit . entrypoint ) ; this . sendStatisticsVisitedPage ( ) ; } if ( features . events && features . events . length ) { const isPageVisitedEvent = features . events . some ( ( ( event ) : boolean => event === CONSTANTS . EVENT_PW_SITE_OPENED ) ) ; if ( isPageVisitedEvent ) { this . sendPostEventVisitedPage ( ) ; } } if ( features . vapid_key ) { await this . data . setApplicationServerKey ( features . vapid_key ) ; } } } private async initPushNotifications ( params ) { await this . data . setDefaultNotificationImage ( params . defaultNotificationImage ) ; await this . data . setDefaultNotificationTitle ( params . defaultNotificationTitle ) ; await this . data . setServiceWorkerUrl ( params . serviceWorkerUrl ) ; await this . data . setServiceWorkerScope ( params . scope ) ; await this . data . setInitParams ( { autoSubscribe : true , ... params , } ) ; await this . initDriver ( ) ; try { await this . defaultProcess ( params ) ; } catch ( error ) { Logger . error ( error , 'Internal error: defaultProcess fail' ) ; } } private async initDriver ( ) { if ( this . platformChecker . isSafari ) { const { safariWebsitePushID : webSitePushId } = await this . data . getInitParams ( ) ; if ( ! webSitePushId ) { throw new Error ( 'For work with Safari Push Notification add safariWebsitePushID to initParams!' ) ; } this . driver = new PushServiceSafari ( this . api , this . data , { webSitePushId } ) ; return ; } if ( this . platformChecker . isAvailableServiceWorker ) { this . driver = new PushServiceDefault ( this . api , this . data , { } ) ; return ; } } public async sendPostEventVisitedPage ( ) { const { document : { title } , location : { href } } = window ; this . api . postEvent ( CONSTANTS . EVENT_PW_SITE_OPENED , { url : href , title : title , device_type : this . platformChecker . platform } ) ; } private param { string } type param { function } handler private subscribeToLegacyEvents ( type , handler ) { switch ( type ) { case CONSTANTS . EVENT_ON_LOAD : handler ( ) ; break ; case CONSTANTS . EVENT_ON_READY : if ( this . ready ) { handler ( this . api ) ; break ; } this . eventBus . addEventHandler ( 'ready' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_REGISTER : this . eventBus . addEventHandler ( 'register' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_SUBSCRIBE : this . eventBus . addEventHandler ( 'subscribe' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_UNSUBSCRIBE : this . eventBus . addEventHandler ( 'unsubscribe' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_SW_INIT_ERROR : this . eventBus . addEventHandler ( 'initialize-service-worker-error' , ( { error } ) => handler ( this . api , error ) ) ; break ; case CONSTANTS . EVENT_ON_PUSH_DELIVERY : this . eventBus . addEventHandler ( 'receive-push' , ( { notification } ) => handler ( this . api , notification ) ) ; break ; case CONSTANTS . EVENT_ON_NOTIFICATION_CLICK : this . eventBus . addEventHandler ( 'open-notification' , ( { notification } ) => handler ( this . api , notification ) ) ; break ; case CONSTANTS . EVENT_ON_NOTIFICATION_CLOSE : this . eventBus . addEventHandler ( 'hide-notification' , ( { notification } ) => handler ( this . api , notification ) ) ; break ; case CONSTANTS . EVENT_ON_CHANGE_COMMUNICATION_ENABLED : this . eventBus . addEventHandler ( 'change-enabled-communication' , ( { isEnabled } ) => handler ( this . api , isEnabled ) ) ; break ; case CONSTANTS . EVENT_ON_PUT_NEW_MESSAGE_TO_INBOX_STORE : this . eventBus . addEventHandler ( 'receive-inbox-message' , ( { message } ) => handler ( this . api , message ) ) ; break ; case CONSTANTS . EVENT_ON_UPDATE_INBOX_MESSAGES : this . eventBus . addEventHandler ( 'update-inbox-messages' , ( { messages } ) => handler ( this . api , messages ) ) ; break ; case CONSTANTS . EVENT_ON_SHOW_NOTIFICATION_PERMISSION_DIALOG : this . eventBus . addEventHandler ( 'show-notification-permission-dialog' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_HIDE_NOTIFICATION_PERMISSION_DIALOG : this . eventBus . addEventHandler ( 'hide-notification-permission-dialog' , ( { permission } ) => handler ( this . api , permission ) ) ; break ; case CONSTANTS . EVENT_ON_SHOW_SUBSCRIPTION_WIDGET : this . eventBus . addEventHandler ( 'show-subscription-widget' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_HIDE_SUBSCRIPTION_WIDGET : this . eventBus . addEventHandler ( 'hide-subscription-widget' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_PERMISSION_DENIED : this . eventBus . addEventHandler ( 'permission-denied' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_PERMISSION_PROMPT : this . eventBus . addEventHandler ( 'permission-default' , ( ) => handler ( this . api ) ) ; break ; case CONSTANTS . EVENT_ON_PERMISSION_GRANTED : this . eventBus . addEventHandler ( 'permission-granted' , ( ) => handler ( this . api ) ) ; break ; } } private emitLegacyEventsFromServiceWorker ( type , payload ? ) { switch ( type ) { case CONSTANTS . EVENT_ON_PUSH_DELIVERY : this . eventBus . dispatchEvent ( 'receive-push' , { notification : payload } ) ; break ; case CONSTANTS . EVENT_ON_NOTIFICATION_CLICK : this . eventBus . dispatchEvent ( 'open-notification' , { notification : payload } ) ; break ; case CONSTANTS . EVENT_ON_NOTIFICATION_CLOSE : this . eventBus . dispatchEvent ( 'hide-notification' , { notification : payload } ) ; break ; case CONSTANTS . EVENT_ON_PUT_NEW_MESSAGE_TO_INBOX_STORE : this . eventBus . dispatchEvent ( 'receive-inbox-message' , { message : payload } ) ; break ; } } }