From 372036ce20ac7f103dea05bae7e8686858d096a4 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Tue, 23 Feb 2021 21:31:15 -0300 Subject: [PATCH] refactor(api): move event's `once` to its own function (#1276) --- .changes/js-event-once.md | 5 +++++ api/src/event.ts | 2 +- api/src/helpers/event.ts | 38 +++++++++++++++++++++++++++++--------- api/src/window.ts | 21 ++++++++++++++++----- 4 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 .changes/js-event-once.md diff --git a/.changes/js-event-once.md b/.changes/js-event-once.md new file mode 100644 index 000000000..ecd847cc4 --- /dev/null +++ b/.changes/js-event-once.md @@ -0,0 +1,5 @@ +--- +"tauri-api": minor +--- + +The event listener `once` kind was moved to a dedicated function. diff --git a/api/src/event.ts b/api/src/event.ts index 14572cfc0..640279e70 100644 --- a/api/src/event.ts +++ b/api/src/event.ts @@ -4,5 +4,5 @@ async function emit(event: string, payload?: string): Promise { return emitEvent(event, undefined, payload) } -export { listen } from './helpers/event' +export { listen, once } from './helpers/event' export { emit } diff --git a/api/src/helpers/event.ts b/api/src/helpers/event.ts index cd8cd8cd8..3f02315fb 100644 --- a/api/src/helpers/event.ts +++ b/api/src/helpers/event.ts @@ -7,16 +7,10 @@ export interface Event { export type EventCallback = (event: Event) => void -/** - * listen to an event from the backend - * - * @param event the event name - * @param handler the event handler callback - */ -async function listen( +async function _listen( event: string, handler: EventCallback, - once = false + once: boolean ): Promise { await invoke({ __tauriModule: 'Event', @@ -29,6 +23,32 @@ async function listen( }) } +/** + * listen to an event from the backend + * + * @param event the event name + * @param handler the event handler callback + */ +async function listen( + event: string, + handler: EventCallback +): Promise { + 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( + event: string, + handler: EventCallback +): Promise { + return _listen(event, handler, true) +} + /** * emits an event to the backend * @@ -51,4 +71,4 @@ async function emit( }) } -export { listen, emit } +export { listen, once, emit } diff --git a/api/src/window.ts b/api/src/window.ts index 2d71dafb2..061971ea4 100644 --- a/api/src/window.ts +++ b/api/src/window.ts @@ -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 @@ -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( event: string, - handler: EventCallback, - once = false + handler: EventCallback ): Promise { - 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( + event: string, + handler: EventCallback + ): Promise { + return once(event, handler) } /**