123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
- // SPDX-License-Identifier: Apache-2.0
- // SPDX-License-Identifier: MIT
- /**
- * The event system allows you to emit events to the backend and listen to events from it.
- *
- * This package is also accessible with `window.__TAURI__.event` when `tauri.conf.json > build > withGlobalTauri` is set to true.
- * @module
- */
- import * as eventApi from './helpers/event'
- import type {
- EventName,
- EventCallback,
- UnlistenFn,
- Event
- } from './helpers/event'
- /**
- * Listen to an event from the backend.
- * @example Listen to the `error` event expecting a string payload
- * ```typescript
- * import { listen } from '@tauri-apps/api/event';
- * const unlisten = await listen<string>('error', (event) => {
- * console.log(`Got error in window ${event.windowLabel}, payload: ${payload}`);
- * });
- *
- * // removes the listener later
- * await unlisten();
- * ```
- *
- * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
- * @param handler Event handler callback.
- * @return A promise resolving to a function to unlisten to the event.
- */
- async function listen<T>(
- event: EventName,
- handler: EventCallback<T>
- ): Promise<UnlistenFn> {
- return eventApi.listen(event, null, handler)
- }
- /**
- * Listen to an one-off event from the backend.
- * @example Listen to the `loaded` event that is only triggered once
- * ```typescript
- * import { once } from '@tauri-apps/api/event';
- * interface LoadedPayload {
- * loggedIn: boolean,
- * token: string
- * }
- * const unlisten = await once<LoadedPayload>('loaded', (event) => {
- * console.log(`App is loaded, logggedIn: ${event.payload.loggedIn}, token: ${event.payload.token}`);
- * });
- * ```
- *
- * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
- * @param handler Event handler callback.
- * @returns A promise resolving to a function to unlisten to the event.
- */
- async function once<T>(
- event: EventName,
- handler: EventCallback<T>
- ): Promise<UnlistenFn> {
- return eventApi.once(event, null, handler)
- }
- /**
- * Emits an event to the backend.
- * @example Emits the `frontend-loaded` event with the given payload
- * ```typescript
- * import { emit } from '@tauri-apps/api/event';
- * await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });
- * ```
- *
- * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
- * @param [payload] Event payload
- * @returns
- */
- async function emit(event: string, payload?: unknown): Promise<void> {
- return eventApi.emit(event, undefined, payload)
- }
- export type { Event, EventName, EventCallback, UnlistenFn }
- export { listen, once, emit }
|