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 5 commits
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
16 changes: 2 additions & 14 deletions packages-exp/messaging-compat/src/messaging-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import {
MessagePayload,
deleteToken,
getToken,
onBackgroundMessage,
onMessage
} from '@firebase/messaging-exp';
import { NextFn, Observer, Unsubscribe } from '@firebase/util';

import { onBackgroundMessage } from '@firebase/messaging-exp/sw';

export interface MessagingCompat {
getToken(options?: {
vapidKey?: string;
Expand Down Expand Up @@ -88,19 +89,6 @@ 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;
Expand Down
18 changes: 14 additions & 4 deletions packages-exp/messaging-compat/src/registerMessagingCompat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
InstanceFactory
} from '@firebase/component';
import firebase, { _FirebaseNamespace } from '@firebase/app-compat';

import { MessagingCompatImpl } from './messaging-compat';

declare module '@firebase/component' {
Expand All @@ -33,10 +34,19 @@ declare module '@firebase/component' {
const messagingCompatFactory: InstanceFactory<'messaging-compat'> = (
container: ComponentContainer
) => {
return new MessagingCompatImpl(
container.getProvider('app-compat').getImmediate(),
container.getProvider('messaging-exp').getImmediate()
);
if (!!navigator) {
// in window
return new MessagingCompatImpl(
container.getProvider('app-compat').getImmediate(),
container.getProvider('messaging-exp').getImmediate()
);
} else {
// in sw
return new MessagingCompatImpl(
container.getProvider('app-compat').getImmediate(),
container.getProvider('messaging-sw-exp').getImmediate()
);
}
};

export function registerMessagingCompat(): void {
Expand Down
52 changes: 11 additions & 41 deletions packages-exp/messaging-exp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,12 @@ import {
Unsubscribe,
getModularInstance
} from '@firebase/util';
import {
onNotificationClick,
onPush,
onSubChange
} from './listeners/sw-listeners';

import { MessagingService } from './messaging-service';
import { Provider } from '@firebase/component';
import { ServiceWorkerGlobalScope } from './util/sw-types';
import { deleteToken as _deleteToken } from './api/deleteToken';
import { getToken as _getToken } from './api/getToken';
import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage';
import { onMessage as _onMessage } from './api/onMessage';
import { messageEventListener } from './listeners/window-listener';

/**
* Retrieves a Firebase Cloud Messaging instance.
Expand All @@ -48,45 +40,23 @@ import { messageEventListener } from './listeners/window-listener';
export function getMessagingInWindow(
app: FirebaseApp = getApp()
): FirebaseMessaging {
app = getModularInstance(app);
const messagingProvider: Provider<'messaging-exp'> = _getProvider(
app,
'messaging-exp'
);
const messaging = messagingProvider.getImmediate();

navigator.serviceWorker.addEventListener('message', e =>
messageEventListener(messaging as MessagingService, e)
);

return messaging;
return _getProvider(getModularInstance(app), 'messaging-exp').getImmediate();
}

/**
* Retrieves a firebase messaging instance.
* Retrieves a Firebase Cloud Messaging instance.
*
* @returns the firebase messaging instance associated with the provided firebase app.
* @returns The Firebase Cloud Messaging instance associated with the provided firebase app.
*
* @public
*/
declare const self: ServiceWorkerGlobalScope;
export function getMessagingInSw(app: FirebaseApp): FirebaseMessaging {
const messagingProvider: Provider<'messaging-exp'> = _getProvider(
app,
'messaging-exp'
);
const messaging = messagingProvider.getImmediate();

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));
});

return messagingProvider.getImmediate();
export function getMessagingInSw(
app: FirebaseApp = getApp()
): FirebaseMessaging {
return _getProvider(
getModularInstance(app),
'messaging-sw-exp'
).getImmediate();
}

/**
Expand Down
37 changes: 34 additions & 3 deletions packages-exp/messaging-exp/src/helpers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ 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 { 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 All @@ -44,13 +51,20 @@ const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
});

return new MessagingService(
const messaging = new MessagingService(
container.getProvider('app-exp').getImmediate(),
container.getProvider('installations-exp-internal').getImmediate(),
container.getProvider('analytics-internal')
);

navigator.serviceWorker.addEventListener('message', e =>
messageEventListener(messaging as MessagingService, e)
);

return messaging;
};

declare const self: ServiceWorkerGlobalScope;
const SwMessagingFactory: InstanceFactory<'messaging-exp'> = (
container: ComponentContainer
) => {
Expand All @@ -68,11 +82,23 @@ const SwMessagingFactory: InstanceFactory<'messaging-exp'> = (
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
});

return new MessagingService(
const messaging = new MessagingService(
container.getProvider('app-exp').getImmediate(),
container.getProvider('installations-exp-internal').getImmediate(),
container.getProvider('analytics-internal')
);

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));
});

return messaging;
};

export function registerMessagingInWindow(): void {
Expand All @@ -81,8 +107,13 @@ export function registerMessagingInWindow(): void {
);
}

/**
* The messaging instance registered in sw is named differently than that of in client. This is
* because both `registerMessagingInWindow` and `registerMessagingInSw` would be called in
* `messaging-compat` and component with the same name can only be registered once.
*/
export function registerMessagingInSw(): void {
_registerComponent(
new Component('messaging-exp', SwMessagingFactory, ComponentType.PUBLIC)
new Component('messaging-sw-exp', SwMessagingFactory, ComponentType.PUBLIC)
);
}
2 changes: 1 addition & 1 deletion packages-exp/messaging-exp/src/index.sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export { isSwSupported as isSupported } from './api/isSupported';

declare module '@firebase/component' {
interface NameServiceMapping {
'messaging-exp': FirebaseMessaging;
'messaging-sw-exp': FirebaseMessaging;
}
}

Expand Down