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

WIP: code cleanups on the way to more advanced cache handling (ref: #17)

parent 2ca23f56
Pipeline #1957 passed with stage
in 3 minutes and 12 seconds
......@@ -25,6 +25,10 @@ self.importScripts(
console.log('(COMMIT_UNKNOWN) SamizdatPlugins.length:', self.SamizdatPlugins.length)
/* ========================================================================= *\
|* === SamizdatInfo === *|
\* ========================================================================= */
// initialize the Samizdat info key-value store
if (typeof self.samizdatStore === 'undefined') {
self.samizdatStore = new idbKeyval.Store('samizdat', 'info')
......@@ -155,7 +159,43 @@ if (typeof SamizdatInfo !== "object") {
\* ========================================================================= */
/**
* Cycles through all the plugins, in the order they got registered
* get a plugin by name
*
* this doesn't have to be super-performant, since we should never have more
* then a few plugins
* (let's see how long it takes for me to eat my own words here)
*/
let getSamizdatPluginByName = (name) => {
for (i=0; i<SamizdatPlugins.length; i++) {
if (SamizdatPlugins[i].name === name) {
return SamizdatPlugins[i]
}
}
return null
}
/**
* run a plugin's fetch() method
* while handling all the auxiliary stuff like saving info in SamizdatInfo
*
* plugin - the plugin to use
* url - string containing the URL to fetch
* lastError - error thrown by the previous plugin, if any (default: null)
*/
let samizdatFetch = (plugin, url, lastError=null) => {
// save info in SamizdatInfo
SamizdatInfo.resources[url].fetchError = lastError;
SamizdatInfo.resources[url].method = plugin.name
// log stuff
console.log("(COMMIT_UNKNOWN) Samizdat handling URL:", url);
console.log('+-- last error : ' + lastError)
console.log('+-- current method : ' + plugin.name)
// run the plugin
return plugin.fetch(url)
}
/**
* Cycles through all the plugins, in the order they got registered,
* and returns a Promise resolving to a Response in case any of the plugins
* was able to get the resource
*
......@@ -171,10 +211,6 @@ let getResourceThroughSamizdat = (url, useStashed=true, doStash=true) => {
console.log('+-- created SamizdatInfo.resources[' + url + ']')
}
// clear the error fetchError info
SamizdatInfo.resources[url].fetchError = null;
SamizdatInfo.resources[url].method = SamizdatPlugins[0].name // whatever the first plugin is called
/**
* this uses Array.reduce() to chain the SamizdatPlugins[]-generated Promises
* using the Promise the first registered plugin as the default value
......@@ -188,20 +224,13 @@ let getResourceThroughSamizdat = (url, useStashed=true, doStash=true) => {
(prevPromise, currentPlugin)=>{
return prevPromise.catch((error)=>{
// Fall back to Samizdat:
console.log("(COMMIT_UNKNOWN) Samizdat fallback handling after: ", error);
console.log('+-- URL : ' + url)
console.log('+-- method : ' + currentPlugin.name)
// record the error in SamizdatInfo (as a string)
SamizdatInfo.resources[url].fetchError = error.toString()
// this is very naïve and should in fact be handled inside the relevant plugin, probably
SamizdatInfo.resources[url].method = currentPlugin.name;
// use the plugin
return currentPlugin.fetch(url)
return samizdatFetch(currentPlugin, url, error.toString())
})
},
// this SamizdatPlugin[].fetch() will run first
// all other promises generated by SamizdatPlugins[] will be chained using catch() on it
SamizdatPlugins[0].fetch(url)
// this samizdatFetch() will run first
// all other promises generated by SamizdatPlugins[] will be chained on it
// using the catch() in reduce() above
samizdatFetch(SamizdatPlugins[0], url)
)
.then((response)=>{
// do we want to stash at all?
......
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