fix(tauri.js) move Notification initialization to tauri.js script (#736)

This commit is contained in:
Lucas Fernandes Nogueira 2020-07-03 13:47:30 -03:00 committed by GitHub
parent c59eebbced
commit 015474657c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 63 deletions

View File

@ -1,46 +1,6 @@
import { Options, PartialOptions, Permission } from './types/notification'
import { Options, Permission } from './types/notification'
import { promisified } from './tauri'
let permissionSettable = false
let permissionValue = 'default'
function setNotificationPermission(value: Permission): void {
permissionSettable = true
// @ts-expect-error
window.Notification.permission = value
permissionSettable = false
}
// @ts-expect-error
window.Notification = (title: string, options?: PartialOptions) => {
sendNotification({
title,
...options
})
}
window.Notification.requestPermission = requestPermission
Object.defineProperty(window.Notification, 'permission', {
enumerable: true,
get: () => permissionValue,
set(v: Permission) {
if (!permissionSettable) {
throw new Error('Readonly property')
}
permissionValue = v
}
})
isPermissionGranted()
.then(response => {
if (response === null) {
setNotificationPermission('default')
} else {
setNotificationPermission(response ? 'granted' : 'denied')
}
})
.catch(err => { throw err })
async function isPermissionGranted(): Promise<boolean | null> {
if (window.Notification.permission !== 'default') {
return await Promise.resolve(window.Notification.permission === 'granted')
@ -51,34 +11,21 @@ async function isPermissionGranted(): Promise<boolean | null> {
}
async function requestPermission(): Promise<Permission> {
return await promisified<Permission>({
cmd: 'requestNotificationPermission'
}).then(permission => {
setNotificationPermission(permission)
return permission
})
return await window.Notification.requestPermission()
}
function sendNotification(options: Options | string): void {
if (typeof options === 'object') {
Object.freeze(options)
if (typeof options === 'string') {
// eslint-disable-next-line no-new
new window.Notification(options)
} else {
// eslint-disable-next-line no-new
new window.Notification(options.title, options)
}
isPermissionGranted()
.then(permission => {
if (permission) {
return promisified({
cmd: 'notification',
options: typeof options === 'string' ? {
body: options
} : options
})
}
})
.catch(err => { throw err })
}
export {
sendNotification,
requestPermission,
isPermissionGranted
}

View File

@ -1,5 +1,5 @@
export interface Options {
title?: string
title: string
body?: string
icon?: string
}

View File

@ -16,6 +16,8 @@ export default [{
'process': './api-src/process.ts',
'tauri': './api-src/tauri.ts',
'window': './api-src/window.ts',
'cli': './api-src/cli.ts',
'notification': './api-src/notification.ts',
},
treeshake: true,
perf: true,

View File

@ -181,4 +181,80 @@ switch (navigator.platform) {
__openLinks()
}, true)
}
let permissionSettable = false
let permissionValue = 'default'
function isPermissionGranted() {
if (window.Notification.permission !== 'default') {
return Promise.resolve(window.Notification.permission === 'granted')
}
return window.__TAURI__.promisified({
cmd: 'isNotificationPermissionGranted'
})
}
function setNotificationPermission(value) {
permissionSettable = true
window.Notification.permission = value
permissionSettable = false
}
function requestPermission() {
return window.__TAURI__.promisified({
cmd: 'requestNotificationPermission'
}).then(function (permission) {
setNotificationPermission(permission)
return permission
})
}
function sendNotification(options) {
if (typeof options === 'object') {
Object.freeze(options)
}
isPermissionGranted()
.then(function (permission) {
if (permission) {
return window.__TAURI__.promisified({
cmd: 'notification',
options: typeof options === 'string' ? {
body: options
} : options
})
}
})
}
window.Notification = function (title, options) {
var opts = options || {}
sendNotification(Object.assign(opts, {
title: title
}))
}
window.Notification.requestPermission = requestPermission
Object.defineProperty(window.Notification, 'permission', {
enumerable: true,
get: function () {
return permissionValue
},
set: function (v) {
if (!permissionSettable) {
throw new Error('Readonly property')
}
permissionValue = v
}
})
isPermissionGranted()
.then(function (response) {
if (response === null) {
setNotificationPermission('default')
} else {
setNotificationPermission(response ? 'granted' : 'denied')
}
})
})()