Created
May 12, 2018 06:52
-
-
Save okjodom/a8f45b9cc6c3a182e8eebb0a27e63514 to your computer and use it in GitHub Desktop.
My solution to clean image cache, promises :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| 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