Created
October 31, 2018 08:46
-
-
Save enijar/c2ed734c4913de7a6eec54721b978d29 to your computer and use it in GitHub Desktop.
IndexedDB Cache Images
This file contains 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
class Cache { | |
constructor(props = {}) { | |
this.version = props.version || 1; | |
this.assets = {}; | |
this.db = null; | |
} | |
init() { | |
return new Promise(resolve => { | |
const request = indexedDB.open('tactics.cache', this.version); | |
request.onupgradeneeded = event => { | |
event.target.result.createObjectStore('cache'); | |
}; | |
request.onsuccess = () => { | |
this.db = request.result; | |
this.db.onerror = () => { | |
console.error('Error creating/accessing db'); | |
}; | |
if (this.db.setVersion && this.db.version !== this.version) { | |
const version = this.db.setVersion(this.version); | |
version.onsuccess = () => { | |
this.db.createObjectStore('cache'); | |
resolve(); | |
}; | |
} else { | |
resolve(); | |
} | |
}; | |
}); | |
} | |
putImage(key, url) { | |
return new Promise((resolve, reject) => { | |
if (!this.db) { | |
return reject('DB not initialized. Call the init method'); | |
} | |
const xhr = new XMLHttpRequest(); | |
xhr.open('GET', url); | |
xhr.responseType = 'blob'; | |
xhr.onload = response; | |
xhr.onerror = e => console.error(e); | |
xhr.send(); | |
const db = this.db; | |
function response(event) { | |
const blob = this.response; | |
const transaction = db.transaction(['cache'], 'readwrite'); | |
const put = transaction.objectStore('cache').put(blob, key); | |
resolve(); | |
} | |
}); | |
} | |
getImage(key) { | |
return new Promise(resolve => { | |
const transaction = this.db.transaction(['cache'], 'readwrite'); | |
transaction.objectStore('cache').get(key).onsuccess = event => { | |
resolve(URL.createObjectURL(event.target.result)); | |
}; | |
}); | |
} | |
} | |
(async () => { | |
try { | |
const cache = new Cache({version: 1}); | |
await cache.init(); | |
await cache.putImage('trophy', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADoAAABYCAYAAACkh+R5AAAST0lEQVR4nO2ceXRc1X3HPyOskUaa0YyW0WbJItZmY5BjS8EK2EYQcJN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K43XuIVGHc/93Uvk574OyD7w2ycGYAZbQ1ma5WY9Bl06el067x07jTYzi+5+HafOdmMulxPlzsEu3mo7h2XINxuOzGgi02H75jXS7v1nAWjtfhM5a99FruFatc0sTYE01e/PDLp0igvzKTOmEYmKuALROR3AmtMx1uZVFZJSpvCFRDkDmHB5jzz7AP/821Z+/NI7s32IGuVgx3iY9FqJDjlwUMhVluRIs02HzomoUtNUzIe8bGWN7PYG6Ojqm8uwU2Iqyc0lfJsTUaV28vL2r9C4+V8puMKHY9DG8IiXHb87Oa/HUeeKuRFNZPmUVErzvV/A2X6azj4bK2pKuef7ryl+6WXzM8dLOmpuD6apmXaz+U3yz/TQ3dUPqE7Cpc8wRZg10cqSHElJnuVKLvY+8SUANWHd0z9CZXFOygPnS8WsifbaApqrFuVKkaiIZcitZhqC4Qgef4hQ7Dyl+dmXHdE5lfavXloDyNIbsLk4++EInUM++hxhvnXv52moNqd0kqnAnPZoIBThR4/eR9rKryfe6ZILSYk9uqKmlHyD5bLao3OS6FgKJVHlHkfS0tNH95A3Kat3OWBORIOC7GDvfeIx2eFOkHynrYN2i5Xqsly1BnO5YE5L1+YJ8/SLr3NNTRktv2mnWD9Kx2CQoxY3S0qycfgjSsh12djSORF9+6RVU5KXJfX0j/D0nnbWrV6By+2mu9+OFNOTnXGFEnZdFiQvCU11peq/9DDptVJ9VYFk0msl5bPE/z35v4//N0Q/xsf4GCnBfwOqu57zeMROYQAAAABJRU5ErkJggg=='); | |
const url = await cache.getImage('trophy'); | |
console.log(`cached url -> ${url}`); | |
} catch (err) { | |
console.error(err); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment