feat: update to 2.0.0-alpha.10 (#441)

This commit is contained in:
Lucas Fernandes Nogueira
2023-06-17 14:54:35 -07:00
committed by GitHub
parent 9359b5b138
commit 32c2a441c3
38 changed files with 847 additions and 974 deletions
-107
View File
@@ -1,107 +0,0 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
__TAURI__: {
transformCallback: <T>(cb: (payload: T) => void) => number;
};
}
}
export interface Event<T> {
/** Event name */
event: string;
/** The label of the window that emitted this event. */
windowLabel: string;
/** Event identifier used to unlisten */
id: number;
/** Event payload */
payload: T;
}
export type EventCallback<T> = (event: Event<T>) => void;
export type UnlistenFn = () => void;
/**
* 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 window.__TAURI_INVOKE__("plugin:event|unlisten", {
event,
eventId,
});
}
/**
* Emits an event to the backend.
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param [windowLabel] The label of the window to which the event is sent, if null/undefined the event will be sent to all windows
* @param [payload] Event payload
* @returns
*/
async function emit(
event: string,
windowLabel?: string,
payload?: unknown
): Promise<void> {
await window.__TAURI_INVOKE__("plugin:event|emit", {
event,
windowLabel,
payload,
});
}
/**
* Listen to an event from the backend.
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param handler Event handler callback.
* @return A promise resolving to a function to unlisten to the event.
*/
async function listen<T>(
event: string,
windowLabel: string | null,
handler: EventCallback<T>
): Promise<UnlistenFn> {
return window
.__TAURI_INVOKE__<number>("plugin:event|listen", {
event,
windowLabel,
handler: window.__TAURI__.transformCallback(handler),
})
.then((eventId) => {
return async () => _unlisten(event, eventId);
});
}
/**
* Listen to an one-off event from the backend.
*
* @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.
*/
async function once<T>(
event: string,
windowLabel: string | null,
handler: EventCallback<T>
): Promise<UnlistenFn> {
return listen<T>(event, windowLabel, (eventData) => {
handler(eventData);
_unlisten(event, eventData.id).catch(() => {
// do nothing
});
});
}
export { emit, listen, once };
+4 -6
View File
@@ -22,9 +22,7 @@ import type {
EventCallback,
UnlistenFn,
} from "@tauri-apps/api/event";
import { TauriEvent } from "@tauri-apps/api/event";
// TODO: use from @tauri-apps/api v2
import { emit, listen, once } from "./event";
import { TauriEvent, emit, listen, once } from "@tauri-apps/api/event";
declare global {
interface Window {
@@ -320,7 +318,7 @@ class WebviewWindowHandle {
listeners.splice(listeners.indexOf(handler), 1);
});
}
return listen(event, this.label, handler);
return listen(event, handler, { target: this.label });
}
/**
@@ -352,7 +350,7 @@ class WebviewWindowHandle {
listeners.splice(listeners.indexOf(handler), 1);
});
}
return once(event, this.label, handler);
return once(event, handler, { target: this.label });
}
/**
@@ -374,7 +372,7 @@ class WebviewWindowHandle {
}
return Promise.resolve();
}
return emit(event, this.label, payload);
return emit(event, payload, { target: this.label });
}
/** @ignore */
+1 -1
View File
@@ -27,6 +27,6 @@
"tslib": "^2.5.0"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-alpha.4"
"@tauri-apps/api": "2.0.0-alpha.5"
}
}
File diff suppressed because one or more lines are too long