Skip to content

Commit 8786a49

Browse files
committed
Remove deprecated callback API
1 parent 59486d1 commit 8786a49

File tree

2 files changed

+9
-38
lines changed

2 files changed

+9
-38
lines changed

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -132,37 +132,18 @@ export class WindowController extends ControllerInterface {
132132
* @return Resolves if the permission was granted, otherwise rejects
133133
*/
134134
async requestPermission(): Promise<void> {
135-
if (
136-
// TODO: Remove the cast when this issue is fixed:
137-
// https://github.com/Microsoft/TypeScript/issues/14701
138-
// tslint:disable-next-line no-any
139-
((Notification as any).permission as NotificationPermission) === 'granted'
140-
) {
135+
if (this.getNotificationPermission_() === 'granted') {
141136
return;
142137
}
143138

144-
return new Promise<void>((resolve, reject) => {
145-
const managePermissionResult = (result: NotificationPermission) => {
146-
if (result === 'granted') {
147-
return resolve();
148-
} else if (result === 'denied') {
149-
return reject(errorFactory.create(ERROR_CODES.PERMISSION_BLOCKED));
150-
} else {
151-
return reject(errorFactory.create(ERROR_CODES.PERMISSION_DEFAULT));
152-
}
153-
};
154-
155-
// The Notification.requestPermission API was changed to
156-
// return a promise so now have to handle both in case
157-
// browsers stop support callbacks for promised version
158-
const permissionPromise = Notification.requestPermission(
159-
managePermissionResult
160-
);
161-
if (permissionPromise) {
162-
// Prefer the promise version as it's the future API.
163-
permissionPromise.then(managePermissionResult);
164-
}
165-
});
139+
const permissionResult = await Notification.requestPermission();
140+
if (permissionResult === 'granted') {
141+
return;
142+
} else if (permissionResult === 'denied') {
143+
throw errorFactory.create(ERROR_CODES.PERMISSION_BLOCKED);
144+
} else {
145+
throw errorFactory.create(ERROR_CODES.PERMISSION_DEFAULT);
146+
}
166147
}
167148

168149
/**

packages/messaging/test/window-controller.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,6 @@ describe('Firebase Messaging > *WindowController', () => {
204204
const controller = new WindowController(app);
205205
return controller.requestPermission();
206206
});
207-
208-
it('should resolve if the requestPermission() is granted using old callback API', () => {
209-
sandbox.stub(Notification as any, 'permission').value('default');
210-
sandbox.stub(Notification, 'requestPermission').callsFake(cb => {
211-
cb('granted');
212-
});
213-
214-
const controller = new WindowController(app);
215-
return controller.requestPermission();
216-
});
217207
});
218208

219209
describe('useServiceWorker()', () => {

0 commit comments

Comments
 (0)