Skip to content

Commit

Permalink
Move badge polling 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 af776b5 commit d96043d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
52 changes: 8 additions & 44 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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__) {
Expand All @@ -44,49 +40,17 @@
}
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();
}
if (unlistenNodeEvents) {
unlistenNodeEvents();
}
if (notificationPoll) {
clearInterval(notificationPoll);
}
unregisterNotificationBadgePoll();
});
$effect(() => document.documentElement.setAttribute("data-theme", $theme));
Expand Down
48 changes: 48 additions & 0 deletions src/lib/notification/appBadge.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit d96043d

Please sign in to comment.