Skip to content

Commit 8a10e7c

Browse files
author
pinarx
authored
Unify pushSubscription/publicVapidKey retrievals (#587)
Use await/async and unify pushSubscription/publicVapidKey retrievals
1 parent ab0faeb commit 8a10e7c

File tree

1 file changed

+97
-115
lines changed

1 file changed

+97
-115
lines changed

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

Lines changed: 97 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default class ControllerInterface {
6464
/**
6565
* @export
6666
*/
67-
getToken(): Promise<string | null> {
67+
async getToken(): Promise<string | null> {
6868
// Check with permissions
6969
const currentPermission = this.getNotificationPermission_();
7070
if (currentPermission !== NOTIFICATION_PERMISSION.granted) {
@@ -77,18 +77,26 @@ export default class ControllerInterface {
7777
// We must wait for permission to be granted
7878
return Promise.resolve(null);
7979
}
80-
let swReg: ServiceWorkerRegistration;
81-
return this.getSWRegistration_()
82-
.then(reg => {
83-
swReg = reg;
84-
return this.tokenDetailsModel_.getTokenDetailsFromSWScope(swReg.scope);
85-
})
86-
.then(tokenDetails => {
87-
if (tokenDetails) {
88-
return this.manageExistingToken(tokenDetails, swReg);
89-
}
90-
return this.getNewToken(swReg);
91-
});
80+
81+
const swReg = await this.getSWRegistration_();
82+
const publicVapidKey = await this.getPublicVapidKey_();
83+
const pushSubscription = await this.getPushSubscription(
84+
swReg,
85+
publicVapidKey
86+
);
87+
const tokenDetails = await this.tokenDetailsModel_.getTokenDetailsFromSWScope(
88+
swReg.scope
89+
);
90+
91+
if (tokenDetails) {
92+
return this.manageExistingToken(
93+
swReg,
94+
pushSubscription,
95+
publicVapidKey,
96+
tokenDetails
97+
);
98+
}
99+
return this.getNewToken(swReg, pushSubscription, publicVapidKey);
92100
}
93101

94102
/**
@@ -100,26 +108,31 @@ export default class ControllerInterface {
100108
* 3) If the database cache is invalidated: Send a request to FCM to update
101109
* the token, and to check if the token is still valid on FCM-side.
102110
*/
103-
private manageExistingToken(
104-
tokenDetails: Object,
105-
swReg: ServiceWorkerRegistration
111+
private async manageExistingToken(
112+
swReg: ServiceWorkerRegistration,
113+
pushSubscription: PushSubscription,
114+
publicVapidKey: Uint8Array,
115+
tokenDetails: Object
106116
): Promise<string> {
107-
return this.isTokenStillValid(tokenDetails).then(isValid => {
108-
if (isValid) {
109-
const now = Date.now();
110-
if (now < tokenDetails['createTime'] + TOKEN_EXPIRATION_MILLIS) {
111-
return tokenDetails['fcmToken'];
112-
} else {
113-
return this.updateToken(tokenDetails, swReg);
114-
}
117+
const isTokenValid = await this.isTokenStillValid(tokenDetails);
118+
if (isTokenValid) {
119+
const now = Date.now();
120+
if (now < tokenDetails['createTime'] + TOKEN_EXPIRATION_MILLIS) {
121+
return tokenDetails['fcmToken'];
115122
} else {
116-
// If the VAPID details are updated, delete the existing token,
117-
// and create a new one.
118-
return this.deleteToken(tokenDetails['fcmToken']).then(() => {
119-
return this.getNewToken(swReg);
120-
});
123+
return this.updateToken(
124+
swReg,
125+
pushSubscription,
126+
publicVapidKey,
127+
tokenDetails
128+
);
121129
}
122-
});
130+
}
131+
132+
// If the token is no longer valid (for example if the VAPID details
133+
// have changed), delete the existing token, and create a new one.
134+
await this.deleteToken(tokenDetails['fcmToken']);
135+
return this.getNewToken(swReg, pushSubscription, publicVapidKey);
123136
}
124137

125138
/*
@@ -135,94 +148,63 @@ export default class ControllerInterface {
135148
});
136149
}
137150

138-
private updateToken(
139-
tokenDetails: Object,
140-
swReg: ServiceWorkerRegistration
151+
private async updateToken(
152+
swReg: ServiceWorkerRegistration,
153+
pushSubscription: PushSubscription,
154+
publicVapidKey: Uint8Array,
155+
tokenDetails: Object
141156
): Promise<string> {
142-
let publicVapidKey: Uint8Array;
143-
let updatedToken: string;
144-
let subscription: PushSubscription;
145-
return this.getPublicVapidKey_()
146-
.then(publicKey => {
147-
publicVapidKey = publicKey;
148-
return this.getPushSubscription(swReg, publicVapidKey);
149-
})
150-
.then(pushSubscription => {
151-
subscription = pushSubscription;
152-
return this.iidModel_.updateToken(
153-
this.messagingSenderId_,
154-
tokenDetails['fcmToken'],
155-
tokenDetails['fcmPushSet'],
156-
subscription,
157-
publicVapidKey
158-
);
159-
})
160-
.catch(err => {
161-
return this.deleteToken(tokenDetails['fcmToken']).then(() => {
162-
throw err;
163-
});
164-
})
165-
.then(token => {
166-
updatedToken = token;
167-
const allDetails = {
168-
swScope: swReg.scope,
169-
vapidKey: publicVapidKey,
170-
subscription: subscription,
171-
fcmSenderId: this.messagingSenderId_,
172-
fcmToken: updatedToken,
173-
fcmPushSet: tokenDetails['fcmPushSet']
174-
};
175-
return this.tokenDetailsModel_.saveTokenDetails(allDetails);
176-
})
177-
.then(() => {
178-
return this.vapidDetailsModel_.saveVapidDetails(
179-
swReg.scope,
180-
publicVapidKey
181-
);
182-
})
183-
.then(() => {
184-
return updatedToken;
185-
});
157+
try {
158+
const updatedToken = await this.iidModel_.updateToken(
159+
this.messagingSenderId_,
160+
tokenDetails['fcmToken'],
161+
tokenDetails['fcmPushSet'],
162+
pushSubscription,
163+
publicVapidKey
164+
);
165+
166+
const allDetails = {
167+
swScope: swReg.scope,
168+
vapidKey: publicVapidKey,
169+
subscription: pushSubscription,
170+
fcmSenderId: this.messagingSenderId_,
171+
fcmToken: updatedToken,
172+
fcmPushSet: tokenDetails['fcmPushSet']
173+
};
174+
175+
await this.tokenDetailsModel_.saveTokenDetails(allDetails);
176+
await this.vapidDetailsModel_.saveVapidDetails(
177+
swReg.scope,
178+
publicVapidKey
179+
);
180+
return updatedToken;
181+
} catch (e) {
182+
await this.deleteToken(tokenDetails['fcmToken']);
183+
throw e;
184+
}
186185
}
187186

188-
private getNewToken(swReg: ServiceWorkerRegistration): Promise<string> {
189-
let publicVapidKey: Uint8Array;
190-
let subscription: PushSubscription;
191-
let tokenDetails: Object;
192-
return this.getPublicVapidKey_()
193-
.then(publicKey => {
194-
publicVapidKey = publicKey;
195-
return this.getPushSubscription(swReg, publicVapidKey);
196-
})
197-
.then(pushSubscription => {
198-
subscription = pushSubscription;
199-
return this.iidModel_.getToken(
200-
this.messagingSenderId_,
201-
subscription,
202-
publicVapidKey
203-
);
204-
})
205-
.then(iidTokenDetails => {
206-
tokenDetails = iidTokenDetails;
207-
const allDetails = {
208-
swScope: swReg.scope,
209-
vapidKey: publicVapidKey,
210-
subscription: subscription,
211-
fcmSenderId: this.messagingSenderId_,
212-
fcmToken: tokenDetails['token'],
213-
fcmPushSet: tokenDetails['pushSet']
214-
};
215-
return this.tokenDetailsModel_.saveTokenDetails(allDetails);
216-
})
217-
.then(() => {
218-
return this.vapidDetailsModel_.saveVapidDetails(
219-
swReg.scope,
220-
publicVapidKey
221-
);
222-
})
223-
.then(() => {
224-
return tokenDetails['token'];
225-
});
187+
private async getNewToken(
188+
swReg: ServiceWorkerRegistration,
189+
pushSubscription: PushSubscription,
190+
publicVapidKey: Uint8Array
191+
): Promise<string> {
192+
const tokenDetails = await this.iidModel_.getToken(
193+
this.messagingSenderId_,
194+
pushSubscription,
195+
publicVapidKey
196+
);
197+
const allDetails = {
198+
swScope: swReg.scope,
199+
vapidKey: publicVapidKey,
200+
subscription: pushSubscription,
201+
fcmSenderId: this.messagingSenderId_,
202+
fcmToken: tokenDetails['token'],
203+
fcmPushSet: tokenDetails['pushSet']
204+
};
205+
await this.tokenDetailsModel_.saveTokenDetails(allDetails);
206+
await this.vapidDetailsModel_.saveVapidDetails(swReg.scope, publicVapidKey);
207+
return tokenDetails['token'];
226208
}
227209

228210
/**

0 commit comments

Comments
 (0)