-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
90 lines (76 loc) · 1.95 KB
/
sw.js
File metadata and controls
90 lines (76 loc) · 1.95 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var cache = 'javascripture.22.0.1565129030';
self.addEventListener('install', function( e ) {
e.waitUntil( caches.open( cache ).then(function(cache) {
return cache.addAll([
'/',
'index.html',
'css/layout.css',
'manifest.json',
'javascripture.svg',
'android-icon-512x512.png',
//libs
'lib/MorphCodes.js',
'lib/MorphParse.js',
//data
'data/bible.js',
'data/extra-dictionary.js',
'data/strongs-dictionary.js',
'data/strongs-greek-dictionary.js',
'data/KJV.js',
'data/strongsObjectWithFamilies2.js',
'data/morphhb.js',
'data/tischendorf.js',
'data/crossReferences.js',
'data/literalConsistent.js',
'data/literalConsistentExtra.js',
//api - so that search works offline?
'api/searchApi.js',
//modules
'build/bundle.js',
//workers
'workers/worker.js',
]);
}));
});
function send_message_to_client(client, msg){
return new Promise(function(resolve, reject){
var msg_chan = new MessageChannel();
msg_chan.port1.onmessage = function(event){
if(event.data.error){
reject(event.data.error);
}else{
resolve(event.data);
}
};
client.postMessage( msg, [msg_chan.port2] );
});
}
function send_message_to_all_clients(msg){
clients.matchAll().then(clients => {
clients.forEach(client => {
send_message_to_client(client, msg).then(m => console.log("SW Received Message: "+m));
})
})
}
self.addEventListener('fetch', function(event) {
send_message_to_all_clients( cache );
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
// Delete unused cache
self.addEventListener('activate', function( event ) {
send_message_to_all_clients( cache );
var cacheWhitelist = [ cache ];
event.waitUntil(
caches.keys().then(function( keyList ) {
return Promise.all(keyList.map(function( key ) {
if (cacheWhitelist.indexOf( key ) === -1) {
return caches.delete( key );
}
}));
})
);
});