Selaa lähdekoodia

fix(api): consistent return pattern (#1245)

Noah Klayman 4 vuotta sitten
vanhempi
sitoutus
0f3009b635
12 muutettua tiedostoa jossa 95 lisäystä ja 93 poistoa
  1. 3 1
      api/.eslintrc.js
  2. 1 1
      api/src/cli.ts
  3. 2 2
      api/src/dialog.ts
  4. 1 1
      api/src/event.ts
  5. 10 10
      api/src/fs.ts
  6. 2 2
      api/src/globalShortcut.ts
  7. 9 9
      api/src/http.ts
  8. 3 3
      api/src/notification.ts
  9. 19 19
      api/src/path.ts
  10. 2 2
      api/src/shell.ts
  11. 1 1
      api/src/tauri.ts
  12. 42 42
      api/src/window.ts

+ 3 - 1
api/.eslintrc.js

@@ -51,6 +51,8 @@ module.exports = {
     'security/detect-pseudoRandomBytes': 'error',
     'space-before-function-paren': 'off',
     '@typescript-eslint/default-param-last': 'off',
-    '@typescript-eslint/strict-boolean-expressions': 0
+    '@typescript-eslint/strict-boolean-expressions': 0,
+    'no-return-await': 'warn',
+    '@typescript-eslint/return-await': 'off'
   }
 }

+ 1 - 1
api/src/cli.ts

@@ -27,7 +27,7 @@ export interface CliMatches {
  * gets the CLI matches
  */
 async function getMatches(): Promise<CliMatches> {
-  return await invoke<CliMatches>({
+  return invoke<CliMatches>({
     __tauriModule: 'Cli',
     message: {
       cmd: 'cliMatches'

+ 2 - 2
api/src/dialog.ts

@@ -29,7 +29,7 @@ async function open(
     Object.freeze(options)
   }
 
-  return await invoke<string | string[]>({
+  return invoke<string | string[]>({
     __tauriModule: 'Dialog',
     mainThread: true,
     message: {
@@ -52,7 +52,7 @@ async function save(options: SaveDialogOptions = {}): Promise<string> {
     Object.freeze(options)
   }
 
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Dialog',
     mainThread: true,
     message: {

+ 1 - 1
api/src/event.ts

@@ -1,7 +1,7 @@
 import { emit as emitEvent } from './helpers/event'
 
 async function emit(event: string, payload?: string): Promise<void> {
-  return await emitEvent(event, undefined, payload)
+  return emitEvent(event, undefined, payload)
 }
 
 export { listen } from './helpers/event'

+ 10 - 10
api/src/fs.ts

@@ -61,7 +61,7 @@ async function readTextFile(
   filePath: string,
   options: FsOptions = {}
 ): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'readTextFile',
@@ -83,7 +83,7 @@ async function readBinaryFile(
   filePath: string,
   options: FsOptions = {}
 ): Promise<number[]> {
-  return await invoke<number[]>({
+  return invoke<number[]>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'readBinaryFile',
@@ -114,7 +114,7 @@ async function writeFile(
     Object.freeze(file)
   }
 
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'writeFile',
@@ -179,7 +179,7 @@ async function writeBinaryFile(
     Object.freeze(file)
   }
 
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'writeBinaryFile',
@@ -203,7 +203,7 @@ async function readDir(
   dir: string,
   options: FsDirOptions = {}
 ): Promise<FileEntry[]> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'readDir',
@@ -228,7 +228,7 @@ async function createDir(
   dir: string,
   options: FsDirOptions = {}
 ): Promise<void> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'createDir',
@@ -252,7 +252,7 @@ async function removeDir(
   dir: string,
   options: FsDirOptions = {}
 ): Promise<void> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'removeDir',
@@ -276,7 +276,7 @@ async function copyFile(
   destination: string,
   options: FsOptions = {}
 ): Promise<void> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'copyFile',
@@ -299,7 +299,7 @@ async function removeFile(
   file: string,
   options: FsOptions = {}
 ): Promise<void> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'removeFile',
@@ -323,7 +323,7 @@ async function renameFile(
   newPath: string,
   options: FsOptions = {}
 ): Promise<void> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Fs',
     message: {
       cmd: 'renameFile',

+ 2 - 2
api/src/globalShortcut.ts

@@ -9,7 +9,7 @@ async function registerShortcut(
   shortcut: string,
   handler: () => void
 ): Promise<void> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'GlobalShortcut',
     message: {
       cmd: 'register',
@@ -24,7 +24,7 @@ async function registerShortcut(
  * @param shortcut shortcut definition, modifiers and key separated by "+" e.g. Alt+Q
  */
 async function unregisterShortcut(shortcut: string): Promise<void> {
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'GlobalShortcut',
     message: {
       cmd: 'unregister',

+ 9 - 9
api/src/http.ts

@@ -80,7 +80,7 @@ export class Client {
    * drops the client instance
    */
   async drop(): Promise<void> {
-    await invoke({
+    return invoke({
       module: 'Http',
       message: {
         cmd: 'dropClient',
@@ -97,7 +97,7 @@ export class Client {
    * @return promise resolving to the response
    */
   async request<T>(options: HttpOptions): Promise<Response<T>> {
-    return await invoke({
+    return invoke({
       module: 'Http',
       message: {
         cmd: 'httpRequest',
@@ -116,7 +116,7 @@ export class Client {
    * @return promise resolving to the response
    */
   async get<T>(url: string, options: RequestOptions): Promise<Response<T>> {
-    return await this.request({
+    return this.request({
       method: 'GET',
       url,
       ...options
@@ -137,7 +137,7 @@ export class Client {
     body: Body,
     options: RequestOptions
   ): Promise<Response<T>> {
-    return await this.request({
+    return this.request({
       method: 'POST',
       url,
       body,
@@ -159,7 +159,7 @@ export class Client {
     body: Body,
     options: RequestOptions
   ): Promise<Response<T>> {
-    return await this.request({
+    return this.request({
       method: 'PUT',
       url,
       body,
@@ -176,7 +176,7 @@ export class Client {
    * @return promise resolving to the response
    */
   async patch<T>(url: string, options: RequestOptions): Promise<Response<T>> {
-    return await this.request({
+    return this.request({
       method: 'PATCH',
       url,
       ...options
@@ -192,7 +192,7 @@ export class Client {
    * @return promise resolving to the response
    */
   async delete<T>(url: string, options: RequestOptions): Promise<Response<T>> {
-    return await this.request({
+    return this.request({
       method: 'DELETE',
       url,
       ...options
@@ -201,7 +201,7 @@ export class Client {
 }
 
 async function getClient(options?: ClientOptions): Promise<Client> {
-  return await invoke<number>({
+  return invoke<number>({
     module: 'Http',
     message: {
       cmd: 'createClient',
@@ -219,7 +219,7 @@ async function fetch<T>(
   if (defaultClient === null) {
     defaultClient = await getClient()
   }
-  return await defaultClient.request({
+  return defaultClient.request({
     url,
     method: options?.method ?? 'GET',
     ...options

+ 3 - 3
api/src/notification.ts

@@ -11,9 +11,9 @@ export type Permission = 'granted' | 'denied' | 'default'
 
 async function isPermissionGranted(): Promise<boolean | null> {
   if (window.Notification.permission !== 'default') {
-    return await Promise.resolve(window.Notification.permission === 'granted')
+    return Promise.resolve(window.Notification.permission === 'granted')
   }
-  return await invoke<void>({
+  return invoke({
     __tauriModule: 'Notification',
     message: {
       cmd: 'isNotificationPermissionGranted'
@@ -22,7 +22,7 @@ async function isPermissionGranted(): Promise<boolean | null> {
 }
 
 async function requestPermission(): Promise<Permission> {
-  return await window.Notification.requestPermission()
+  return window.Notification.requestPermission()
 }
 
 function sendNotification(options: Options | string): void {

+ 19 - 19
api/src/path.ts

@@ -7,7 +7,7 @@ import { BaseDirectory } from './fs'
  * @return {Promise<string>}
  */
 async function appDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -23,7 +23,7 @@ async function appDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function audioDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -39,7 +39,7 @@ async function audioDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function cacheDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -55,7 +55,7 @@ async function cacheDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function configDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -71,7 +71,7 @@ async function configDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function dataDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -87,7 +87,7 @@ async function dataDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function desktopDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -103,7 +103,7 @@ async function desktopDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function documentDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -119,7 +119,7 @@ async function documentDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function downloadDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -135,7 +135,7 @@ async function downloadDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function executableDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -151,7 +151,7 @@ async function executableDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function fontDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -167,7 +167,7 @@ async function fontDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function homeDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -183,7 +183,7 @@ async function homeDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function localDataDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -199,7 +199,7 @@ async function localDataDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function pictureDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -215,7 +215,7 @@ async function pictureDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function publicDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -231,7 +231,7 @@ async function publicDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function resourceDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -247,7 +247,7 @@ async function resourceDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function runtimeDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -263,7 +263,7 @@ async function runtimeDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function templateDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -279,7 +279,7 @@ async function templateDir(): Promise<string> {
  * @return {Promise<string>}
  */
 async function videoDir(): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',
@@ -298,7 +298,7 @@ async function resolvePath(
   path: string,
   directory: BaseDirectory
 ): Promise<string> {
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Fs',
     message: {
       cmd: 'resolvePath',

+ 2 - 2
api/src/shell.ts

@@ -15,7 +15,7 @@ async function execute(
     Object.freeze(args)
   }
 
-  return await invoke<string>({
+  return invoke<string>({
     __tauriModule: 'Shell',
     message: {
       cmd: 'execute',
@@ -31,7 +31,7 @@ async function execute(
  * @param url the URL to open
  */
 async function open(url: string): Promise<void> {
-  await invoke({
+  return invoke({
     __tauriModule: 'Shell',
     message: {
       cmd: 'open',

+ 1 - 1
api/src/tauri.ts

@@ -57,7 +57,7 @@ function transformCallback(
  * @return {Promise<T>} Promise resolving or rejecting to the backend response
  */
 async function invoke<T>(args: any): Promise<T> {
-  return await new Promise((resolve, reject) => {
+  return new Promise((resolve, reject) => {
     const callback = transformCallback((e) => {
       resolve(e)
       Reflect.deleteProperty(window, error)

+ 42 - 42
api/src/window.ts

@@ -8,9 +8,9 @@ interface WindowDef {
 declare global {
   interface Window {
     __TAURI__: {
-      __windows: WindowDef[],
+      __windows: WindowDef[]
       __currentWindow: WindowDef
-    };
+    }
   }
 }
 
@@ -35,23 +35,29 @@ class TauriWindow {
    * @param handler the event handler callback
    * @param once unlisten after the first trigger if true
    */
-  async listen<T>(event: string, handler: EventCallback<T>, once = false): Promise<void> {
-    await listen(event, handler, once)
+  async listen<T>(
+    event: string,
+    handler: EventCallback<T>,
+    once = false
+  ): Promise<void> {
+    return listen(event, handler, once)
   }
 
   /**
-  * emits an event to the webview
-  *
-  * @param event the event name
-  * @param [payload] the event payload
-  */
+   * emits an event to the webview
+   *
+   * @param event the event name
+   * @param [payload] the event payload
+   */
   async emit(event: string, payload?: string): Promise<void> {
-    await emit(event, this.label, payload)
+    return emit(event, this.label, payload)
   }
 }
 
-function getTauriWindow(label: string = getCurrentWindow().label): TauriWindow | null {
-  if (getWindows().some(w => w.label === label)) {
+function getTauriWindow(
+  label: string = getCurrentWindow().label
+): TauriWindow | null {
+  if (getWindows().some((w) => w.label === label)) {
     return new TauriWindow(label)
   } else {
     return null
@@ -60,10 +66,10 @@ function getTauriWindow(label: string = getCurrentWindow().label): TauriWindow |
 
 class WindowManager {
   /**
- * Updates the window resizable flag.
- */
+   * Updates the window resizable flag.
+   */
   async setResizable(resizable: boolean): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setResizable',
@@ -78,7 +84,7 @@ class WindowManager {
    * @param title the new title
    */
   async setTitle(title: string): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setTitle',
@@ -91,7 +97,7 @@ class WindowManager {
    * Maximizes the window.
    */
   async maximize(): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'maximize'
@@ -103,7 +109,7 @@ class WindowManager {
    * Unmaximizes the window.
    */
   async unmaximize(): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'unmaximize'
@@ -115,7 +121,7 @@ class WindowManager {
    * Minimizes the window.
    */
   async minimize(): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'minimize'
@@ -127,7 +133,7 @@ class WindowManager {
    * Unminimizes the window.
    */
   async unminimize(): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'unminimize'
@@ -139,7 +145,7 @@ class WindowManager {
    * Sets the window visibility to true.
    */
   async show(): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'show'
@@ -151,7 +157,7 @@ class WindowManager {
    * Sets the window visibility to false.
    */
   async hide(): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'hide'
@@ -165,7 +171,7 @@ class WindowManager {
    * @param {boolean} transparent whether the the window should be transparent or not
    */
   async setTransparent(transparent: boolean): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setTransparent',
@@ -180,7 +186,7 @@ class WindowManager {
    * @param {boolean} decorations whether the window should have borders and bars
    */
   async setDecorations(decorations: boolean): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setDecorations',
@@ -195,7 +201,7 @@ class WindowManager {
    * @param {boolean} alwaysOnTop whether the window should always be on top of other windows or not
    */
   async setAlwaysOnTop(alwaysOnTop: boolean): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setAlwaysOnTop',
@@ -210,7 +216,7 @@ class WindowManager {
    * @param {number} width the new window width
    */
   async setWidth(width: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setWidth',
@@ -225,7 +231,7 @@ class WindowManager {
    * @param {number} height the new window height
    */
   async setHeight(height: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setHeight',
@@ -241,7 +247,7 @@ class WindowManager {
    * @param {number} height the new window height
    */
   async resize(width: number, height: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'resize',
@@ -258,7 +264,7 @@ class WindowManager {
    * @param {number} minHeight the new window min height
    */
   async setMinSize(minWidth: number, minHeight: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setMinSize',
@@ -275,7 +281,7 @@ class WindowManager {
    * @param {number} maxHeight the new window max height
    */
   async setMaxSize(maxWidth: number, maxHeight: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setMaxSize',
@@ -291,7 +297,7 @@ class WindowManager {
    * @param {number} x the new window x position
    */
   async setX(x: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setX',
@@ -306,7 +312,7 @@ class WindowManager {
    * @param {number} y the new window y position
    */
   async setY(y: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setY',
@@ -322,7 +328,7 @@ class WindowManager {
    * @param {number} y the new window y position
    */
   async setPosition(x: number, y: number): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setPosition',
@@ -338,7 +344,7 @@ class WindowManager {
    * @param {boolean} fullscreen whether the window should go to fullscreen or not
    */
   async setFullscreen(fullscreen: boolean): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setFullscreen',
@@ -353,7 +359,7 @@ class WindowManager {
    * @param {string | number[]} icon icon bytes or path to the icon file
    */
   async setIcon(icon: 'string' | number[]): Promise<void> {
-    await invoke({
+    return invoke({
       __tauriModule: 'Window',
       message: {
         cmd: 'setIcon',
@@ -365,10 +371,4 @@ class WindowManager {
 
 const manager = new WindowManager()
 
-export {
-  TauriWindow,
-  getTauriWindow,
-  getCurrentWindow,
-  getWindows,
-  manager
-}
+export { TauriWindow, getTauriWindow, getCurrentWindow, getWindows, manager }