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

cache.unstash() implemented (fixes #22); index.html knows how to use it now, too

parent 8bfde357
Pipeline #1952 passed with stage
in 3 minutes and 39 seconds
......@@ -184,33 +184,34 @@
/**
* adding certain resources from cache
* stashing and unstashing resources
*
* stash param means "stash" if set to true (the default), "unstash" otherwise
*/
samizdat.addResourcesToCache = () => {
caches.open('v1')
.then((cache) => {
var resources = []
document.querySelectorAll('.fetched-resources-item input:checked')
.forEach((el)=>{
resources.push(el.parentElement.querySelector('.fetched-resource-url').innerText)
})
return SamizdatPlugins[1].stash(resources)
})
}
/**
* removing certain resources from cache
*/
samizdat.clearResourcesFromCache = () => {
caches.open('v1')
.then((cache) => {
document.querySelectorAll('.fetched-resources-item input:checked')
.forEach((el)=>{
cache.delete(el.parentElement.querySelector('.fetched-resource-url').innerText)
})
})
}
samizdat.stashOrUnstashResources = (stash=true) => {
// what are we doing?
if (stash) {
var operation = 'stash'
} else {
var operation = 'unstash'
}
// get the resources
var resources = []
document
.querySelectorAll('.fetched-resources-item input:checked')
.forEach((el)=>{
resources.push(el.parentElement.querySelector('.fetched-resource-url').innerText)
})
// 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...')
return SamizdatPlugins[i][operation](resources)
}
}
// if we're here that means there was no plugin able to stash things
return Promise.reject(new Error('No stashing plugin found'))
/**
* publishing certain resources to Gun+IPFS
......@@ -292,7 +293,7 @@
<p>The list below contains all resources fetched in relation to this page.</p>
<ul id="fetched-resources-list"></ul>
<p id="fetched-resources-list-empty">The list is empty, but if the Service Worker is running it should be populated soon.</p>
<p id="fetched-resources-controls"><button type="button" onclick="samizdat.toggleResourceCheckboxes()">Toggle selection</button><span class="spacer"></span><button type="button" onclick="samizdat.addResourcesToCache()">Add selected to cache</button><button type="button" onclick="samizdat.clearResourcesFromCache()">Clear selected from cache</button><span class="spacer"></span><input type="text" placeholder="Gun username" id="samizdat-gun-user"/><input type="password" placeholder="Gun password" id="samizdat-gun-password"/><button type="button" onclick="samizdat.publishResourcesToGunAndIPFS()">Publish to Gun+IPFS</button></p>
<p id="fetched-resources-controls"><button type="button" onclick="samizdat.toggleResourceCheckboxes()">Toggle selection</button><span class="spacer"></span><button type="button" onclick="samizdat.stashOrUnstashResources(true)">Add selected to cache</button><button type="button" onclick="samizdat.stashOrUnstashResources(false)">Clear selected from cache</button><span class="spacer"></span><input type="text" placeholder="Gun username" id="samizdat-gun-user"/><input type="password" placeholder="Gun password" id="samizdat-gun-password"/><button type="button" onclick="samizdat.publishResourcesToGunAndIPFS()">Publish to Gun+IPFS</button></p>
</div>
<p id="footer">ServiceWorker: <span id="samizdat-commit-service-worker">NO_INFO</span>&nbsp;::&nbsp;index.html: <span id="samizdat-commit-index-html">COMMIT_UNKNOWN</span><br/>code: <span id="samizdat-code"><a href="https://git.occrp.org/libre/samizdat/">here</a></span>&nbsp;::&nbsp;license: <span id="samizdat-license"><a href="https://git.occrp.org/libre/samizdat/blob/master/LICENSE">AGPL</a></span></p>
</body>
......
......@@ -17,9 +17,13 @@ let getContentFromCache = (url) => {
}
/**
* add resources to cache
*
* implements the stash() Samizdat plugin method
*
* accepts either a Response
* or a string containing a URL
* or an array of string URLs
* or an Array of string URLs
*/
let cacheContent = (resource, url) => {
return caches.open('v1')
......@@ -46,6 +50,40 @@ let cacheContent = (resource, url) => {
}
})
}
/**
* remove resources from cache
*
* implements the unstash() Samizdat plugin method
*
* accepts either a Response
* or a string containing a URL
* or an Array of string URLs
*/
let clearCachedContent = (resource) => {
return caches.open('v1')
.then((cache) => {
if (typeof resource === 'string') {
// assume URL
console.log("(COMMIT_UNKNOWN) deleting a cached URL")
return cache.delete(resource)
} else if (Array.isArray(resource)) {
// assume array of URLs
console.log("(COMMIT_UNKNOWN) deleting an Array of cached URLs")
return Promise.all(
resource.map((res)=>{
return cache.delete(res)
})
)
} else {
// assume a Response
// which means we have an URL in resource.url
console.log("(COMMIT_UNKNOWN) removing a Response from cache: " + resource.url)
return cache.delete(resource.url)
}
})
}
// initialize the SamizdatPlugins array
if (!Array.isArray(self.SamizdatPlugins)) {
......@@ -59,5 +97,6 @@ self.SamizdatPlugins.push({
description: 'Locally cached responses, using the Cache API.',
version: 'COMMIT_UNKNOWN',
fetch: getContentFromCache,
stash: cacheContent
stash: cacheContent,
unstash: clearCachedContent
})
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