event.d.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import type { EventCallback, UnlistenFn, Event } from './helpers/event';
  2. export type EventName = `${TauriEvent}` | (string & Record<never, never>);
  3. /**
  4. * @since 1.1.0
  5. */
  6. export declare enum TauriEvent {
  7. WINDOW_RESIZED = "tauri://resize",
  8. WINDOW_MOVED = "tauri://move",
  9. WINDOW_CLOSE_REQUESTED = "tauri://close-requested",
  10. WINDOW_CREATED = "tauri://window-created",
  11. WINDOW_DESTROYED = "tauri://destroyed",
  12. WINDOW_FOCUS = "tauri://focus",
  13. WINDOW_BLUR = "tauri://blur",
  14. WINDOW_SCALE_FACTOR_CHANGED = "tauri://scale-change",
  15. WINDOW_THEME_CHANGED = "tauri://theme-changed",
  16. WINDOW_FILE_DROP = "tauri://file-drop",
  17. WINDOW_FILE_DROP_HOVER = "tauri://file-drop-hover",
  18. WINDOW_FILE_DROP_CANCELLED = "tauri://file-drop-cancelled",
  19. MENU = "tauri://menu",
  20. CHECK_UPDATE = "tauri://update",
  21. UPDATE_AVAILABLE = "tauri://update-available",
  22. INSTALL_UPDATE = "tauri://update-install",
  23. STATUS_UPDATE = "tauri://update-status",
  24. DOWNLOAD_PROGRESS = "tauri://update-download-progress"
  25. }
  26. /**
  27. * Listen to an event. The event can be either global or window-specific.
  28. * See {@link Event.windowLabel} to check the event source.
  29. *
  30. * @example
  31. * ```typescript
  32. * import { listen } from '@tauri-apps/api/event';
  33. * const unlisten = await listen<string>('error', (event) => {
  34. * console.log(`Got error in window ${event.windowLabel}, payload: ${event.payload}`);
  35. * });
  36. *
  37. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  38. * unlisten();
  39. * ```
  40. *
  41. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  42. * @param handler Event handler callback.
  43. * @returns A promise resolving to a function to unlisten to the event.
  44. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  45. *
  46. * @since 1.0.0
  47. */
  48. declare function listen<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>;
  49. /**
  50. * Listen to an one-off event. See {@link listen} for more information.
  51. *
  52. * @example
  53. * ```typescript
  54. * import { once } from '@tauri-apps/api/event';
  55. * interface LoadedPayload {
  56. * loggedIn: boolean,
  57. * token: string
  58. * }
  59. * const unlisten = await once<LoadedPayload>('loaded', (event) => {
  60. * console.log(`App is loaded, loggedIn: ${event.payload.loggedIn}, token: ${event.payload.token}`);
  61. * });
  62. *
  63. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  64. * unlisten();
  65. * ```
  66. *
  67. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  68. * @returns A promise resolving to a function to unlisten to the event.
  69. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  70. *
  71. * @since 1.0.0
  72. */
  73. declare function once<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>;
  74. /**
  75. * Emits an event to the backend and all Tauri windows.
  76. * @example
  77. * ```typescript
  78. * import { emit } from '@tauri-apps/api/event';
  79. * await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });
  80. * ```
  81. *
  82. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  83. *
  84. * @since 1.0.0
  85. */
  86. declare function emit(event: string, payload?: unknown): Promise<void>;
  87. export type { Event, EventCallback, UnlistenFn };
  88. export { listen, once, emit };