impl events

pull/352/head
Lucas Nogueira 2 years ago
parent c5b1fc97a6
commit 2469c8a74c
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

@ -3604,7 +3604,6 @@ checksum = "fd1ba337640d60c3e96bc6f0638a939b9c9a7f2c316a1598c279828b3d1dc8c5"
[[package]] [[package]]
name = "tauri" name = "tauri"
version = "2.0.0-alpha.8" version = "2.0.0-alpha.8"
source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytes", "bytes",
@ -3660,7 +3659,6 @@ dependencies = [
[[package]] [[package]]
name = "tauri-build" name = "tauri-build"
version = "2.0.0-alpha.4" version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cargo_toml", "cargo_toml",
@ -3681,7 +3679,6 @@ dependencies = [
[[package]] [[package]]
name = "tauri-codegen" name = "tauri-codegen"
version = "2.0.0-alpha.4" version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b"
dependencies = [ dependencies = [
"base64 0.21.0", "base64 0.21.0",
"brotli", "brotli",
@ -3706,7 +3703,6 @@ dependencies = [
[[package]] [[package]]
name = "tauri-macros" name = "tauri-macros"
version = "2.0.0-alpha.4" version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b"
dependencies = [ dependencies = [
"heck 0.4.1", "heck 0.4.1",
"proc-macro2", "proc-macro2",
@ -3907,7 +3903,6 @@ dependencies = [
[[package]] [[package]]
name = "tauri-runtime" name = "tauri-runtime"
version = "0.13.0-alpha.4" version = "0.13.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b"
dependencies = [ dependencies = [
"gtk", "gtk",
"http", "http",
@ -3928,7 +3923,6 @@ dependencies = [
[[package]] [[package]]
name = "tauri-runtime-wry" name = "tauri-runtime-wry"
version = "0.13.0-alpha.4" version = "0.13.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b"
dependencies = [ dependencies = [
"cocoa", "cocoa",
"gtk", "gtk",
@ -3948,7 +3942,6 @@ dependencies = [
[[package]] [[package]]
name = "tauri-utils" name = "tauri-utils"
version = "2.0.0-alpha.4" version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b"
dependencies = [ dependencies = [
"aes-gcm", "aes-gcm",
"brotli", "brotli",

@ -31,8 +31,14 @@ tauri-plugin-updater = { path = "../../../plugins/updater" }
tauri-plugin-window = { path = "../../../plugins/window" } tauri-plugin-window = { path = "../../../plugins/window" }
[patch.crates-io] [patch.crates-io]
tauri = { git = "https://github.com/tauri-apps/tauri", branch = "next" } #tauri = { git = "https://github.com/tauri-apps/tauri", branch = "next" }
tauri-build = { git = "https://github.com/tauri-apps/tauri", branch = "next" } #tauri-build = { git = "https://github.com/tauri-apps/tauri", branch = "next" }
tauri = { path = "../../../../tauri/core/tauri" }
tauri-build = { path = "../../../../tauri/core/tauri-build" }
[patch.'https://github.com/tauri-apps/tauri']
tauri = { path = "../../../../tauri/core/tauri" }
tauri-build = { path = "../../../../tauri/core/tauri-build" }
[dependencies.tauri] [dependencies.tauri]
version = "2.0.0-alpha.8" version = "2.0.0-alpha.8"

@ -20,7 +20,6 @@
import App from "./views/App.svelte"; import App from "./views/App.svelte";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { listen } from "@tauri-apps/api/event";
import { ask } from "tauri-plugin-dialog-api"; import { ask } from "tauri-plugin-dialog-api";
if (appWindow.label !== "main") { if (appWindow.label !== "main") {
@ -124,7 +123,7 @@
onMount(async () => { onMount(async () => {
const window = getCurrent(); const window = getCurrent();
isWindowMaximized = await window.isMaximized(); isWindowMaximized = await window.isMaximized();
listen("tauri://resize", async () => { window.onResized(async () => {
isWindowMaximized = await window.isMaximized(); isWindowMaximized = await window.isMaximized();
}); });
}); });

@ -1,41 +1,41 @@
<script> <script>
import { listen, emit } from '@tauri-apps/api/event' import { appWindow } from "tauri-plugin-window-api";
import { invoke } from '@tauri-apps/api/tauri' import { invoke } from "@tauri-apps/api/tauri";
import { onMount, onDestroy } from 'svelte' import { onMount, onDestroy } from "svelte";
export let onMessage export let onMessage;
let unlisten let unlisten;
onMount(async () => { onMount(async () => {
unlisten = await listen('rust-event', onMessage) unlisten = await appWindow.listen("rust-event", onMessage);
}) });
onDestroy(() => { onDestroy(() => {
if (unlisten) { if (unlisten) {
unlisten() unlisten();
} }
}) });
function log() { function log() {
invoke('log_operation', { invoke("log_operation", {
event: 'tauri-click', event: "tauri-click",
payload: 'this payload is optional because we used Option in Rust' payload: "this payload is optional because we used Option in Rust",
}) });
} }
function performRequest() { function performRequest() {
invoke('perform_request', { invoke("perform_request", {
endpoint: 'dummy endpoint arg', endpoint: "dummy endpoint arg",
body: { body: {
id: 5, id: 5,
name: 'test' name: "test",
} },
}) })
.then(onMessage) .then(onMessage)
.catch(onMessage) .catch(onMessage);
} }
function emitEvent() { function emitEvent() {
emit('js-event', 'this is the payload string') appWindow.emit("js-event", "this is the payload string");
} }
</script> </script>

@ -21,17 +21,17 @@ export type RawEvent = {
type RawEventKind = type RawEventKind =
| "any " | "any "
| { | {
access?: unknown; access?: unknown;
} }
| { | {
create?: unknown; create?: unknown;
} }
| { | {
modify?: unknown; modify?: unknown;
} }
| { | {
remove?: unknown; remove?: unknown;
} }
| "other"; | "other";
export type DebouncedEvent = export type DebouncedEvent =

@ -0,0 +1,98 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { invoke, transformCallback } from "@tauri-apps/api/tauri";
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 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 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 invoke<number>("plugin:event|listen", {
event,
windowLabel,
handler: 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 };

File diff suppressed because it is too large Load Diff

@ -29,4 +29,4 @@
"dependencies": { "dependencies": {
"@tauri-apps/api": "^1.2.0" "@tauri-apps/api": "^1.2.0"
} }
} }

Loading…
Cancel
Save