uppy/examples/bundled/sw.js
Antoine du Hamel 6b7ad5e7c7
Stricter linter (#3095)
* enforce some eslint rules

* enforce accessibility linter rules

* harden lint rules with only 1 or 2 warnings

* fix remaining rules with less than 3 warnings

* fix e2e tests

* fix remaining rules with less than 4 warnings

* fix remaining rules with less than 6 warnings

* fix `shuffleTaglines`

* fix companion build
2021-08-17 20:32:57 +02:00

66 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// This service worker is needed for Golden Retriever plugin,
// only include if youve enabled it
// https://uppy.io/docs/golden-retriever/
/* globals clients */
/* eslint-disable no-restricted-globals */
const fileCache = Object.create(null)
function getCache (name) {
if (!fileCache[name]) {
fileCache[name] = Object.create(null)
}
return fileCache[name]
}
self.addEventListener('install', (event) => {
console.log('Installing Uppy Service Worker...')
event.waitUntil(Promise.resolve()
.then(() => self.skipWaiting()))
})
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim())
})
function sendMessageToAllClients (msg) {
clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage(msg)
})
})
}
function addFile (store, file) {
getCache(store)[file.id] = file.data
console.log('Added file blob to service worker cache:', file.data)
}
function removeFile (store, fileID) {
delete getCache(store)[fileID]
console.log('Removed file blob from service worker cache:', fileID)
}
function getFiles (store) {
sendMessageToAllClients({
type: 'uppy/ALL_FILES',
store,
files: getCache(store),
})
}
self.addEventListener('message', (event) => {
switch (event.data.type) {
case 'uppy/ADD_FILE':
addFile(event.data.store, event.data.file)
break
case 'uppy/REMOVE_FILE':
removeFile(event.data.store, event.data.fileID)
break
case 'uppy/GET_FILES':
getFiles(event.data.store)
break
}
})