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

WIP: cleanup in preparation for advanced cache handling (ref: #17)

parent 7c4d6523
Pipeline #1959 canceled with stage
in 4 minutes and 11 seconds
......@@ -238,19 +238,29 @@ let getResourceThroughSamizdat = (url, useStashed=true, doStash=true) => {
samizdatFetch(SamizdatPlugins[0], url, null, useStashed)
)
.then((response)=>{
// do we want to stash at all?
if (doStash) {
// find the first stashing plugin
for (i=0; i<SamizdatPlugins.length; i++) {
if (typeof SamizdatPlugins[i].stash === 'function') {
// yes, this returns a promise...
return SamizdatInfo.resources[url].method.then((method)=>{
// if we got this from the same stashing plugin in the first place, this is moot
if (method === SamizdatPlugins[i].name) {
console.log('(COMMIT_UNKNOWN) Not stashing, since resource is retrieved by a stashing plugin:', url);
return response
}
// yes, this returns a promise...
return SamizdatInfo.resources[url].method.then((method)=>{
// get the plugin that was used to fetch content
plugin = getSamizdatPluginByName(method)
// if it's a stashing plugin...
if (typeof plugin.stash === 'function') {
// we obviously do not want to stash
console.log('(COMMIT_UNKNOWN) Not stashing, since resource is already retrieved by a stashing plugin:', url);
// since we got the data from a stashing plugin,
// let's run the rest of plugins in the background to check if we can get a fresher resource
// and stash it in cache for later use
//getResourceThroughSamizdat(url, false, true)
// return the response so that stuff can keep happening
return response
// otherwise, let's see if we want to stash
} else if (doStash) {
// find the first stashing plugin
for (i=0; i<SamizdatPlugins.length; i++) {
if (typeof SamizdatPlugins[i].stash === 'function') {
// ok, now we're in business
console.log('(COMMIT_UNKNOWN) Stashing a successful fetch of:', url);
......@@ -264,15 +274,15 @@ let getResourceThroughSamizdat = (url, useStashed=true, doStash=true) => {
// 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
// or we don't want to stash the resources in the first place
// that's fine, but let's make sure the response goes forth
return response
// if we're here it means we went through the whole list of plugins
// and found not a single stashing plugin
// or we don't want to stash the resources in the first place
// that's fine, but let's make sure the response goes forth
return response
})
})
// a final catch... in case all plugins fail
.catch((err)=>{
......@@ -326,5 +336,5 @@ self.addEventListener('fetch', event => {
// 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(getResourceThroughSamizdat(cleanURL, false))
return void event.respondWith(getResourceThroughSamizdat(cleanURL))
});
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