-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiodic-sync.js
39 lines (35 loc) · 1.09 KB
/
periodic-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
33
34
35
36
37
38
39
import { addEventListener, periodicSync, caches, index, dispatchEvent } from "@virtualstate/internal";
addEventListener("periodicsync", ({ 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 periodicSync.register("images", {
minInterval: 5 * 60 * 1000 // Refresh every 5 minutes
});
// Ran elsewhere by scheduler
// Is usually managed by generatePeriodicSyncVirtualEvents
await dispatchEvent({
type: "periodicsync",
tag: "images",
schedule: {
immediate: true
// can give delay here or cron
// minInterval doesn't always mean a fixed rate
//
// delay: 5 * 60 * 1000,
// repeat: true
}
});