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

Auto-stashing fetched resources re-enabled (and a bit smarter)

parent 4962f983
......@@ -205,7 +205,7 @@
// cycle through plugins and find the first that implements a stash() method
for (i=0; i<SamizdatPlugins.length; i++) {
if (typeof SamizdatPlugins[i][operation] === 'function') {
console.log('(COMMIT_UNKNOWN) Using plugin ' + SamizdatPlugins[i].name + ' to ' + operation + ' the resources...')
console.log('(COMMIT_UNKNOWN) Using plugin "' + SamizdatPlugins[i].name + '" to ' + operation + ' the resources...')
return SamizdatPlugins[i][operation](resources)
}
}
......
......@@ -201,46 +201,69 @@ self.addEventListener('fetch', event => {
* this also means that SamizdatPlugins[0].fetch() below will run first
* (counter-intutively!)
*/
SamizdatPlugins.reduce((prevPromise, nextPlugin)=>{
return prevPromise.catch((error)=>{
// Fall back to Samizdat:
console.log("(COMMIT_UNKNOWN) Samizdat fallback handling after: ", error);
console.log('+-- URL : ' + cleanURL)
console.log('+-- method : ' + nextPlugin.name)
// record the error in SamizdatInfo (as a string)
SamizdatInfo.resources[cleanURL].fetchError = error.toString()
// this is very naïve and should in fact be handled inside the relevant plugin, probably
SamizdatInfo.resources[cleanURL].method = nextPlugin.name;
// use the plugin
return nextPlugin.fetch(cleanURL)
})
},
// this SamizdatPlugin[].fetch() will run first
// all other promises generated by SamizdatPlugins[] will be chained using catch() on it
SamizdatPlugins[0].fetch(cleanURL)
)
/*.then((response)=>{
// 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[1].stash(response.clone(), cleanURL).then((res)=>{
SamizdatPlugins.reduce(
(prevPromise, currentPlugin)=>{
return prevPromise.catch((error)=>{
// Fall back to Samizdat:
console.log("(COMMIT_UNKNOWN) Samizdat fallback handling after: ", error);
console.log('+-- URL : ' + cleanURL)
console.log('+-- method : ' + currentPlugin.name)
// record the error in SamizdatInfo (as a string)
SamizdatInfo.resources[cleanURL].fetchError = error.toString()
// this is very naïve and should in fact be handled inside the relevant plugin, probably
SamizdatInfo.resources[cleanURL].method = currentPlugin.name;
// use the plugin
return currentPlugin.fetch(cleanURL)
})
},
// this SamizdatPlugin[].fetch() will run first
// all other promises generated by SamizdatPlugins[] will be chained using catch() on it
SamizdatPlugins[0].fetch(cleanURL)
)
.then((response)=>{
// find the first stashing plugin
for (i=0; i<SamizdatPlugins.length; i++) {
if (typeof SamizdatPlugins[i].stash === 'function') {
// if we got this from the same stashing plugin in the first place, this is moot
if (SamizdatInfo.resources[cleanURL].method === SamizdatPlugins[i].name) {
console.log('(COMMIT_UNKNOWN) Not stashing, since resource is retrieved by a stashing plugin:', cleanURL);
return response
}
// ok, now we're in business
console.log('(COMMIT_UNKNOWN) Stashing a successful fetch of:', cleanURL);
console.log('+-- fetched using :', SamizdatInfo.resources[cleanURL].method)
console.log('+-- stashing using :', SamizdatPlugins[i].name)
// working on clone()'ed response so that the original one is not touched
// TODO: should a failed stashing break the flow here? probably not!
return SamizdatPlugins[i].stash(response.clone(), cleanURL)
.then((res)=>{
// original response will be needed further down
return response
})
}
}
// if we're here it means we went through the whole list of plugins
// and found not a single stashing plugin
// that's fine, but let's make sure the response goes forth
return response
})
})*/
// a final catch... in case all plugins fail
.catch((err)=>{
console.log("(COMMIT_UNKNOWN) Samizdat also failed completely: ", err);
console.log('+-- URL : ' + cleanURL)
// cleanup
SamizdatInfo.resources[cleanURL].fetchError = err.toString()
// this is very naïve and should in fact be handled inside the relevant plugin, probably
SamizdatInfo.resources[cleanURL].method = null
// rethrow
throw err
})
// this is where we can attach a .then() that will run regardless of the result
// which could then:
// - cache the results or perform some other housekeeping in case of success
// - handle the error in case of failure
// a final catch... in case all plugins fail
.catch((err)=>{
console.log("(COMMIT_UNKNOWN) Samizdat also failed completely: ", err);
console.log('+-- URL : ' + cleanURL)
// cleanup
SamizdatInfo.resources[cleanURL].fetchError = err.toString()
// this is very naïve and should in fact be handled inside the relevant plugin, probably
SamizdatInfo.resources[cleanURL].method = null
// rethrow
throw err
})
// this is where we can attach a .then() that will run regardless of the result
// which could then:
// - cache the results or perform some other housekeeping in case of success
// - handle the error in case of failure
);
});
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