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β
Name | Type | Defined 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
Name | Type |
---|---|
event | Event <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
Name | Type | Description |
---|---|---|
event | string | Event 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
Name | Type | Description |
---|---|---|
event | EventName | Event name. Must include only alphanumeric characters, - , / , : and _ . |
handler | EventCallback <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
Name | Type | Description |
---|---|---|
event | EventName | Event name. Must include only alphanumeric characters, - , / , : and _ . |
handler | EventCallback <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.