mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:33:28 +01:00
- desktop build clean, tsc clean, lint 0, home/ 89/89
- advances desktop-2/3: dispatches now produce durable events in ~/.fleetcrown/events.jsonl with proper runId/sentinel so stop-hooks and web control plane treat them as first-class
- still no embedded watcher (idle via co-running home/watcher.ts for now)
Refs: desktop-app.md phases, Cockpit.roadmap T1/T2, prior landing 2d98638
57 lines
1.8 KiB
JavaScript
57 lines
1.8 KiB
JavaScript
/**
|
|
* FleetCrown service worker — handles Web Push events.
|
|
*
|
|
* Scope: root ("/") so notifications fire regardless of which app page is open
|
|
* (or none — that's the point of background push).
|
|
*
|
|
* Two events:
|
|
* - "push" — show the OS notification using the payload body
|
|
* - "notificationclick" — bring an open tab to focus, or open one at the URL
|
|
*/
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(self.skipWaiting());
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|
|
|
|
self.addEventListener("push", (event) => {
|
|
let data = { title: "FleetCrown", body: "Agent update", url: "/control", tag: "fleetcrown" };
|
|
if (event.data) {
|
|
try { data = { ...data, ...event.data.json() }; }
|
|
catch { data.body = event.data.text() || data.body; }
|
|
}
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title, {
|
|
body: data.body,
|
|
tag: data.tag,
|
|
data: { url: data.url },
|
|
// Coalesce same-tag notifications instead of stacking; the user only
|
|
// needs to see the latest state for a given tab.
|
|
renotify: false,
|
|
}),
|
|
);
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
const url = event.notification.data?.url || "/control";
|
|
event.notification.close();
|
|
event.waitUntil((async () => {
|
|
const all = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
|
|
// Focus a FleetCrown tab if one's already open; navigate it to the target.
|
|
for (const client of all) {
|
|
try {
|
|
const u = new URL(client.url);
|
|
if (u.origin === self.location.origin) {
|
|
await client.focus();
|
|
if ("navigate" in client) await client.navigate(url);
|
|
return;
|
|
}
|
|
} catch { /* ignore */ }
|
|
}
|
|
await self.clients.openWindow(url);
|
|
})());
|
|
});
|