Skip to content
This repository was archived by the owner on Jul 8, 2024. It is now read-only.

Commit 4c146ab

Browse files
joshbeckmantheLufenk
authored andcommitted
fix(requestPermission): pass resolution callback
1) Safari doesn't return a promise when requestPermission is invoked. This was resulting in an error and the supplied resolution callback function was never called. 2) Additionally, Node v10 was promoted to LTS causing Travis CI to use it, resulting in a known bug: nodejs/node#20325 . This was fixed by specifying node v8 in travis config
1 parent ad7b003 commit 4c146ab

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
- secure: AwJJp7im+vXzxa/UsYu/EJNppu+ss9l0GW8ftaXLNnMRkLJyo6m24DCJuhfH/ynxrSq5b2Zc7gEqDfxVWUgpNR+rrHZoHe+R/QnL60sqQXE2KvblDvG4o4e9F7vFp1xsohJ3/TTm6footWEiVlP25oVkQgi/oWhFsygJD6VGerDa2CodtU2r4p6UV5KuI8mUZuQg+rndkosMZa1BkZcz6v8e1AmJkGiIl/Agw0ye6C9iav4KoU+EXRyoxEq+dTAuhnVKA3CntOngoPDrUTQy5303x6Gzaz7uByWIyIR+uEud65RvCBduxCgREazkXdCqd/vCS65gYYktxl3fNG2so5VpKAbF/pTOylWexB10xhB+k+alIxhl3QytUx1pqDR4WI6c8d6ot+sZd7AjwvlyWdwDPuLoDB2eA18K3HbFMgjONmJFFI3gyKfyg1z5FfZm6dogfuQGZeSyxuedDAo+FygKGbgvBa+JHerR1WjU+TnFcVpwgaX/sma8Q4ff9WYLw2YOIiSi0H5V5tDi8lrtOqZmIYH4Vv2Cbl1gaAsVOOo+IBfTWor4oJzAb/jRKuNbqvhUJIqW6RYS7f8c/LMrcdn+LWHGj3zcFQ+LNFxID4OCghf7A3FvmE8A5XD07w04ofnWzrgqzgWy5+38Qj23l5IpFNgD2XGNohk7JpeGm3E=
77
language: node_js
88
node_js:
9-
- lts/*
9+
- "8.12"
1010
before_install: yarn global add greenkeeper-lockfile@1
1111
before_script:
1212
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64

src/push/Permission.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ export default class Permission {
4141
_requestWithCallback(onGranted: () => void, onDenied: () => void) {
4242
const existing = this.get();
4343

44+
var resolved = false;
4445
var resolve = (result = this._win.Notification.permission) => {
46+
if (resolved) return;
47+
resolved = true;
4548
if (typeof result === 'undefined' && this._win.webkitNotifications)
4649
result = this._win.webkitNotifications.checkPermission();
4750
if (result === this.GRANTED || result === 0) {
4851
if (onGranted) onGranted();
4952
} else if (onDenied) onDenied();
5053
};
54+
var request;
5155

5256
/* Permissions already set */
5357
if (existing !== this.DEFAULT) {
@@ -62,13 +66,16 @@ export default class Permission {
6266
this._win.Notification &&
6367
this._win.Notification.requestPermission
6468
) {
65-
/* Chrome 23+ */
66-
this._win.Notification
67-
.requestPermission()
68-
.then(resolve)
69-
.catch(function() {
69+
/* Safari 12+ */
70+
/* This resolve argument will only be used in Safari */
71+
/* CHrome, instead, returns a Promise */
72+
request = this._win.Notification.requestPermission(resolve);
73+
if (request && request.then) {
74+
/* Chrome 23+ */
75+
request.then(resolve).catch(function() {
7076
if (onDenied) onDenied();
7177
});
78+
}
7279
} else if (onGranted) {
7380
/* Let the user continue by default */
7481
onGranted();
@@ -97,8 +104,13 @@ export default class Permission {
97104
this._win.webkitNotifications.checkPermission;
98105

99106
return new Promise((resolvePromise, rejectPromise) => {
100-
var resolver = result =>
107+
var resolved = false;
108+
var resolver = result => {
109+
if (resolved) return;
110+
resolved = true;
101111
isGranted(result) ? resolvePromise() : rejectPromise();
112+
};
113+
var request;
102114

103115
if (hasPermissions) {
104116
resolver(existing);
@@ -107,12 +119,14 @@ export default class Permission {
107119
resolver(result);
108120
});
109121
} else if (isModernAPI) {
110-
this._win.Notification
111-
.requestPermission()
112-
.then(result => {
113-
resolver(result);
114-
})
115-
.catch(rejectPromise);
122+
/* Safari 12+ */
123+
/* This resolver argument will only be used in Safari */
124+
/* CHrome, instead, returns a Promise */
125+
request = this._win.Notification.requestPermission(resolver);
126+
if (request && request.then) {
127+
/* Chrome 23+ */
128+
request.then(resolver).catch(rejectPromise);
129+
}
116130
} else resolvePromise();
117131
});
118132
}

0 commit comments

Comments
 (0)