ソースを参照

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

Lucas Fernandes Nogueira 5 年 前
コミット
015474657c

+ 9 - 62
cli/tauri.js/api-src/notification.ts

@@ -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
 }

+ 1 - 1
cli/tauri.js/api-src/types/notification.ts

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

+ 2 - 0
cli/tauri.js/rollup.config.js

@@ -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,

+ 76 - 0
cli/tauri.js/templates/tauri.js

@@ -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')
+      }
+    })
 })()