Skip to content

Commit 4f28096

Browse files
author
pinarx
authored
Fix a bug that was introduced in PR: #591 (#614)
* Fix a bug that was introduced in PR: #591 * [AUTOMATED]: Prettier Code Styling * rewording and reordering functions to adhere to comments.
1 parent 790c158 commit 4f28096

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

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

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export default class ControllerInterface {
8080

8181
const swReg = await this.getSWRegistration_();
8282
const publicVapidKey = await this.getPublicVapidKey_();
83+
// If a PushSubscription exists it's returned, otherwise a new subscription
84+
// is generated and returned.
8385
const pushSubscription = await this.getPushSubscription(
8486
swReg,
8587
publicVapidKey
@@ -134,8 +136,10 @@ export default class ControllerInterface {
134136
}
135137

136138
// If the token is no longer valid (for example if the VAPID details
137-
// have changed), delete the existing token, and create a new one.
138-
await this.deleteToken(tokenDetails['fcmToken']);
139+
// have changed), delete the existing token from the FCM client and server
140+
// database. No need to unsubscribe from the Service Worker as we have a
141+
// good push subscription that we'd like to use in getNewToken.
142+
await this.deleteTokenFromDB(tokenDetails['fcmToken']);
139143
return this.getNewToken(swReg, pushSubscription, publicVapidKey);
140144
}
141145

@@ -228,29 +232,33 @@ export default class ControllerInterface {
228232
* whether or not the unsubscribe request was processed successfully.
229233
* @export
230234
*/
231-
deleteToken(token: string): Promise<Boolean> {
232-
return this.tokenDetailsModel_
233-
.deleteToken(token)
234-
.then(details => {
235-
return this.iidModel_.deleteToken(
236-
details['fcmSenderId'],
237-
details['fcmToken'],
238-
details['fcmPushSet']
239-
);
240-
})
241-
.then(() => {
242-
return this.getSWRegistration_()
243-
.then(registration => {
244-
if (registration) {
245-
return registration.pushManager.getSubscription();
246-
}
247-
})
248-
.then(subscription => {
249-
if (subscription) {
250-
return subscription.unsubscribe();
251-
}
252-
});
253-
});
235+
async deleteToken(token: string): Promise<Boolean> {
236+
// Delete the token details from the database.
237+
await this.deleteTokenFromDB(token);
238+
// Unsubscribe from the SW.
239+
const registration = await this.getSWRegistration_();
240+
if (registration) {
241+
const pushSubscription = await registration.pushManager.getSubscription();
242+
if (pushSubscription) {
243+
return pushSubscription.unsubscribe();
244+
}
245+
}
246+
// If there's no SW, consider it a success.
247+
return true;
248+
}
249+
250+
/**
251+
* This method will delete the token from the client database, and make a
252+
* call to FCM to remove it from the server DB. Does not temper with the
253+
* push subscription.
254+
*/
255+
private async deleteTokenFromDB(token: string): Promise<void> {
256+
const details = await this.tokenDetailsModel_.deleteToken(token);
257+
await this.iidModel_.deleteToken(
258+
details['fcmSenderId'],
259+
details['fcmToken'],
260+
details['fcmPushSet']
261+
);
254262
}
255263

256264
getSWRegistration_(): Promise<ServiceWorkerRegistration> {
@@ -261,14 +269,6 @@ export default class ControllerInterface {
261269
throw this.errorFactory_.create(Errors.codes.SHOULD_BE_INHERITED);
262270
}
263271

264-
//
265-
// The following methods should only be available in the window.
266-
//
267-
268-
requestPermission() {
269-
throw this.errorFactory_.create(Errors.codes.AVAILABLE_IN_WINDOW);
270-
}
271-
272272
/**
273273
* Gets a PushSubscription for the current user.
274274
*/
@@ -288,6 +288,14 @@ export default class ControllerInterface {
288288
});
289289
}
290290

291+
//
292+
// The following methods should only be available in the window.
293+
//
294+
295+
requestPermission() {
296+
throw this.errorFactory_.create(Errors.codes.AVAILABLE_IN_WINDOW);
297+
}
298+
291299
/**
292300
* @export
293301
* @param {!ServiceWorkerRegistration} registration

0 commit comments

Comments
 (0)