Skip to content

Commit

Permalink
Bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Sep 8, 2021
1 parent b45a05e commit 0a8ee13
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ To cache and sideload static assets:
Always include and invoke indexed-cache at the end, right before </body>.
Use the unpkg CDN or download and host the script locally (dist/indexed-cache.min.js).
!-->
<script src="https://unpkg.com/@knadh/indexed-cache@0.3.2/dist/indexed-cache.min.js"></script>
<script src="https://unpkg.com/@knadh/indexed-cache@0.3.3/dist/indexed-cache.min.js"></script>
<script>const ic = new IndexedCache(); await ic.init(); ic.load();</script>
</body>
</html>
Expand Down
58 changes: 33 additions & 25 deletions dist/indexed-cache.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class IndexedCache {
// referenced on the page.
if (this.opt.prune) {
// Pass the list of keys found on the page.
const keys = objs.reduce((obj, v) => { obj.push(v.key); return obj }, []);
const keys = objs.map(obj => obj.key);
this._prune(keys);
}
}
Expand Down Expand Up @@ -215,29 +215,33 @@ class IndexedCache {
// (hash changed or date expired), fetch the asset over HTTP, cache it, and load it.
async _getBlob (obj) {
return new Promise((resolve, reject) => {
const req = this._store().get(obj.key);
req.onsuccess = (e) => {
const data = e.target.result;

// Reject if there is no stored data, or if the hash has changed.
if (!data || (obj.hash && (data.hash !== obj.hash))) {
reject(new Error(''));
return
}

// Reject and delete if the object has expired.
if (data.expiry && new Date() > new Date(data.expiry)) {
this.deleteKey(data.key);
reject(new Error(''));
return
}

resolve(data);
};
try {
const req = this._store().get(obj.key);
req.onsuccess = (e) => {
const data = e.target.result;

// Reject if there is no stored data, or if the hash has changed.
if (!data || (obj.hash && (data.hash !== obj.hash))) {
reject(new Error(''));
return
}

// Reject and delete if the object has expired.
if (data.expiry && new Date() > new Date(data.expiry)) {
this.deleteKey(data.key);
reject(new Error(''));
return
}

resolve(data);
};

req.onerror = (e) => {
req.onerror = (e) => {
reject(e.target.error);
};
} catch (e) {
reject(e.target.error);
};
}
})
}

Expand All @@ -258,9 +262,13 @@ class IndexedCache {
blob: b
};

const req = this._store().put(data);
req.onsuccess = (e) => resolve(data);
req.onerror = (e) => reject(e.target.error);
try {
const req = this._store().put(data);
req.onsuccess = (e) => resolve(data);
req.onerror = (e) => reject(e.target.error);
} catch (e) {
reject(e.target.error);
}
});
}).catch((e) => {
reject(new Error(e.toString()));
Expand Down
4 changes: 2 additions & 2 deletions dist/indexed-cache.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knadh/indexed-cache",
"version": "0.3.2",
"version": "0.3.3",
"description": "Sideload static assets on pages and cache them in IndexedDB ensuring longer-term storage.",
"main": "src/index.js",
"files": [
Expand Down

0 comments on commit 0a8ee13

Please sign in to comment.