Commit c3422d2f authored by Michał "rysiek" Woźniak's avatar Michał "rysiek" Woźniak
Browse files

Updated service-worker.js and index.html to reflect fetch() being a plugin now (ref: #21)

parent bff5a6a8
Pipeline #1949 passed with stage
in 24 seconds
......@@ -15,6 +15,7 @@
<script defer src="./lib/gun.js"></script>
<script defer src="./lib/sea.js"></script>
<script defer src="./lib/webrtc.js"></script>
<script defer src="./plugins/fetch.js"></script>
<script defer src="./plugins/cache.js"></script>
<script defer src="./plugins/gun-ipfs.js"></script>
<script>
......@@ -66,7 +67,7 @@
if (typeof fetchedResourcesDisplay !== 'object') {
fetchedResourcesDisplay = document.getElementById("fetched-resources-list")
}
var itemHTML = `<li class="fetched-resources-item"><label><input type="checkbox" checked="checked"/><span class="fetched-resource-url"><span>${si.url}</span></span><span class="fetched-resource-method fetch ${(si.method === 'fetch') ? 'active' : ''}">fetch</span>`
var itemHTML = ''
SamizdatPlugins.forEach((plugin)=>{
var pclass = samizdat.safeClassName(plugin.name);
itemHTML += `<span class="fetched-resource-method ${pclass} ${(si.method === plugin.name) ? 'active' : ''}">${plugin.name}</span>`
......@@ -193,7 +194,7 @@
.forEach((el)=>{
resources.push(el.parentElement.querySelector('.fetched-resource-url').innerText)
})
return SamizdatPlugins[0].push(resources)
return SamizdatPlugins[1].push(resources)
})
}
......@@ -230,10 +231,6 @@
// add plugin status display
samizdat.addPluginStatus({
name: 'fetch',
description: 'Regular HTTP(S) fetch()'
})
SamizdatPlugins.forEach(samizdat.addPluginStatus)
// TODO: do it better, watch for ongoing requests or some such?
......@@ -260,12 +257,7 @@
)
// once we have all the data...
.then((val)=>{
// add plugin stats for the regular HTTP(S) fetch()
samizdat.updatePluginStatus({
name: 'fetch',
description: 'Regular HTTP(S) fetch()'
})
// and all other plugins
// add plugins' stats
SamizdatPlugins.forEach(samizdat.updatePluginStatus)
})
}, 5000)
......
......@@ -18,6 +18,7 @@ if (!Array.isArray(self.SamizdatPlugins)) {
// order in which plugins are loaded defines the order
// in which they are called!
self.importScripts(
"./plugins/fetch.js",
"./plugins/cache.js",
"./plugins/gun-ipfs.js",
"./lib/idb-keyval-iife.min.js");
......@@ -186,18 +187,19 @@ self.addEventListener('fetch', event => {
// clear the error fetchError info
SamizdatInfo.resources[cleanURL].fetchError = null;
SamizdatInfo.resources[cleanURL].method = 'fetch'; // assume the standard fetch will work
SamizdatInfo.resources[cleanURL].method = SamizdatPlugins[0].name // whatever the first plugin is called
// GET requests to our own domain that are *not* #samizdat-info requests
// get handled by plugins in case of an error
return void event.respondWith(
/**
* this uses Array.reduce() to chain the SamizdatPlugins[]-generated Promises
* using the Promise from a basic HTTP(S) fetch() as the default value
* using the Promise the first registered plugin as the default value
*
* see: https://css-tricks.com/why-using-reduce-to-sequentially-resolve-promises-works/
*
* this also means that fetch() below will run first (counter-intutively!)
* this also means that SamizdatPlugins[0].fetch() below will run first
* (counter-intutively!)
*/
SamizdatPlugins.reduce((prevPromise, nextPlugin)=>{
return prevPromise.catch((error)=>{
......@@ -221,7 +223,7 @@ self.addEventListener('fetch', event => {
// TODO: this assumes the cache plugin is the first one!
// TODO: we should not be caching a response fetched from cache!
console.log("(COMMIT_UNKNOWN) Caching successful fetch: ", cleanURL);
return SamizdatPlugins[0].push(response.clone(), cleanURL).then((res)=>{
return SamizdatPlugins[1].push(response.clone(), cleanURL).then((res)=>{
return response
})
})*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment