Skip to content

Commit

Permalink
refactor: Reduce the number of fetch requests (#49)
Browse files Browse the repository at this point in the history
* refactor: Reduce the number of fetch requests

* chore: bump version

* refactor: export caches

---------

Co-authored-by: huang yao <[email protected]>
  • Loading branch information
kirakiray and kirakiray authored Nov 6, 2023
1 parent a24d0a9 commit 71ce814
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 23 deletions.
24 changes: 19 additions & 5 deletions dist/drill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! drill.js - v5.3.2 https://github.com/kirakiray/drill.js (c) 2018-2023 YAO
//! drill.js - v5.3.3 https://github.com/kirakiray/drill.js (c) 2018-2023 YAO
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
Expand Down Expand Up @@ -38,6 +38,20 @@
}
}

const caches = new Map();
const wrapFetch = async (url) => {
let fetchObj = caches.get(url);

if (!fetchObj) {
fetchObj = fetch(url);
caches.set(url, fetchObj);
}

const resp = await fetchObj;

return resp.clone();
};

const processor = {};

const addHandler = (name, handler) => {
Expand Down Expand Up @@ -96,7 +110,7 @@

let resp;
try {
resp = await fetch(url);
resp = await wrapFetch(url);
} catch (error) {
throw wrapError(`Load ${url} failed`, error);
}
Expand All @@ -115,7 +129,7 @@
if (!ctx.result) {
const { url } = ctx;

ctx.result = await fetch(url).then((e) => e.json());
ctx.result = await wrapFetch(url).then((e) => e.json());
}

await next();
Expand All @@ -125,7 +139,7 @@
if (!ctx.result) {
const { url } = ctx;

const data = await fetch(url).then((e) => e.arrayBuffer());
const data = await wrapFetch(url).then((e) => e.arrayBuffer());

const module = await WebAssembly.compile(data);
const instance = new WebAssembly.Instance(module);
Expand Down Expand Up @@ -162,7 +176,7 @@
})
);
} else {
ctx.result = await fetch(url).then((e) => e.text());
ctx.result = await wrapFetch(url).then((e) => e.text());
}
}

Expand Down
4 changes: 2 additions & 2 deletions dist/drill.min.js

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

Loading

0 comments on commit 71ce814

Please sign in to comment.