Skip to content

[Deprecated] Separate window/sw API checks #4446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions packages-exp/messaging-exp/src/helpers/isSupported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,10 @@
* limitations under the License.
*/

export function isSupported(): boolean {
if (self && 'ServiceWorkerGlobalScope' in self) {
// Running in ServiceWorker context
return isSWControllerSupported();
} else {
// Assume we are in the window context.
return isWindowControllerSupported();
}
}

/**
* Checks to see if the required APIs exist.
*/
function isWindowControllerSupported(): boolean {
export function isWindowSupported(): boolean {
return (
'indexedDB' in window &&
indexedDB !== null &&
Expand All @@ -45,7 +35,7 @@ function isWindowControllerSupported(): boolean {
/**
* Checks to see if the required APIs exist within SW Context.
*/
function isSWControllerSupported(): boolean {
export function isSwSupported(): boolean {
return (
'indexedDB' in self &&
indexedDB !== null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@ import {
ComponentType,
InstanceFactory
} from '@firebase/component';
import { ERROR_FACTORY, ErrorCode } from '../util/errors';

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

const messagingFactory: InstanceFactory<'messaging-exp'> = (
container: ComponentContainer
) => {
if (!isSupported()) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}

return new MessagingService(
container.getProvider('app-exp').getImmediate(),
container.getProvider('installations-exp-internal').getImmediate(),
Expand Down
12 changes: 10 additions & 2 deletions packages-exp/messaging-exp/src/index.sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
* limitations under the License.
*/

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

import { ERROR_FACTORY, ErrorCode } from './util/errors';

import { FirebaseMessaging } from '@firebase/messaging-types-exp';
import { isSwSupported } from './helpers/isSupported';
import { registerMessaging } from './helpers/registerMessaging';

export { onBackgroundMessage, getMessaging } from './api';

declare module '@firebase/component' {
Expand All @@ -27,4 +31,8 @@ declare module '@firebase/component' {
}
}

if (!isSwSupported()) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}

registerMessaging();
12 changes: 10 additions & 2 deletions packages-exp/messaging-exp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
* limitations under the License.
*/

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

import { ERROR_FACTORY, ErrorCode } from './util/errors';

import { FirebaseMessaging } from '@firebase/messaging-types-exp';
import { isWindowSupported } from './helpers/isSupported';
import { registerMessaging } from './helpers/registerMessaging';

export {
getToken,
deleteToken,
Expand All @@ -34,4 +38,8 @@ declare module '@firebase/component' {
}
}

if (!isWindowSupported()) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}

registerMessaging();