Skip to content

Migrates Listeners Registration from getMessaging to Messaging's Factory Methods 🏭 #4918

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

Merged
merged 8 commits into from
May 21, 2021
Merged
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
3 changes: 3 additions & 0 deletions common/api-review/messaging-exp.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export function onBackgroundMessage(messaging: FirebaseMessaging, nextOrObserver
// @public
export function onMessage(messaging: FirebaseMessaging, nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>): Unsubscribe;

// @internal
export function _registerListeners(messaging: FirebaseMessaging): void;

export { Unsubscribe }


Expand Down
16 changes: 3 additions & 13 deletions packages-exp/messaging-compat/src/messaging-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {
FirebaseMessaging,
MessagePayload,
_registerListeners,
deleteToken,
getToken,
onBackgroundMessage,
Expand Down Expand Up @@ -88,22 +89,11 @@ function isSwSupported(): boolean {
}

export class MessagingCompatImpl implements MessagingCompat, _FirebaseService {
swRegistration?: ServiceWorkerRegistration;
vapidKey?: string;

onBackgroundMessageHandler:
| NextFn<MessagePayload>
| Observer<MessagePayload>
| null = null;

onMessageHandler:
| NextFn<MessagePayload>
| Observer<MessagePayload>
| null = null;

constructor(readonly app: AppCompat, readonly _delegate: FirebaseMessaging) {
this.app = app;
this._delegate = _delegate;

_registerListeners(_delegate);
}

async getToken(options?: {
Expand Down
37 changes: 37 additions & 0 deletions packages-exp/messaging-exp/src/helpers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ import {
} from '@firebase/component';
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
import { isSwSupported, isWindowSupported } from '../api/isSupported';
import {
onNotificationClick,
onPush,
onSubChange
} from '../listeners/sw-listeners';

import { FirebaseMessaging } from '../interfaces/public-types';
import { MessagingService } from '../messaging-service';
import { ServiceWorkerGlobalScope } from '../util/sw-types';
import { _registerComponent } from '@firebase/app-exp';
import { messageEventListener } from '../listeners/window-listener';

const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
container: ComponentContainer
Expand Down Expand Up @@ -86,3 +94,32 @@ export function registerMessagingInSw(): void {
new Component('messaging-exp', SwMessagingFactory, ComponentType.PUBLIC)
);
}

declare const self: ServiceWorkerGlobalScope;
/**
* Conditionally registers the listeners. Theses registrations are done in `getMessagingInSw` and
* `getMessagingInWindow` for the v9 SDK. This method exists for `messaging-compat` because the
* injected messaging instance to `messaging-compat` isn't created through the `getMessaging`
* method. Thus the main package of v9 needs to export this method to support `messaging-compat`.
*
* @internal
*/
export function _registerListeners(messaging: FirebaseMessaging): void {
if (!!navigator) {
// in window
navigator.serviceWorker.addEventListener('message', e =>
messageEventListener(messaging as MessagingService, e)
);
} else {
// in sw
self.addEventListener('push', e => {
e.waitUntil(onPush(e, messaging as MessagingService));
});
self.addEventListener('pushsubscriptionchange', e => {
e.waitUntil(onSubChange(e, messaging as MessagingService));
});
self.addEventListener('notificationclick', e => {
e.waitUntil(onNotificationClick(e));
});
}
}
1 change: 1 addition & 0 deletions packages-exp/messaging-exp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {
getMessagingInWindow as getMessaging,
onBackgroundMessage
} from './api';
export { _registerListeners } from './helpers/register';
export { isWindowSupported as isSupported } from './api/isSupported';
export * from './interfaces/public-types';

Expand Down