Skip to main content

event

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 build.withGlobalTauri in tauri.conf.json is set to true.

Enumerations​

TauriEvent​

Since: 1.1.0

Enumeration Members​

NameTypeDefined in
"tauri://update"event.ts:34
"tauri://update-download-progress"event.ts:38
"tauri://update-install"event.ts:36
"tauri://menu"event.ts:33
"tauri://update-status"event.ts:37
"tauri://update-available"event.ts:35
"tauri://blur"event.ts:27
"tauri://close-requested"event.ts:23
"tauri://window-created"event.ts:24
"tauri://destroyed"event.ts:25
"tauri://file-drop"event.ts:30
"tauri://file-drop-cancelled"event.ts:32
"tauri://file-drop-hover"event.ts:31
"tauri://focus"event.ts:26
"tauri://move"event.ts:22
"tauri://resize"event.ts:21
"tauri://scale-change"event.ts:28
"tauri://theme-changed"event.ts:29

Interfaces​

Event<T>​

Type parameters

  • T

Properties​

event​

event: EventName

Event name

Defined in: helpers/event.ts:12

id​

id: number

Event identifier used to unlisten

Defined in: helpers/event.ts:16

payload​

payload: T

Event payload

Defined in: helpers/event.ts:18

windowLabel​

windowLabel: string

The label of the window that emitted this event.

Defined in: helpers/event.ts:14

Type Aliases​

EventCallback<T>​

EventCallback<T>: (event: Event<T>) => void

Type parameters

  • T

Type declaration

(event: Event<T>): void

Parameters

NameType
eventEvent<T>

Returns: void

Defined in: helpers/event.ts:21

EventName​

EventName: ${TauriEvent} | string & Record<never, never>

Defined in: event.ts:15

UnlistenFn​

UnlistenFn: () => void

Type declaration

(): void

Returns: void

Defined in: helpers/event.ts:23

Functions​

emit​

emit(event: string, payload?: unknown): Promise<void>

Emits an event to the backend and all Tauri windows.

Example

import { emit } from '@tauri-apps/api/event';
await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });

Since: 1.0.0

Parameters

NameTypeDescription
eventstringEvent name. Must include only alphanumeric characters, -, /, : and _.
payload?unknown-

Returns: Promise<void>

listen​

listen<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>

Listen to an event. The event can be either global or window-specific. See windowLabel to check the event source.

Example

import { listen } from '@tauri-apps/api/event';
const unlisten = await listen<string>('error', (event) => {
console.log(`Got error in window ${event.windowLabel}, payload: ${event.payload}`);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Since: 1.0.0

Type parameters

  • T

Parameters

NameTypeDescription
eventEventNameEvent name. Must include only alphanumeric characters, -, /, : and _.
handlerEventCallback<T>Event handler callback.

Returns: Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.

once​

once<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>

Listen to an one-off event. See listen for more information.

Example

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, loggedIn: ${event.payload.loggedIn}, token: ${event.payload.token}`);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Since: 1.0.0

Type parameters

  • T

Parameters

NameTypeDescription
eventEventNameEvent name. Must include only alphanumeric characters, -, /, : and _.
handlerEventCallback<T>-

Returns: Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.