-
Notifications
You must be signed in to change notification settings - Fork 83
/
sw.js
74 lines (67 loc) · 2.23 KB
/
sw.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
self.addEventListener('sync', function(sync_event) {
var responseData;
fetch(new Request("/background-sync/demo/sample.json", {cache: "no-store"}))
.then(response => {
if (response.status == 200) {
return response.text();
} else {
throw new Error("" + response.status + " " + response.statusText);
}
})
.then(responseText => {
responseData = JSON.parse(responseText);
return openBgSyncDB();
})
.then(db => updateSyncTime(db, "one-shot", +new Date()))
.then(syncCount => {
self.registration.showNotification("Sync fired! (" +syncCount +
") Fetched list v" + responseData.list_version);
})
.catch(err => {
self.registration.showNotification("Sync fired! There was an error.");
self.registration.showNotification(err.message);
postErrorToClients(err);
});
});
function openBgSyncDB() {
return new Promise((resolve, reject) => {
var open_request = indexedDB.open("BgSyncDemoDB", 1);
open_request.onerror = function(event) {
reject(new Error("Error opening database."));
};
open_request.onsuccess = function(event) {
resolve(event.target.result);
};
open_request.onupgradeneeded = function(event) {
var db = event.target.result;
db.createObjectStore("syncs", { keyPath: "type" });
};
})
}
function updateSyncTime(db, syncType, syncTime) {
return new Promise((resolve, reject) => {
var store = db.transaction(["syncs"], "readwrite").objectStore("syncs");
var get_request = store.get(syncType);
get_request.onerror = function(event) {
reject(new Error("Error getting value from database."));
}
get_request.onsuccess = function(event) {
var data = get_request.result || { type: syncType };
data.time = syncTime;
data.syncCount = (data.syncCount || 0) + 1;
var put_request = store.put(data);
put_request.onerror = function(event) {
reject(new Error("Error saving value to database."));
}
put_request.onsuccess = function(event) {
resolve(data.syncCount);
};
};
});
}
function postErrorToClients(err) {
clients.matchAll({includeUncontrolled: true})
.then(clientList => {
clientList.forEach(client => client.postMessage(err.message));
});
}