event.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * The event system allows you to emit events to the backend and listen to events from it.
  6. *
  7. * This package is also accessible with `window.__TAURI__.event` when `tauri.conf.json > build > withGlobalTauri` is set to true.
  8. * @module
  9. */
  10. import * as eventApi from './helpers/event'
  11. import type {
  12. EventName,
  13. EventCallback,
  14. UnlistenFn,
  15. Event
  16. } from './helpers/event'
  17. /**
  18. * Listen to an event from the backend.
  19. * @example Listen to the `error` event expecting a string payload
  20. * ```typescript
  21. * import { listen } from '@tauri-apps/api/event';
  22. * const unlisten = await listen<string>('error', (event) => {
  23. * console.log(`Got error in window ${event.windowLabel}, payload: ${payload}`);
  24. * });
  25. *
  26. * // removes the listener later
  27. * await unlisten();
  28. * ```
  29. *
  30. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  31. * @param handler Event handler callback.
  32. * @return A promise resolving to a function to unlisten to the event.
  33. */
  34. async function listen<T>(
  35. event: EventName,
  36. handler: EventCallback<T>
  37. ): Promise<UnlistenFn> {
  38. return eventApi.listen(event, null, handler)
  39. }
  40. /**
  41. * Listen to an one-off event from the backend.
  42. * @example Listen to the `loaded` event that is only triggered once
  43. * ```typescript
  44. * import { once } from '@tauri-apps/api/event';
  45. * interface LoadedPayload {
  46. * loggedIn: boolean,
  47. * token: string
  48. * }
  49. * const unlisten = await once<LoadedPayload>('loaded', (event) => {
  50. * console.log(`App is loaded, logggedIn: ${event.payload.loggedIn}, token: ${event.payload.token}`);
  51. * });
  52. * ```
  53. *
  54. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  55. * @param handler Event handler callback.
  56. * @returns A promise resolving to a function to unlisten to the event.
  57. */
  58. async function once<T>(
  59. event: EventName,
  60. handler: EventCallback<T>
  61. ): Promise<UnlistenFn> {
  62. return eventApi.once(event, null, handler)
  63. }
  64. /**
  65. * Emits an event to the backend.
  66. * @example Emits the `frontend-loaded` event with the given payload
  67. * ```typescript
  68. * import { emit } from '@tauri-apps/api/event';
  69. * await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });
  70. * ```
  71. *
  72. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  73. * @param [payload] Event payload
  74. * @returns
  75. */
  76. async function emit(event: string, payload?: unknown): Promise<void> {
  77. return eventApi.emit(event, undefined, payload)
  78. }
  79. export type { Event, EventName, EventCallback, UnlistenFn }
  80. export { listen, once, emit }