Skip to content

Commit f9d66e3

Browse files
committed
Remove deprecated callback API
1 parent b0baf8d commit f9d66e3

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

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

163144
/**

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)