// 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('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( event: EventName, handler: EventCallback ): Promise { 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('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( event: EventName, handler: EventCallback ): Promise { 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 { return eventApi.emit(event, undefined, payload) } export type { Event, EventName, EventCallback, UnlistenFn } export { listen, once, emit }