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

@ -31,8 +31,14 @@ tauri-plugin-updater = { path = "../../../plugins/updater" }
tauri-plugin-window = { path = "../../../plugins/window" }
[patch.crates-io]
tauri = { git = "https://github.com/tauri-apps/tauri", branch = "next" }
tauri-build = { 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 = { 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]
version = "2.0.0-alpha.8"

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

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

@ -21,17 +21,17 @@ export type RawEvent = {
type RawEventKind =
| "any "
| {
access?: unknown;
}
access?: unknown;
}
| {
create?: unknown;
}
create?: unknown;
}
| {
modify?: unknown;
}
modify?: unknown;
}
| {
remove?: unknown;
}
remove?: unknown;
}
| "other";
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": {
"@tauri-apps/api": "^1.2.0"
}
}
}

Loading…
Cancel
Save