Skip to content

Commit 372036c

Browse files
authored
refactor(api): move event's once to its own function (#1276)
1 parent 8a12068 commit 372036c

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

.changes/js-event-once.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-api": minor
3+
---
4+
5+
The event listener `once` kind was moved to a dedicated function.

api/src/event.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ async function emit(event: string, payload?: string): Promise<void> {
44
return emitEvent(event, undefined, payload)
55
}
66

7-
export { listen } from './helpers/event'
7+
export { listen, once } from './helpers/event'
88
export { emit }

api/src/helpers/event.ts

+29-9
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ export interface Event<T> {
77

88
export type EventCallback<T> = (event: Event<T>) => void
99

10-
/**
11-
* listen to an event from the backend
12-
*
13-
* @param event the event name
14-
* @param handler the event handler callback
15-
*/
16-
async function listen<T>(
10+
async function _listen<T>(
1711
event: string,
1812
handler: EventCallback<T>,
19-
once = false
13+
once: boolean
2014
): Promise<void> {
2115
await invoke({
2216
__tauriModule: 'Event',
@@ -29,6 +23,32 @@ async function listen<T>(
2923
})
3024
}
3125

26+
/**
27+
* listen to an event from the backend
28+
*
29+
* @param event the event name
30+
* @param handler the event handler callback
31+
*/
32+
async function listen<T>(
33+
event: string,
34+
handler: EventCallback<T>
35+
): Promise<void> {
36+
return _listen(event, handler, false)
37+
}
38+
39+
/**
40+
* listen to an one-off event from the backend
41+
*
42+
* @param event the event name
43+
* @param handler the event handler callback
44+
*/
45+
async function once<T>(
46+
event: string,
47+
handler: EventCallback<T>
48+
): Promise<void> {
49+
return _listen(event, handler, true)
50+
}
51+
3252
/**
3353
* emits an event to the backend
3454
*
@@ -51,4 +71,4 @@ async function emit(
5171
})
5272
}
5373

54-
export { listen, emit }
74+
export { listen, once, emit }

api/src/window.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { invoke } from './tauri'
2-
import { EventCallback, emit, listen } from './helpers/event'
2+
import { EventCallback, emit, listen, once } from './helpers/event'
33

44
interface WindowDef {
55
label: string
@@ -33,14 +33,25 @@ class TauriWindow {
3333
*
3434
* @param event the event name
3535
* @param handler the event handler callback
36-
* @param once unlisten after the first trigger if true
3736
*/
3837
async listen<T>(
3938
event: string,
40-
handler: EventCallback<T>,
41-
once = false
39+
handler: EventCallback<T>
4240
): Promise<void> {
43-
return listen(event, handler, once)
41+
return listen(event, handler)
42+
}
43+
44+
/**
45+
* Listen to an one-off event emitted by the webview
46+
*
47+
* @param event the event name
48+
* @param handler the event handler callback
49+
*/
50+
async once<T>(
51+
event: string,
52+
handler: EventCallback<T>
53+
): Promise<void> {
54+
return once(event, handler)
4455
}
4556

4657
/**

0 commit comments

Comments
 (0)