@@ -26,23 +26,27 @@ import { ERROR_CODES, errorFactory } from './src/models/errors';
2626
2727import * as types from '@firebase/messaging-types' ;
2828
29+ export interface MessagingServiceFactory extends FirebaseServiceFactory {
30+ isSupported ?( ) : boolean ;
31+ }
32+
2933export function registerMessaging ( instance : _FirebaseNamespace ) : void {
3034 const messagingName = 'messaging' ;
31- const factoryMethod : FirebaseServiceFactory = app => {
35+
36+ const factoryMethod : MessagingServiceFactory = app => {
37+ if ( ! isSupported ( ) ) {
38+ throw errorFactory . create ( ERROR_CODES . UNSUPPORTED_BROWSER ) ;
39+ }
40+
3241 if ( self && 'ServiceWorkerGlobalScope' in self ) {
3342 // Running in ServiceWorker context
34- if ( isSWControllerSupported ( ) ) {
35- return new SWController ( app ) ;
36- }
43+ return new SWController ( app ) ;
3744 } else {
3845 // Assume we are in the window context.
39- if ( isWindowControllerSupported ( ) ) {
40- return new WindowController ( app ) ;
41- }
46+ return new WindowController ( app ) ;
4247 }
43-
44- throw errorFactory . create ( ERROR_CODES . UNSUPPORTED_BROWSER ) ;
4548 } ;
49+ factoryMethod . isSupported = isSupported ;
4650
4751 const namespaceExports = {
4852 // no-inline
@@ -73,6 +77,16 @@ declare module '@firebase/app-types' {
7377 }
7478}
7579
80+ function isSupported ( ) : boolean {
81+ if ( self && 'ServiceWorkerGlobalScope' in self ) {
82+ // Running in ServiceWorker context
83+ return isSWControllerSupported ( ) ;
84+ } else {
85+ // Assume we are in the window context.
86+ return isWindowControllerSupported ( ) ;
87+ }
88+ }
89+
7690/**
7791 * Checks to see if the required APIs exist.
7892 */
0 commit comments