event.d.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { type WindowLabel } from '../window';
  2. import { type EventName } from '../event';
  3. export interface Event<T> {
  4. /** Event name */
  5. event: EventName;
  6. /** The label of the window that emitted this event. */
  7. windowLabel: string;
  8. /** Event identifier used to unlisten */
  9. id: number;
  10. /** Event payload */
  11. payload: T;
  12. }
  13. export type EventCallback<T> = (event: Event<T>) => void;
  14. export type UnlistenFn = () => void;
  15. /**
  16. * Emits an event to the backend.
  17. *
  18. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  19. * @param [windowLabel] The label of the window to which the event is sent, if null/undefined the event will be sent to all windows
  20. * @param [payload] Event payload
  21. * @returns
  22. */
  23. declare function emit(event: string, windowLabel?: WindowLabel, payload?: unknown): Promise<void>;
  24. /**
  25. * Listen to an event from the backend.
  26. *
  27. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  28. * @param handler Event handler callback.
  29. * @return A promise resolving to a function to unlisten to the event.
  30. */
  31. declare function listen<T>(event: EventName, windowLabel: string | null, handler: EventCallback<T>): Promise<UnlistenFn>;
  32. /**
  33. * Listen to an one-off event from the backend.
  34. *
  35. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  36. * @param handler Event handler callback.
  37. * @returns A promise resolving to a function to unlisten to the event.
  38. */
  39. declare function once<T>(event: EventName, windowLabel: string | null, handler: EventCallback<T>): Promise<UnlistenFn>;
  40. export { emit, listen, once };