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 app.withGlobalTauri
in tauri.conf.json
is set to true
.
Enumerations
TauriEvent
Since
1.1.0
Enumeration Members
DRAG
DRAG: 'tauri://drag';
Source: event.ts:60
DROP
DROP: 'tauri://drop';
Source: event.ts:61
DROP_CANCELLED
DROP_CANCELLED: 'tauri://drag-cancelled';
Source: event.ts:63
DROP_OVER
DROP_OVER: 'tauri://drop-over';
Source: event.ts:62
WEBVIEW_CREATED
WEBVIEW_CREATED: 'tauri://webview-created';
Source: event.ts:59
WINDOW_BLUR
WINDOW_BLUR: 'tauri://blur';
Source: event.ts:55
WINDOW_CLOSE_REQUESTED
WINDOW_CLOSE_REQUESTED: 'tauri://close-requested';
Source: event.ts:52
WINDOW_CREATED
WINDOW_CREATED: 'tauri://window-created';
Source: event.ts:58
WINDOW_DESTROYED
WINDOW_DESTROYED: 'tauri://destroyed';
Source: event.ts:53
WINDOW_FOCUS
WINDOW_FOCUS: 'tauri://focus';
Source: event.ts:54
WINDOW_MOVED
WINDOW_MOVED: 'tauri://move';
Source: event.ts:51
WINDOW_RESIZED
WINDOW_RESIZED: 'tauri://resize';
Source: event.ts:50
WINDOW_SCALE_FACTOR_CHANGED
WINDOW_SCALE_FACTOR_CHANGED: 'tauri://scale-change';
Source: event.ts:56
WINDOW_THEME_CHANGED
WINDOW_THEME_CHANGED: 'tauri://theme-changed';
Source: event.ts:57
Interfaces
Event
Type parameters
Parameter |
---|
T |
Properties
Property | Type | Description |
---|---|---|
event | EventName | Event name |
id | number | Event identifier used to unlisten |
payload | T | Event payload |
Options
Properties
Property | Type | Description |
---|---|---|
target ? | string | EventTarget | The event target to listen to, defaults to { kind: 'Any' } , see EventTarget.If a string is provided, EventTarget.AnyLabel is used. |
Type Aliases
EventCallback
EventCallback: <T> (event) => void
Type parameters
Parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
event | Event < T > |
Returns
void
Source: event.ts:31
EventName
EventName: \${TauriEvent}\ | string & Record< never, never >
Source: event.ts:35
EventTarget
EventTarget: {kind: "Any";} | {kind: "AnyLabel"; label: string;} | {kind: "App";} | {kind: "Window"; label: string;} | {kind: "Webview"; label: string;} | {kind: "WebviewWindow"; label: string;}
Source: event.ts:14
UnlistenFn
UnlistenFn: () => void
Returns
void
Source: event.ts:33
Functions
emit()
emit(event, payload?): Promise< void >
Emits an event to all targets.
Example
import { emit } from '@tauri-apps/api/event';await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });
Since
1.0.0
Parameters
Parameter | Type | Description |
---|---|---|
event | string | Event name. Must include only alphanumeric characters, - , / , : and _ . |
payload ? | unknown | Event payload. |
Returns
Promise
< void
>
Source: event.ts:176
emitTo()
emitTo( target, event, payload?): Promise< void >
Emits an event to all targets matching the given target.
Example
import { emitTo } from '@tauri-apps/api/event';await emitTo('main', 'frontend-loaded', { loggedIn: true, token: 'authToken' });
Since
1.0.0
Parameters
Parameter | Type | Description |
---|---|---|
target | string | EventTarget | Label of the target Window/Webview/WebviewWindow or raw EventTarget object. |
event | string | Event name. Must include only alphanumeric characters, - , / , : and _ . |
payload ? | unknown | Event payload. |
Returns
Promise
< void
>
Source: event.ts:198
listen()
listen<T>( event, handler, options?): Promise< UnlistenFn >
Listen to an emitted event to any target.
Example
import { listen } from '@tauri-apps/api/event';const unlisten = await listen<string>('error', (event) => { console.log(`Got error, payload: ${event.payload}`);});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmountedunlisten();
Since
1.0.0
Type parameters
Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
event | EventName | Event name. Must include only alphanumeric characters, - , / , : and _ . |
handler | EventCallback < T > | Event handler callback. |
options ? | Options | Event listening options. |
Returns
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.
Source: event.ts:103
once()
once<T>( event, handler, options?): Promise< UnlistenFn >
Listens once to an emitted event to any target.
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 unmountedunlisten();
Since
1.0.0
Type parameters
Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
event | EventName | Event name. Must include only alphanumeric characters, - , / , : and _ . |
handler | EventCallback < T > | Event handler callback. |
options ? | Options | Event listening options. |
Returns
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.
Source: event.ts:147
© 2024 Tauri Contributors. CC-BY / MIT