-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
32 lines (28 loc) · 848 Bytes
/
sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { addEventListener, sync, caches, index, dispatchEvent } from "@virtualstate/internal";
addEventListener("sync", ({ tag, waitUntil }) => {
if (tag === "images") {
waitUntil(onSyncImages());
}
async function onSyncImages() {
const cache = await caches.open("contentIndex");
for (const { id, icons } of await index.getAll()) {
for (const { src } of icons) {
console.log(`Updating icon "${src}" for ${id}`);
await cache.put(
src,
await fetch(src)
);
}
}
}
});
await sync.register("images");
// Ran elsewhere by scheduler
// Is usually managed by generateVirtualSyncEvents
await dispatchEvent({
type: "sync",
tag: "images",
schedule: {
immediate: true
}
});