-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move badge polling to its own module
- Loading branch information
Showing
2 changed files
with
56 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |