diff options
Diffstat (limited to 'appcache.js')
-rw-r--r-- | appcache.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/appcache.js b/appcache.js new file mode 100644 index 0000000..87d5e80 --- /dev/null +++ b/appcache.js | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | * appcache.js | ||
3 | * Part of Pointless Viewer, a Beamer presentation viewer | ||
4 | * Copyright 2018 Pacien TRAN-GIRARD | ||
5 | * License: GNU GPL v3 | ||
6 | */ | ||
7 | |||
8 | "use strict"; | ||
9 | |||
10 | class AppCache { | ||
11 | constructor() { | ||
12 | this.cacheName = "cache"; | ||
13 | this.filesToCache = [ | ||
14 | "viewer.html", | ||
15 | "appcache.js", | ||
16 | |||
17 | "pointless/pdfjs/pdf.js", | ||
18 | "pointless/pdfjs/pdf.worker.js", | ||
19 | |||
20 | "pointless/viewer/viewer.css", | ||
21 | "pointless/viewer/init.js", | ||
22 | "pointless/viewer/viewer.js", | ||
23 | "pointless/viewer/presentation.js", | ||
24 | "pointless/viewer/stage.js", | ||
25 | "pointless/viewer/screen.js", | ||
26 | "pointless/viewer/timer.js" | ||
27 | ]; | ||
28 | |||
29 | var appCache = this; | ||
30 | self.addEventListener("install", function(event) { | ||
31 | event.waitUntil(appCache._onInstall()); | ||
32 | }); | ||
33 | |||
34 | self.addEventListener("fetch", function(event) { | ||
35 | event.respondWith(appCache._onFetch(event.request)); | ||
36 | }); | ||
37 | } | ||
38 | |||
39 | _onInstall() { | ||
40 | var self = this; | ||
41 | return caches.open(this.cacheName).then(function(cache) { | ||
42 | return cache.addAll(self.filesToCache); | ||
43 | }); | ||
44 | } | ||
45 | |||
46 | _onFetch(request) { | ||
47 | return navigator.onLine ? this._fetchUpdate(request) : caches.match(request); | ||
48 | } | ||
49 | |||
50 | _fetchUpdate(request) { | ||
51 | return caches.open(this.cacheName).then(function(cache) { | ||
52 | return fetch(request).then(function(response) { | ||
53 | cache.put(request, response.clone()); | ||
54 | return response | ||
55 | }); | ||
56 | }); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | var appCache = new AppCache(); | ||