Skip to content

Commit 68e2827

Browse files
committed
Remove deprecated callback API
1 parent e3c4fb5 commit 68e2827

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
@@ -129,37 +129,18 @@ export class WindowController extends ControllerInterface {
129129
* @return Resolves if the permission was granted, otherwise rejects
130130
*/
131131
async requestPermission(): Promise<void> {
132-
if (
133-
// TODO: Remove the cast when this issue is fixed:
134-
// https://github.com/Microsoft/TypeScript/issues/14701
135-
// tslint:disable-next-line no-any
136-
((Notification as any).permission as NotificationPermission) === 'granted'
137-
) {
132+
if (this.getNotificationPermission_() === 'granted') {
138133
return;
139134
}
140135

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

165146
/**

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

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

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

0 commit comments

Comments
 (0)