Skip to content

Commit

Permalink
Await event listeners and unlisten on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinez committed Oct 1, 2024
1 parent d4825ab commit 5016898
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { onMount } from "svelte";
import { onDestroy, onMount } from "svelte";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
Expand All @@ -18,22 +18,26 @@
const activeRouteStore = router.activeRouteStore;
type UnlistenFn = () => void;
let unlisten_events: UnlistenFn | undefined = undefined;

Check warning on line 22 in src/App.svelte

View workflow job for this annotation

GitHub Actions / lint typescript

Variable name `unlisten_events` must match one of the following formats: camelCase
let unlisten_node: UnlistenFn | undefined = undefined;

Check warning on line 23 in src/App.svelte

View workflow job for this annotation

GitHub Actions / lint typescript

Variable name `unlisten_node` must match one of the following formats: camelCase
onMount(async () => {
try {
await listen("event", event => {
console.log(event.payload);
});
unlisten_events = await listen("event", event => {
console.log(event.payload);
});
await listen<boolean>("node_running", event => {
nodeRunning.set(event.payload);
});
unlisten_node = await listen<boolean>("node_running", event => {
nodeRunning.set(event.payload);
});
try {
// For development purposes don't run tauri commands when viewing from
// a browser.
if (window.__TAURI_INTERNALS__) {
await invoke("authenticate");
}
void router.loadFromLocation();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
void router.push({
Expand All @@ -43,9 +47,16 @@
hint: e.hint,
},
});
} finally {
void router.loadFromLocation();
}
});
onDestroy(() => {
if (unlisten_node) unlisten_node();
if (unlisten_events) unlisten_events();
});
$: document.documentElement.setAttribute("data-theme", $theme);
</script>

Expand Down

0 comments on commit 5016898

Please sign in to comment.