Skip to content

Commit 5d3b4cf

Browse files
committed
Separate window/sw API checks
1 parent 4986a3e commit 5d3b4cf

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

packages-exp/messaging-exp/src/helpers/isSupported.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
export function isSupported(): boolean {
19-
if (self && 'ServiceWorkerGlobalScope' in self) {
20-
// Running in ServiceWorker context
21-
return isSWControllerSupported();
22-
} else {
23-
// Assume we are in the window context.
24-
return isWindowControllerSupported();
25-
}
26-
}
27-
2818
/**
2919
* Checks to see if the required APIs exist.
3020
*/
31-
function isWindowControllerSupported(): boolean {
21+
export function isWindowSupported(): boolean {
3222
return (
3323
'indexedDB' in window &&
3424
indexedDB !== null &&
@@ -45,7 +35,7 @@ function isWindowControllerSupported(): boolean {
4535
/**
4636
* Checks to see if the required APIs exist within SW Context.
4737
*/
48-
function isSWControllerSupported(): boolean {
38+
export function isSwSupported(): boolean {
4939
return (
5040
'indexedDB' in self &&
5141
indexedDB !== null &&

packages-exp/messaging-exp/src/helpers/register.ts renamed to packages-exp/messaging-exp/src/helpers/registerMessaging.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,13 @@ import {
2121
ComponentType,
2222
InstanceFactory
2323
} from '@firebase/component';
24-
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
2524

2625
import { MessagingService } from '../messaging-service';
2726
import { _registerComponent } from '@firebase/app-exp';
28-
import { isSupported } from './isSupported';
2927

3028
const messagingFactory: InstanceFactory<'messaging-exp'> = (
3129
container: ComponentContainer
3230
) => {
33-
if (!isSupported()) {
34-
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
35-
}
36-
3731
return new MessagingService(
3832
container.getProvider('app-exp').getImmediate(),
3933
container.getProvider('installations-exp-internal').getImmediate(),

packages-exp/messaging-exp/src/index.sw.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseMessaging } from '@firebase/messaging-types-exp';
19-
import { registerMessaging } from './helpers/register';
2018
import '@firebase/installations-exp';
2119

20+
import { ERROR_FACTORY, ErrorCode } from './util/errors';
21+
22+
import { FirebaseMessaging } from '@firebase/messaging-types-exp';
23+
import { isSwSupported } from './helpers/isSupported';
24+
import { registerMessaging } from './helpers/registerMessaging';
25+
2226
export { onBackgroundMessage, getMessaging } from './api';
2327

2428
declare module '@firebase/component' {
@@ -27,4 +31,8 @@ declare module '@firebase/component' {
2731
}
2832
}
2933

34+
if (!isSwSupported()) {
35+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
36+
}
37+
3038
registerMessaging();

packages-exp/messaging-exp/src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseMessaging } from '@firebase/messaging-types-exp';
19-
import { registerMessaging } from './helpers/register';
2018
import '@firebase/installations-exp';
2119

20+
import { ERROR_FACTORY, ErrorCode } from './util/errors';
21+
22+
import { FirebaseMessaging } from '@firebase/messaging-types-exp';
23+
import { isWindowSupported } from './helpers/isSupported';
24+
import { registerMessaging } from './helpers/registerMessaging';
25+
2226
export {
2327
getToken,
2428
deleteToken,
@@ -34,4 +38,8 @@ declare module '@firebase/component' {
3438
}
3539
}
3640

41+
if (!isWindowSupported()) {
42+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
43+
}
44+
3745
registerMessaging();

0 commit comments

Comments
 (0)