Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Move badge polling to its own module
Rūdolfs Ošiņš committed 1 year ago
commit d96043d7046db5dd7664e89867b34ec11061769e
parent af776b554f8575d7f67b83d116562f0246943a3c
2 files changed +56 -44
modified src/App.svelte
@@ -1,14 +1,8 @@
<script lang="ts">
  import type { UnlistenFn } from "@tauri-apps/api/event";
-
  import type { NotificationCount } from "@bindings/cob/inbox/NotificationCount";

-
  import { onDestroy, onMount } from "svelte";
-
  import { SvelteMap } from "svelte/reactivity";
-
  import sum from "lodash/sum";
-
  import { platform } from "@tauri-apps/plugin-os";
-

-
  import { invoke } from "@app/lib/invoke";
  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";
@@ -23,14 +17,16 @@
  import Patch from "@app/views/repo/Patch.svelte";
  import Patches from "@app/views/repo/Patches.svelte";
  import Repos from "./views/home/Repos.svelte";
-
  import { authenticated, checkAuthPeriodically } from "./lib/auth";
+
  import { checkAuthPeriodically } from "./lib/auth";
+
  import {
+
    registerNotificationBadgePoll,
+
    unregisterNotificationBadgePoll,
+
  } from "./lib/notification/appBadge";

  const activeRouteStore = router.activeRouteStore;

  let unlistenEvents: UnlistenFn | undefined = undefined;
  let unlistenNodeEvents: UnlistenFn | undefined = undefined;
-
  let notificationPoll: ReturnType<typeof setInterval> | undefined = undefined;
-
  let pollingNotificationsInProgress: boolean = false;

  onMount(async () => {
    if (window.__TAURI_INTERNALS__) {
@@ -44,39 +40,9 @@
    }

    await checkAuthPeriodically(true);
-

-
    if (window.__TAURI_OS_PLUGIN_INTERNALS__ && platform() === "macos") {
-
      await setNotificationBadge();
-
      notificationPoll = setInterval(async () => {
-
        if (
-
          pollingNotificationsInProgress ||
-
          !$nodeRunning ||
-
          !$authenticated
-
        ) {
-
          return;
-
        }
-

-
        await setNotificationBadge();
-
      }, 5_000);
-
    }
+
    await registerNotificationBadgePoll();
  });

-
  async function setNotificationBadge() {
-
    try {
-
      pollingNotificationsInProgress = true;
-

-
      const count = await invoke<Record<string, NotificationCount>>(
-
        "count_notifications_by_repo",
-
      );
-
      const notificationCount = new SvelteMap(Object.entries(count));
-
      await invoke("set_badge", {
-
        count: sum(Array.from(notificationCount.values()).map(c => c.count)),
-
      });
-
    } finally {
-
      pollingNotificationsInProgress = false;
-
    }
-
  }
-

  onDestroy(() => {
    if (unlistenEvents) {
      unlistenEvents();
@@ -84,9 +50,7 @@
    if (unlistenNodeEvents) {
      unlistenNodeEvents();
    }
-
    if (notificationPoll) {
-
      clearInterval(notificationPoll);
-
    }
+
    unregisterNotificationBadgePoll();
  });

  $effect(() => document.documentElement.setAttribute("data-theme", $theme));
added src/lib/notification/appBadge.ts
@@ -0,0 +1,48 @@
+
import type { NotificationCount } from "@bindings/cob/inbox/NotificationCount";
+

+
import sum from "lodash/sum";
+
import { SvelteMap } from "svelte/reactivity";
+
import { get } from "svelte/store";
+
import { platform } from "@tauri-apps/plugin-os";
+

+
import { authenticated } from "@app/lib/auth";
+
import { invoke } from "@app/lib/invoke";
+
import { nodeRunning } from "@app/lib/events";
+

+
let pollHandle: ReturnType<typeof setInterval> | undefined = undefined;
+
let pollingInProgress: boolean = false;
+

+
async function setNotificationBadge() {
+
  try {
+
    pollingInProgress = true;
+

+
    const count = await invoke<Record<string, NotificationCount>>(
+
      "count_notifications_by_repo",
+
    );
+
    const notificationCount = new SvelteMap(Object.entries(count));
+
    await invoke("set_badge", {
+
      count: sum(Array.from(notificationCount.values()).map(c => c.count)),
+
    });
+
  } finally {
+
    pollingInProgress = false;
+
  }
+
}
+

+
export async function registerNotificationBadgePoll() {
+
  if (window.__TAURI_OS_PLUGIN_INTERNALS__ && platform() === "macos") {
+
    await setNotificationBadge();
+
    pollHandle = setInterval(async () => {
+
      if (pollingInProgress || !get(nodeRunning) || !get(authenticated)) {
+
        return;
+
      }
+

+
      await setNotificationBadge();
+
    }, 5_000);
+
  }
+
}
+

+
export function unregisterNotificationBadgePoll() {
+
  if (pollHandle) {
+
    clearInterval(pollHandle);
+
  }
+
}