event.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { listen as listen$1, once as once$1, emit as emit$1 } from './helpers/event.js';
  2. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  3. // SPDX-License-Identifier: Apache-2.0
  4. // SPDX-License-Identifier: MIT
  5. /**
  6. * The event system allows you to emit events to the backend and listen to events from it.
  7. *
  8. * This package is also accessible with `window.__TAURI__.event` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  9. * @module
  10. */
  11. /**
  12. * @since 1.1.0
  13. */
  14. var TauriEvent;
  15. (function (TauriEvent) {
  16. TauriEvent["WINDOW_RESIZED"] = "tauri://resize";
  17. TauriEvent["WINDOW_MOVED"] = "tauri://move";
  18. TauriEvent["WINDOW_CLOSE_REQUESTED"] = "tauri://close-requested";
  19. TauriEvent["WINDOW_CREATED"] = "tauri://window-created";
  20. TauriEvent["WINDOW_DESTROYED"] = "tauri://destroyed";
  21. TauriEvent["WINDOW_FOCUS"] = "tauri://focus";
  22. TauriEvent["WINDOW_BLUR"] = "tauri://blur";
  23. TauriEvent["WINDOW_SCALE_FACTOR_CHANGED"] = "tauri://scale-change";
  24. TauriEvent["WINDOW_THEME_CHANGED"] = "tauri://theme-changed";
  25. TauriEvent["WINDOW_FILE_DROP"] = "tauri://file-drop";
  26. TauriEvent["WINDOW_FILE_DROP_HOVER"] = "tauri://file-drop-hover";
  27. TauriEvent["WINDOW_FILE_DROP_CANCELLED"] = "tauri://file-drop-cancelled";
  28. TauriEvent["MENU"] = "tauri://menu";
  29. TauriEvent["CHECK_UPDATE"] = "tauri://update";
  30. TauriEvent["UPDATE_AVAILABLE"] = "tauri://update-available";
  31. TauriEvent["INSTALL_UPDATE"] = "tauri://update-install";
  32. TauriEvent["STATUS_UPDATE"] = "tauri://update-status";
  33. TauriEvent["DOWNLOAD_PROGRESS"] = "tauri://update-download-progress";
  34. })(TauriEvent || (TauriEvent = {}));
  35. /**
  36. * Listen to an event. The event can be either global or window-specific.
  37. * See {@link Event.windowLabel} to check the event source.
  38. *
  39. * @example
  40. * ```typescript
  41. * import { listen } from '@tauri-apps/api/event';
  42. * const unlisten = await listen<string>('error', (event) => {
  43. * console.log(`Got error in window ${event.windowLabel}, payload: ${event.payload}`);
  44. * });
  45. *
  46. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  47. * unlisten();
  48. * ```
  49. *
  50. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  51. * @param handler Event handler callback.
  52. * @returns A promise resolving to a function to unlisten to the event.
  53. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  54. *
  55. * @since 1.0.0
  56. */
  57. async function listen(event, handler) {
  58. return listen$1(event, null, handler);
  59. }
  60. /**
  61. * Listen to an one-off event. See {@link listen} for more information.
  62. *
  63. * @example
  64. * ```typescript
  65. * import { once } from '@tauri-apps/api/event';
  66. * interface LoadedPayload {
  67. * loggedIn: boolean,
  68. * token: string
  69. * }
  70. * const unlisten = await once<LoadedPayload>('loaded', (event) => {
  71. * console.log(`App is loaded, loggedIn: ${event.payload.loggedIn}, token: ${event.payload.token}`);
  72. * });
  73. *
  74. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  75. * unlisten();
  76. * ```
  77. *
  78. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  79. * @returns A promise resolving to a function to unlisten to the event.
  80. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  81. *
  82. * @since 1.0.0
  83. */
  84. async function once(event, handler) {
  85. return once$1(event, null, handler);
  86. }
  87. /**
  88. * Emits an event to the backend and all Tauri windows.
  89. * @example
  90. * ```typescript
  91. * import { emit } from '@tauri-apps/api/event';
  92. * await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });
  93. * ```
  94. *
  95. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  96. *
  97. * @since 1.0.0
  98. */
  99. async function emit(event, payload) {
  100. return emit$1(event, undefined, payload);
  101. }
  102. export { TauriEvent, emit, listen, once };