Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Move node event listeners to its own module
✓ CI success Rūdolfs Ošiņš committed 1 year ago
commit a720e8a9803e3252cc4bb703d9c54e73eabe59f8
parent d96043d7046db5dd7664e89867b34ec11061769e
1 passed (1 total) View logs
2 files changed +33 -23
modified src/App.svelte
@@ -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";

@@ -19,37 +15,24 @@
  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();
  });

modified src/lib/events.ts
@@ -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();
+
  }
+
}