event.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 { invokeTauriCommand } from './helpers/tauri'
  11. import { emit as emitEvent } from './helpers/event'
  12. import { transformCallback } from './tauri'
  13. import { LiteralUnion } from 'type-fest'
  14. interface Event<T> {
  15. /** Event name */
  16. event: EventName
  17. /** Event identifier used to unlisten */
  18. id: number
  19. /** Event payload */
  20. payload: T
  21. }
  22. type EventName = LiteralUnion<
  23. | 'tauri://update'
  24. | 'tauri://update-available'
  25. | 'tauri://update-install'
  26. | 'tauri://update-status'
  27. | 'tauri://resize'
  28. | 'tauri://move'
  29. | 'tauri://close-requested'
  30. | 'tauri://focus'
  31. | 'tauri://blur'
  32. | 'tauri://scale-change'
  33. | 'tauri://menu'
  34. | 'tauri://file-drop'
  35. | 'tauri://file-drop-hover'
  36. | 'tauri://file-drop-cancelled',
  37. string
  38. >
  39. type EventCallback<T> = (event: Event<T>) => void
  40. type UnlistenFn = () => void
  41. /**
  42. * Unregister the event listener associated with the given id.
  43. *
  44. * @ignore
  45. * @param eventId Event identifier
  46. * @returns
  47. */
  48. async function _unlisten(eventId: number): Promise<void> {
  49. return invokeTauriCommand({
  50. __tauriModule: 'Event',
  51. message: {
  52. cmd: 'unlisten',
  53. eventId
  54. }
  55. })
  56. }
  57. /**
  58. * Listen to an event from the backend.
  59. *
  60. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  61. * @param handler Event handler callback.
  62. * @return A promise resolving to a function to unlisten to the event.
  63. */
  64. async function listen<T>(
  65. event: EventName,
  66. handler: EventCallback<T>
  67. ): Promise<UnlistenFn> {
  68. return invokeTauriCommand<number>({
  69. __tauriModule: 'Event',
  70. message: {
  71. cmd: 'listen',
  72. event,
  73. handler: transformCallback(handler)
  74. }
  75. }).then((eventId) => {
  76. return async () => _unlisten(eventId)
  77. })
  78. }
  79. /**
  80. * Listen to an one-off event from the backend.
  81. *
  82. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  83. * @param handler Event handler callback.
  84. * @returns A promise resolving to a function to unlisten to the event.
  85. */
  86. async function once<T>(
  87. event: EventName,
  88. handler: EventCallback<T>
  89. ): Promise<UnlistenFn> {
  90. return listen<T>(event, (eventData) => {
  91. handler(eventData)
  92. _unlisten(eventData.id).catch(() => {})
  93. })
  94. }
  95. /**
  96. * Emits an event to the backend.
  97. *
  98. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  99. * @param [payload] Event payload
  100. * @returns
  101. */
  102. async function emit(event: string, payload?: unknown): Promise<void> {
  103. return emitEvent(event, null, payload)
  104. }
  105. export type { Event, EventName, EventCallback, UnlistenFn }
  106. export { listen, once, emit }