Skip to content

Commit 59486d1

Browse files
committed
Some more async/await
1 parent 4b4eebc commit 59486d1

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

packages/messaging/src/controllers/controller-interface.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,11 @@ export abstract class ControllerInterface implements FirebaseMessaging {
7070
async getToken(): Promise<string | null> {
7171
// Check with permissions
7272
const currentPermission = this.getNotificationPermission_();
73-
if (currentPermission !== 'granted') {
74-
if (currentPermission === 'denied') {
75-
return Promise.reject(
76-
errorFactory.create(ERROR_CODES.NOTIFICATIONS_BLOCKED)
77-
);
78-
}
79-
73+
if (currentPermission === 'denied') {
74+
throw errorFactory.create(ERROR_CODES.NOTIFICATIONS_BLOCKED);
75+
} else if (currentPermission !== 'granted') {
8076
// We must wait for permission to be granted
81-
return Promise.resolve(null);
77+
return null;
8278
}
8379

8480
const swReg = await this.getSWRegistration_();
@@ -216,7 +212,6 @@ export abstract class ControllerInterface implements FirebaseMessaging {
216212
* unsubscribes the token from FCM and then unregisters the push
217213
* subscription if it exists. It returns a promise that indicates
218214
* whether or not the unsubscribe request was processed successfully.
219-
* @export
220215
*/
221216
async deleteToken(token: string): Promise<boolean> {
222217
// Delete the token details from the database.

packages/messaging/src/controllers/window-controller.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import {
3434
} from '../models/worker-page-message';
3535
import { ControllerInterface } from './controller-interface';
3636

37+
interface ManifestContent {
38+
gcm_sender_id: string;
39+
}
40+
3741
export class WindowController extends ControllerInterface {
3842
private registrationToUse: ServiceWorkerRegistration | null = null;
3943
private publicVapidKeyToUse: Uint8Array | null = null;
@@ -73,17 +77,17 @@ export class WindowController extends ControllerInterface {
7377
* @return Returns a promise that resolves to an FCM token or null if
7478
* permission isn't granted.
7579
*/
76-
getToken(): Promise<string | null> {
80+
async getToken(): Promise<string | null> {
7781
// Check that the required API's are available
7882
if (!this.isSupported_()) {
79-
return Promise.reject(
80-
errorFactory.create(ERROR_CODES.UNSUPPORTED_BROWSER)
81-
);
83+
throw errorFactory.create(ERROR_CODES.UNSUPPORTED_BROWSER);
8284
}
8385

84-
return this.manifestCheck_().then(() => {
85-
return super.getToken();
86-
});
86+
if (!this.manifestCheckPromise) {
87+
this.manifestCheckPromise = this.manifestCheck_();
88+
}
89+
await this.manifestCheckPromise;
90+
return super.getToken();
8791
}
8892

8993
/**
@@ -94,41 +98,32 @@ export class WindowController extends ControllerInterface {
9498
*/
9599
// Visible for testing
96100
// TODO: make private
97-
manifestCheck_(): Promise<void> {
98-
if (this.manifestCheckPromise) {
99-
return this.manifestCheckPromise;
100-
}
101-
101+
async manifestCheck_(): Promise<void> {
102102
const manifestTag = document.querySelector<HTMLAnchorElement>(
103103
'link[rel="manifest"]'
104104
);
105+
105106
if (!manifestTag) {
106-
this.manifestCheckPromise = Promise.resolve();
107-
} else {
108-
this.manifestCheckPromise = fetch(manifestTag.href)
109-
.then(response => {
110-
return response.json();
111-
})
112-
.catch(() => {
113-
// If the download or parsing fails allow check.
114-
// We only want to error if we KNOW that the gcm_sender_id is incorrect.
115-
})
116-
.then(manifestContent => {
117-
if (!manifestContent) {
118-
return;
119-
}
120-
121-
if (!manifestContent['gcm_sender_id']) {
122-
return;
123-
}
124-
125-
if (manifestContent['gcm_sender_id'] !== '103953800507') {
126-
throw errorFactory.create(ERROR_CODES.INCORRECT_GCM_SENDER_ID);
127-
}
128-
});
107+
return;
108+
}
109+
110+
let manifestContent: ManifestContent;
111+
try {
112+
const response = await fetch(manifestTag.href);
113+
manifestContent = await response.json();
114+
} catch (e) {
115+
// If the download or parsing fails allow check.
116+
// We only want to error if we KNOW that the gcm_sender_id is incorrect.
117+
return;
129118
}
130119

131-
return this.manifestCheckPromise;
120+
if (!manifestContent || !manifestContent.gcm_sender_id) {
121+
return;
122+
}
123+
124+
if (manifestContent.gcm_sender_id !== '103953800507') {
125+
throw errorFactory.create(ERROR_CODES.INCORRECT_GCM_SENDER_ID);
126+
}
132127
}
133128

134129
/**
@@ -339,12 +334,12 @@ export class WindowController extends ControllerInterface {
339334
* This will return the default VAPID key or the uint8array version of the public VAPID key
340335
* provided by the developer.
341336
*/
342-
getPublicVapidKey_(): Promise<Uint8Array> {
337+
async getPublicVapidKey_(): Promise<Uint8Array> {
343338
if (this.publicVapidKeyToUse) {
344-
return Promise.resolve(this.publicVapidKeyToUse);
339+
return this.publicVapidKeyToUse;
345340
}
346341

347-
return Promise.resolve(DEFAULT_PUBLIC_VAPID_KEY);
342+
return DEFAULT_PUBLIC_VAPID_KEY;
348343
}
349344

350345
/**

0 commit comments

Comments
 (0)