Skip to content

Instantly share code, notes, and snippets.

@okjodom
Created May 12, 2018 06:52
Show Gist options
  • Select an option

  • Save okjodom/a8f45b9cc6c3a182e8eebb0a27e63514 to your computer and use it in GitHub Desktop.

Select an option

Save okjodom/a8f45b9cc6c3a182e8eebb0a27e63514 to your computer and use it in GitHub Desktop.
My solution to clean image cache, promises :)
...
IndexController.prototype._cleanImageCache = function() {
return this._dbPromise.then(function(db) {
if (!db) return;
// TODO: open the 'wittr' object store, get all the messages,
// gather all the photo urls.
var store = db.transaction('wittrs').objectStore('wittrs');
return store.getAll().then((messages) => {
if (!messages) return;
// get url from each message, retun urls
var urls = messages.map(message => {
if (!message.photo) return;
return "http://localhost:8888" + message.photo;
}).filter(url => {
return url;
});
return urls;
}).then( photoUrls => {
if (!photoUrls) return;
// Open the 'wittr-content-imgs' cache, and delete any entry
// that you no longer need.
return caches.open('wittr-content-imgs').then(cache => {
return cache.keys().then(requests => {
return requests.forEach(request => {
if (!photoUrls.includes(request.url)){
return cache.delete(request);
}
return;
});
});
});
});
});
};
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment