MCPcopy Create free account
hub / github.com/violentmonkey/violentmonkey / cacheOrFetch

Function cacheOrFetch

src/background/utils/storage-fetch.js:23–45  ·  view source on GitHub ↗

@return {VMStorageFetch}

(handlers = {})

Source from the content-addressed store, hash-verified

21
22/** @return {VMStorageFetch} */
23function cacheOrFetch(handlers = {}) {
24 const requests = {};
25 const { init, transform } = handlers;
26 return function cacheOrFetchHandler(...args) {
27 const [url] = args;
28 const promise = requests[url] || (requests[url] = this::doFetch(...args));
29 return promise;
30 };
31 /** @this {VMStorageArea} */
32 async function doFetch(...args) {
33 const [url, options] = args;
34 try {
35 const res = await requestNewer(url, init ? init(options) : options);
36 if (res) {
37 const result = transform ? await transform(res, ...args) : res.data;
38 await this.setOne(url, result);
39 return result;
40 }
41 } finally {
42 delete requests[url];
43 }
44 }
45}
46
47/**
48 * @param {string} url

Callers 1

storage-fetch.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected