Skip to content

Commit

Permalink
refactor(api): move event's once to its own function (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Feb 24, 2021
1 parent 8a12068 commit 372036c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changes/js-event-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-api": minor
---

The event listener `once` kind was moved to a dedicated function.
2 changes: 1 addition & 1 deletion api/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ async function emit(event: string, payload?: string): Promise<void> {
return emitEvent(event, undefined, payload)
}

export { listen } from './helpers/event'
export { listen, once } from './helpers/event'
export { emit }
38 changes: 29 additions & 9 deletions api/src/helpers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ export interface Event<T> {

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

/**
* listen to an event from the backend
*
* @param event the event name
* @param handler the event handler callback
*/
async function listen<T>(
async function _listen<T>(
event: string,
handler: EventCallback<T>,
once = false
once: boolean
): Promise<void> {
await invoke({
__tauriModule: 'Event',
Expand All @@ -29,6 +23,32 @@ async function listen<T>(
})
}

/**
* listen to an event from the backend
*
* @param event the event name
* @param handler the event handler callback
*/
async function listen<T>(
event: string,
handler: EventCallback<T>
): Promise<void> {
return _listen(event, handler, false)
}

/**
* listen to an one-off event from the backend
*
* @param event the event name
* @param handler the event handler callback
*/
async function once<T>(
event: string,
handler: EventCallback<T>
): Promise<void> {
return _listen(event, handler, true)
}

/**
* emits an event to the backend
*
Expand All @@ -51,4 +71,4 @@ async function emit(
})
}

export { listen, emit }
export { listen, once, emit }
21 changes: 16 additions & 5 deletions api/src/window.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { invoke } from './tauri'
import { EventCallback, emit, listen } from './helpers/event'
import { EventCallback, emit, listen, once } from './helpers/event'

interface WindowDef {
label: string
Expand Down Expand Up @@ -33,14 +33,25 @@ class TauriWindow {
*
* @param event the event name
* @param handler the event handler callback
* @param once unlisten after the first trigger if true
*/
async listen<T>(
event: string,
handler: EventCallback<T>,
once = false
handler: EventCallback<T>
): Promise<void> {
return listen(event, handler, once)
return listen(event, handler)
}

/**
* Listen to an one-off event emitted by the webview
*
* @param event the event name
* @param handler the event handler callback
*/
async once<T>(
event: string,
handler: EventCallback<T>
): Promise<void> {
return once(event, handler)
}

/**
Expand Down

0 comments on commit 372036c

Please sign in to comment.