Skip to content

Commit

Permalink
refactor(api/notifications): fix 'Notification.requestPermission'
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Nov 3, 2023
1 parent bd945b6 commit a042fa7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
18 changes: 16 additions & 2 deletions api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6834,7 +6834,7 @@ declare module "socket:notification" {
* `NotificationOptions` class constructor.
* @ignore
* @param {object} [options = {}]
* @param {'auto'|'ltr|'rtl'=} [options.dir = 'auto']
* @param {string=} [options.dir = 'auto']
* @param {NotificationAction[]=} [options.actions = []]
* @param {string|URL=} [options.badge = '']
* @param {string=} [options.body = '']
Expand All @@ -6848,7 +6848,21 @@ declare module "socket:notification" {
* @param {boolean=} [options.silent = false]
* @param {number[]=} [options.vibrate = []]
*/
constructor(options?: object);
constructor(options?: {
dir?: string | undefined;
actions?: NotificationAction[] | undefined;
badge?: (string | URL) | undefined;
body?: string | undefined;
data?: (any | null) | undefined;
icon?: (string | URL) | undefined;
image?: (string | URL) | undefined;
lang?: string | undefined;
tag?: string | undefined;
boolean?: boolean | undefined;
requireInteraction?: boolean | undefined;
silent?: boolean | undefined;
vibrate?: number[] | undefined;
});
/**
* An array of actions to display in the notification.
* @type {NotificationAction[]}
Expand Down
25 changes: 22 additions & 3 deletions api/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class NativeNotificationProxy extends NativeNotification {

this.#notification = notification

// @ts-ignore
this.onerror = (event) => {
error = true
notification.dispatchEvent(new ErrorEvent('error', {
Expand All @@ -85,6 +86,7 @@ class NativeNotificationProxy extends NativeNotification {
}))
}

// @ts-ignore
this.onclose = (event) => {
if (error) return
if (!clicked) {
Expand All @@ -99,6 +101,7 @@ class NativeNotificationProxy extends NativeNotification {
}
}

// @ts-ignore
this.onclick = () => {
if (error) return
clicked = true
Expand All @@ -112,6 +115,7 @@ class NativeNotificationProxy extends NativeNotification {
globalThis.dispatchEvent(event)
}

// @ts-ignore
this.onshow = () => {
if (error) return
const event = new CustomEvent(NOTIFICATION_PRESENTED_EVENT, {
Expand Down Expand Up @@ -245,7 +249,7 @@ export class NotificationOptions {
* `NotificationOptions` class constructor.
* @ignore
* @param {object} [options = {}]
* @param {'auto'|'ltr|'rtl'=} [options.dir = 'auto']
* @param {string=} [options.dir = 'auto']
* @param {NotificationAction[]=} [options.actions = []]
* @param {string|URL=} [options.badge = '']
* @param {string=} [options.body = '']
Expand All @@ -261,6 +265,7 @@ export class NotificationOptions {
*/
constructor (options = {}) {
if ('dir' in options) {
// @ts-ignore
if (!(options.dir in NotificationDirection)) {
throw new TypeError(
'Failed to read the \'dir\' property from \'NotificationOptions\': ' +
Expand All @@ -269,10 +274,12 @@ export class NotificationOptions {
)
}

// @ts-ignore
this.#dir = options.dir
}

if ('actions' in options) {
// @ts-ignore
if (!(Symbol.iterator in options.actions)) {
throw new TypeError(
'Failed to read the \'actions\' property from ' +
Expand All @@ -281,6 +288,7 @@ export class NotificationOptions {
)
}

// @ts-ignore
for (const action of options.actions) {
try {
this.#actions.push(new NotificationAction(action))
Expand Down Expand Up @@ -489,6 +497,7 @@ export async function showNotification (title, options, onclick = null, onshow =
notification.onclick = onclick
notification.onshow = onshow
notification.onerror = (e) => reject(e.error)
// @ts-ignore
notification.onshow = () => resolve()
})
}
Expand Down Expand Up @@ -531,16 +540,22 @@ export class Notification extends EventTarget {
*/
static async requestPermission () {
if (isLinux) {
// @ts-ignore
if (typeof NativeNotification?.requestPermission === 'function') {
// @ts-ignore
return await NativeNotification.requestPermission()
}

return 'denied'
}

const status = await permissions.request({ name: 'notifications' })
status.unsubscribe()
return status.state
return new Promise((resolve) => {
status.onchange = (event) => {
status.unsubscribe()
resolve(event.detail.state)
}
})
}

#onclick = null
Expand Down Expand Up @@ -591,12 +606,15 @@ export class Notification extends EventTarget {
)
}

// @ts-ignore
this.#id = (rand64() & 0xFFFFn).toString()

if (isLinux) {
const proxy = new NativeNotificationProxy(this)
const request = new Promise((resolve) => {
// @ts-ignore
proxy.addEventListener('show', () => resolve({}))
// @ts-ignore
proxy.addEventListener('error', (e) => resolve({ err: e.error }))
})

Expand Down Expand Up @@ -640,6 +658,7 @@ export class Notification extends EventTarget {
// propagate error to caller
this[Symbol.for('Notification.request')].then((result) => {
if (result?.err) {
// @ts-ignore
state.pending.delete(this.id, this)
removeNotificationPresentedListener()
removeNotificationResponseListener()
Expand Down

0 comments on commit a042fa7

Please sign in to comment.