Skip to content

Commit c77018d

Browse files
committed
Implement isSupported static method
1 parent 115d9a4 commit c77018d

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

packages/messaging/index.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ import { ERROR_CODES, errorFactory } from './src/models/errors';
2626

2727
import * as types from '@firebase/messaging-types';
2828

29+
export interface MessagingServiceFactory extends FirebaseServiceFactory {
30+
isSupported?(): boolean;
31+
}
32+
2933
export 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
*/

packages/messaging/test/index.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import { sandbox, SinonSandbox, SinonStub } from 'sinon';
2020
import { FirebaseApp } from '@firebase/app-types';
2121
import {
2222
_FirebaseNamespace,
23-
FirebaseServiceFactory
2423
} from '@firebase/app-types/private';
2524

26-
import { registerMessaging } from '../index';
25+
import { MessagingServiceFactory, registerMessaging } from '../index';
2726
import { ERROR_CODES } from '../src/models/errors';
2827

2928
import { SWController } from '../src/controllers/sw-controller';
@@ -54,7 +53,7 @@ describe('Firebase Messaging > registerMessaging', () => {
5453
});
5554

5655
describe('factoryMethod', () => {
57-
let factoryMethod: FirebaseServiceFactory;
56+
let factoryMethod: MessagingServiceFactory;
5857
let fakeApp: FirebaseApp;
5958

6059
beforeEach(() => {
@@ -66,6 +65,12 @@ describe('Firebase Messaging > registerMessaging', () => {
6665
});
6766
});
6867

68+
describe('isSupported', () => {
69+
it('is a static method on factoryMethod', () => {
70+
expect(factoryMethod.isSupported).to.be.a('function');
71+
});
72+
});
73+
6974
describe('in Service Worker context', () => {
7075
beforeEach(() => {
7176
// self.ServiceWorkerGlobalScope exists

0 commit comments

Comments
 (0)