event.cjs 4.0 KB

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