diff --git a/packages/messaging/src/api.ts b/packages/messaging/src/api.ts index 56af3928503..821c2fe759c 100644 --- a/packages/messaging/src/api.ts +++ b/packages/messaging/src/api.ts @@ -31,9 +31,11 @@ import { import { MessagingService } from './messaging-service'; import { deleteToken as _deleteToken } from './api/deleteToken'; import { getToken as _getToken } from './api/getToken'; +import { isSwSupported, isWindowSupported } from './api/isSupported'; import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage'; import { onMessage as _onMessage } from './api/onMessage'; import { _setDeliveryMetricsExportedToBigQueryEnabled } from './api/setDeliveryMetricsExportedToBigQueryEnabled'; +import { ERROR_FACTORY, ErrorCode } from './util/errors'; /** * Retrieves a Firebase Cloud Messaging instance. @@ -43,6 +45,22 @@ import { _setDeliveryMetricsExportedToBigQueryEnabled } from './api/setDeliveryM * @public */ export function getMessagingInWindow(app: FirebaseApp = getApp()): Messaging { + // Conscious decision to make this async check non-blocking during the messaging instance + // initialization phase for performance consideration. An error would be thrown latter for + // developer's information. Developers can then choose to import and call `isSupported` for + // special handling. + isWindowSupported().then( + isSupported => { + // If `isWindowSupported()` resolved, but returned false. + if (!isSupported) { + throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); + } + }, + _ => { + // If `isWindowSupported()` rejected. + throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED); + } + ); return _getProvider(getModularInstance(app), 'messaging').getImmediate(); } @@ -54,10 +72,23 @@ export function getMessagingInWindow(app: FirebaseApp = getApp()): Messaging { * @public */ export function getMessagingInSw(app: FirebaseApp = getApp()): Messaging { - return _getProvider( - getModularInstance(app), - 'messaging-sw' - ).getImmediate(); + // Conscious decision to make this async check non-blocking during the messaging instance + // initialization phase for performance consideration. An error would be thrown latter for + // developer's information. Developers can then choose to import and call `isSupported` for + // special handling. + isSwSupported().then( + isSupported => { + // If `isSwSupported()` resolved, but returned false. + if (!isSupported) { + throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); + } + }, + _ => { + // If `isSwSupported()` rejected. + throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED); + } + ); + return _getProvider(getModularInstance(app), 'messaging-sw').getImmediate(); } /** diff --git a/packages/messaging/src/helpers/register.ts b/packages/messaging/src/helpers/register.ts index d85e0318e15..97c7dfb6327 100644 --- a/packages/messaging/src/helpers/register.ts +++ b/packages/messaging/src/helpers/register.ts @@ -21,8 +21,6 @@ import { ComponentType, InstanceFactory } from '@firebase/component'; -import { ERROR_FACTORY, ErrorCode } from '../util/errors'; -import { isSwSupported, isWindowSupported } from '../api/isSupported'; import { onNotificationClick, onPush, @@ -40,8 +38,6 @@ import { messageEventListener } from '../listeners/window-listener'; const WindowMessagingFactory: InstanceFactory<'messaging'> = ( container: ComponentContainer ) => { - maybeThrowWindowError(); - const messaging = new MessagingService( container.getProvider('app').getImmediate(), container.getProvider('installations-internal').getImmediate(), @@ -58,8 +54,6 @@ const WindowMessagingFactory: InstanceFactory<'messaging'> = ( const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = ( container: ComponentContainer ) => { - maybeThrowWindowError(); - const messaging = container .getProvider('messaging') .getImmediate() as MessagingService; @@ -71,40 +65,10 @@ const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = ( return messagingInternal; }; -function maybeThrowWindowError(): void { - // Conscious decision to make this async check non-blocking during the messaging instance - // initialization phase for performance consideration. An error would be thrown latter for - // developer's information. Developers can then choose to import and call `isSupported` for - // special handling. - isWindowSupported() - .then(isSupported => { - if (!isSupported) { - throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); - } - }) - .catch(_ => { - throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED); - }); -} - declare const self: ServiceWorkerGlobalScope; const SwMessagingFactory: InstanceFactory<'messaging'> = ( container: ComponentContainer ) => { - // Conscious decision to make this async check non-blocking during the messaging instance - // initialization phase for performance consideration. An error would be thrown latter for - // developer's information. Developers can then choose to import and call `isSupported` for - // special handling. - isSwSupported() - .then(isSupported => { - if (!isSupported) { - throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); - } - }) - .catch(_ => { - throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED); - }); - const messaging = new MessagingService( container.getProvider('app').getImmediate(), container.getProvider('installations-internal').getImmediate(),