File size: 1,171 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<script>
  import { getCurrentWebview } from "@tauri-apps/api/webview";
  import { invoke } from "@tauri-apps/api/core";
  import { onMount, onDestroy } from "svelte";

  const webview = getCurrentWebview();

  export let onMessage;
  let unlisten;

  onMount(async () => {
    unlisten = await webview.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() {
    webview.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>