Files
tauri-plugins-workspace/examples/api/src/views/Communication.svelte
T
Amr Bashir 84133b57b8 feat(window): refactor and improvements (#426)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
2023-08-07 13:20:17 -03:00

53 lines
1.1 KiB
Svelte

<script>
import { getCurrent } from "@tauri-apps/plugin-window";
import { invoke } from "@tauri-apps/api/tauri";
import { onMount, onDestroy } from "svelte";
const appWindow = getCurrent();
export let onMessage;
let unlisten;
onMount(async () => {
unlisten = await appWindow.listen("rust-event", onMessage);
});
onDestroy(() => {
if (unlisten) {
unlisten();
}
});
function log() {
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",
body: {
id: 5,
name: "test",
},
})
.then(onMessage)
.catch(onMessage);
}
function emitEvent() {
appWindow.emit("js-event", "this is the payload string");
}
</script>
<div>
<button class="btn" id="log" on:click={log}>Call Log API</button>
<button class="btn" id="request" on:click={performRequest}>
Call Request (async) API
</button>
<button class="btn" id="event" on:click={emitEvent}>
Send event to Rust
</button>
</div>