-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
event.ts
178 lines (164 loc) · 4.98 KB
/
event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2019-2023 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 [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
* @module
*/
import { invoke, transformCallback } from './tauri'
interface Event<T> {
/** Event name */
event: EventName
/** The label of the window that emitted this event. */
windowLabel: string
/** Event identifier used to unlisten */
id: number
/** Event payload */
payload: T
}
type EventCallback<T> = (event: Event<T>) => void
type UnlistenFn = () => void
type EventName = `${TauriEvent}` | (string & Record<never, never>)
interface Options {
/**
* Label of the window the function targets.
*
* When listening to events and using this value,
* only events triggered by the window with the given label are received.
*
* When emitting events, only the window with the given label will receive it.
*/
target?: string
}
/**
* @since 1.1.0
*/
enum TauriEvent {
WINDOW_RESIZED = 'tauri://resize',
WINDOW_MOVED = 'tauri://move',
WINDOW_CLOSE_REQUESTED = 'tauri://close-requested',
WINDOW_CREATED = 'tauri://window-created',
WINDOW_DESTROYED = 'tauri://destroyed',
WINDOW_FOCUS = 'tauri://focus',
WINDOW_BLUR = 'tauri://blur',
WINDOW_SCALE_FACTOR_CHANGED = 'tauri://scale-change',
WINDOW_THEME_CHANGED = 'tauri://theme-changed',
WINDOW_FILE_DROP = 'tauri://file-drop',
WINDOW_FILE_DROP_HOVER = 'tauri://file-drop-hover',
WINDOW_FILE_DROP_CANCELLED = 'tauri://file-drop-cancelled',
MENU = 'tauri://menu'
}
/**
* Unregister the event listener associated with the given name and id.
*
* @ignore
* @param event The event name
* @param eventId Event identifier
* @returns
*/
async function _unlisten(event: string, eventId: number): Promise<void> {
await invoke('plugin:event|unlisten', {
event,
eventId
})
}
/**
* Listen to an event. The event can be either global or window-specific.
* See {@link Event.windowLabel} to check the event source.
*
* @example
* ```typescript
* 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();
* ```
*
* @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.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*
* @since 1.0.0
*/
async function listen<T>(
event: EventName,
handler: EventCallback<T>,
options?: Options
): Promise<UnlistenFn> {
return invoke<number>('plugin:event|listen', {
event,
windowLabel: options?.target,
handler: transformCallback(handler)
}).then((eventId) => {
return async () => _unlisten(event, eventId)
})
}
/**
* Listen to an one-off event. See {@link listen} for more information.
*
* @example
* ```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, 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();
* ```
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @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.
*
* @since 1.0.0
*/
async function once<T>(
event: EventName,
handler: EventCallback<T>,
options?: Options
): Promise<UnlistenFn> {
return listen<T>(
event,
(eventData) => {
handler(eventData)
_unlisten(event, eventData.id).catch(() => {})
},
options
)
}
/**
* Emits an event to the backend and all Tauri windows.
* @example
* ```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 `_`.
*
* @since 1.0.0
*/
async function emit(
event: string,
payload?: unknown,
options?: Options
): Promise<void> {
await invoke('plugin:event|emit', {
event,
windowLabel: options?.target,
payload
})
}
export type { Event, EventCallback, UnlistenFn, EventName, Options }
export { listen, once, emit, TauriEvent }