Skip to content

Commit

Permalink
Move node event listeners to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolfs committed Jan 27, 2025
1 parent d96043d commit a720e8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
29 changes: 6 additions & 23 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<script lang="ts">
import type { UnlistenFn } from "@tauri-apps/api/event";
import { listen } from "@tauri-apps/api/event";
import { onDestroy, onMount } from "svelte";
import * as router from "@app/lib/router";
import { nodeRunning } from "@app/lib/events";
import { theme } from "@app/components/ThemeSwitch.svelte";
import { unreachable } from "@app/lib/utils";
Expand All @@ -18,38 +14,25 @@
import Patches from "@app/views/repo/Patches.svelte";
import Repos from "./views/home/Repos.svelte";
import { checkAuthPeriodically } from "./lib/auth";
import {
registerNodeEventListeners,
unregisterNodeEventListeners,
} from "./lib/events";
import {
registerNotificationBadgePoll,
unregisterNotificationBadgePoll,
} from "./lib/notification/appBadge";
const activeRouteStore = router.activeRouteStore;
let unlistenEvents: UnlistenFn | undefined = undefined;
let unlistenNodeEvents: UnlistenFn | undefined = undefined;
onMount(async () => {
if (window.__TAURI_INTERNALS__) {
unlistenEvents = await listen("event", () => {
// Add handler for incoming events
});
unlistenNodeEvents = await listen<boolean>("node_running", event => {
nodeRunning.set(event.payload);
});
}
await registerNodeEventListeners();
await checkAuthPeriodically(true);
await registerNotificationBadgePoll();
});
onDestroy(() => {
if (unlistenEvents) {
unlistenEvents();
}
if (unlistenNodeEvents) {
unlistenNodeEvents();
}
unregisterNodeEventListeners();
unregisterNotificationBadgePoll();
});
Expand Down
27 changes: 27 additions & 0 deletions src/lib/events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
import type { UnlistenFn } from "@tauri-apps/api/event";

import { listen } from "@tauri-apps/api/event";
import { writable } from "svelte/store";

export const nodeRunning = writable<boolean>(false);

let unlistenEvents: UnlistenFn | undefined = undefined;
let unlistenNodeEvents: UnlistenFn | undefined = undefined;

export async function registerNodeEventListeners() {
if (window.__TAURI_INTERNALS__) {
unlistenEvents = await listen("event", () => {
// Add handler for incoming events
});

unlistenNodeEvents = await listen<boolean>("node_running", event => {
nodeRunning.set(event.payload);
});
}
}

export function unregisterNodeEventListeners() {
if (unlistenEvents) {
unlistenEvents();
}
if (unlistenNodeEvents) {
unlistenNodeEvents();
}
}

0 comments on commit a720e8a

Please sign in to comment.