|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import { getToken, deleteToken } from '../core/token-management'; |
19 |
| -import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies'; |
20 |
| -import { FirebaseMessaging } from '@firebase/messaging-types'; |
21 |
| -import { ERROR_FACTORY, ErrorCode } from '../util/errors'; |
22 |
| -import { NextFn, Observer, Unsubscribe } from '@firebase/util'; |
23 |
| -import { InternalMessage, MessageType } from '../interfaces/internal-message'; |
24 | 18 | import {
|
25 |
| - CONSOLE_CAMPAIGN_ID, |
26 | 19 | CONSOLE_CAMPAIGN_ANALYTICS_ENABLED,
|
| 20 | + CONSOLE_CAMPAIGN_ID, |
27 | 21 | CONSOLE_CAMPAIGN_NAME,
|
28 | 22 | CONSOLE_CAMPAIGN_TIME,
|
29 | 23 | DEFAULT_SW_PATH,
|
30 | 24 | DEFAULT_SW_SCOPE,
|
31 | 25 | DEFAULT_VAPID_KEY
|
32 | 26 | } from '../util/constants';
|
33 |
| -import { FirebaseApp } from '@firebase/app-types'; |
| 27 | +import { ERROR_FACTORY, ErrorCode } from '../util/errors'; |
| 28 | +import { InternalMessage, MessageType } from '../interfaces/internal-message'; |
| 29 | +import { NextFn, Observer, Unsubscribe } from '@firebase/util'; |
| 30 | +import { deleteToken, getToken } from '../core/token-management'; |
| 31 | + |
34 | 32 | import { ConsoleMessageData } from '../interfaces/message-payload';
|
35 |
| -import { isConsoleMessage } from '../helpers/is-console-message'; |
| 33 | +import { FirebaseApp } from '@firebase/app-types'; |
| 34 | +import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies'; |
| 35 | +import { FirebaseMessaging } from '@firebase/messaging-types'; |
36 | 36 | import { FirebaseService } from '@firebase/app-types/private';
|
| 37 | +import { isConsoleMessage } from '../helpers/is-console-message'; |
37 | 38 |
|
38 | 39 | export class WindowController implements FirebaseMessaging, FirebaseService {
|
39 | 40 | private vapidKey: string | null = null;
|
40 | 41 | private swRegistration?: ServiceWorkerRegistration;
|
41 |
| - private onMessageCallback: NextFn<object> | null = null; |
| 42 | + private onMessageCallback: NextFn<object> | Observer<object> | null = null; |
42 | 43 |
|
43 | 44 | constructor(
|
44 | 45 | private readonly firebaseDependencies: FirebaseInternalDependencies
|
@@ -131,12 +132,8 @@ export class WindowController implements FirebaseMessaging, FirebaseService {
|
131 | 132 | * message.
|
132 | 133 | * @return The unsubscribe function for the observer.
|
133 | 134 | */
|
134 |
| - // TODO: Simplify this to only accept a function and not an Observer. |
135 | 135 | onMessage(nextOrObserver: NextFn<object> | Observer<object>): Unsubscribe {
|
136 |
| - this.onMessageCallback = |
137 |
| - typeof nextOrObserver === 'function' |
138 |
| - ? nextOrObserver |
139 |
| - : nextOrObserver.next; |
| 136 | + this.onMessageCallback = nextOrObserver; |
140 | 137 |
|
141 | 138 | return () => {
|
142 | 139 | this.onMessageCallback = null;
|
@@ -193,8 +190,13 @@ export class WindowController implements FirebaseMessaging, FirebaseService {
|
193 | 190 |
|
194 | 191 | const { type, payload } = (event.data as InternalMessage).firebaseMessaging;
|
195 | 192 |
|
| 193 | + // onMessageCallback is either a function or observer/subscriber. |
196 | 194 | if (this.onMessageCallback && type === MessageType.PUSH_RECEIVED) {
|
197 |
| - this.onMessageCallback(payload); |
| 195 | + if (typeof this.onMessageCallback === 'function') { |
| 196 | + this.onMessageCallback(payload); |
| 197 | + } else { |
| 198 | + this.onMessageCallback.next(payload); |
| 199 | + } |
198 | 200 | }
|
199 | 201 |
|
200 | 202 | const { data } = payload;
|
|
0 commit comments