-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
171 lines (155 loc) · 4.76 KB
/
sw.js
File metadata and controls
171 lines (155 loc) · 4.76 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const CACHE_NAME = 'acidbros-v152';
const ASSETS = [
'./',
'./index.html',
'./css/base.css',
'./css/icons.css',
'./css/layout.css',
'./css/components.css',
'./css/machines.css',
'./css/overlays.css',
'./js/main.js',
'./js/ui/RotaryKnob.js',
'./js/audio/AudioEngine.js',
'./js/audio/ClockProcessor.js',
'./js/audio/TB303.js',
'./js/audio/TR909.js',
'./js/data/Data.js',
'./js/data/FileManager.js',
'./js/data/BinaryFormatEncoder.js',
'./js/data/BinaryFormatDecoder.js',
'./js/ui/UI.js',
'./js/ui/DrumSynthUI.js',
'./js/audio/tr909/DrumVoice.js',
'./js/audio/tr909/UnifiedSynth.js',
'./js/ui/Oscilloscope.js',
'./js/midi/MidiManager.js',
'./manifest.json',
'./assets/favicon.png',
'./assets/DSEG7Classic-Bold.woff2',
'./assets/samples/tr909/hh01.wav',
'./assets/samples/tr909/oh01.wav',
'./assets/samples/tr909/cr01.wav',
'./assets/samples/tr909/rd01.wav',
'./assets/icons/add.svg',
'./assets/icons/settings.svg',
'./assets/icons/bd.svg',
'./assets/icons/sd.svg',
'./assets/icons/lt.svg',
'./assets/icons/mt.svg',
'./assets/icons/ht.svg',
'./assets/icons/rs.svg',
'./assets/icons/cp.svg',
'./assets/icons/ch.svg',
'./assets/icons/oh.svg',
'./assets/icons/cr.svg',
'./assets/icons/rd.svg',
'./assets/icons/folder.svg',
'./assets/icons/play.svg',
'./assets/icons/stop.svg',
'./assets/icons/dice.svg',
'./assets/icons/trash.svg',
'./assets/icons/share.svg',
'./assets/icons/cog.svg',
'./assets/icons/coffee.svg',
'./assets/icons/shuffle.svg',
'./assets/icons/copy.svg',
'./assets/icons/paste.svg',
'./assets/icons/file-new.svg',
'./assets/icons/file-save.svg',
'./assets/icons/file-import.svg',
'./assets/icons/file-export.svg',
'./assets/icons/file-delete-all.svg',
'./assets/icons/close.svg',
'./assets/icons/refresh.svg',
'./assets/icons/unlocked.svg',
'./assets/icons/locked.svg',
'./assets/icons/saw.svg',
'./assets/icons/sq.svg',
'./assets/icons/pattern.svg',
'./assets/icons/song.svg',
'./assets/icons/edit.svg'
];
self.addEventListener('install', (e) => {
self.skipWaiting();
e.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(ASSETS);
})
);
});
const isSameOriginGetRequest = (request) => {
if (!request || request.method !== 'GET') return false;
const url = new URL(request.url);
return url.origin === self.location.origin;
};
const cachePutIfOk = async (request, response) => {
if (!response || !response.ok) return;
const cache = await caches.open(CACHE_NAME);
await cache.put(request, response.clone());
};
const networkFirst = async (request) => {
try {
const fresh = await fetch(request);
await cachePutIfOk(request, fresh);
return fresh;
} catch (err) {
const cache = await caches.open(CACHE_NAME);
const cached = await cache.match(request);
if (cached) return cached;
if (request.mode === 'navigate') {
const fallback = await cache.match('./index.html');
if (fallback) return fallback;
}
throw err;
}
};
const staleWhileRevalidate = async (request, event) => {
const cache = await caches.open(CACHE_NAME);
const cached = await cache.match(request);
const updatePromise = fetch(request)
.then(async (fresh) => {
await cachePutIfOk(request, fresh);
return fresh;
})
.catch(() => null);
if (cached) {
event.waitUntil(updatePromise);
return cached;
}
const fresh = await updatePromise;
if (fresh) return fresh;
if (request.mode === 'navigate') {
const fallback = await cache.match('./index.html');
if (fallback) return fallback;
}
return Response.error();
};
self.addEventListener('fetch', (e) => {
if (!isSameOriginGetRequest(e.request)) return;
const destination = e.request.destination;
const shouldUseNetworkFirst =
e.request.mode === 'navigate' ||
destination === 'style' ||
destination === 'script' ||
destination === 'worker' ||
destination === 'audio';
if (shouldUseNetworkFirst) {
e.respondWith(networkFirst(e.request));
return;
}
e.respondWith(staleWhileRevalidate(e.request, e));
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
})
);
}).then(() => self.clients.claim())
);
});