UNPKG

@gorgonjs/gorgon

Version:

A simple caching library for async functions

159 lines (158 loc) 5.71 kB
const g = () => { const o = {}, u = o.hasOwnProperty, i = { init: async () => { }, get: async (s) => { if (u.call(o, s) && o[s].val) return o[s].val; }, set: async (s, t, r) => { await i.clear(s); var e = !1; return r && r.expiry && r.expiry > 0 && (e = setTimeout(function() { i.clear(s); }, r.expiry)), o[s] = { policy: r, val: t, to: e }, t; }, keys: async () => Object.keys(o), clear: async (s) => { if (!s) { for (var t in o) i._clear(t); return !0; } return i._clear(s); }, _clear: (s) => u.call(o, s) ? (o[s].to && clearTimeout(o[s].to), o[s] = null, delete o[s], !0) : !1 }; return i; }, m = (() => { const o = {}, u = o.hasOwnProperty, i = { debug: !1, defaultProvider: "memory", retry: 5e3 }, s = function(r) { const e = { expiry: !1, provider: i.defaultProvider }; if (!r) return e; if (r instanceof Date) { var a = /* @__PURE__ */ new Date(); e.expiry = Math.ceil((r.getTime() - a.getTime()) / 1e3); } else typeof r == "object" && r.expiry ? (r.expiry instanceof Date ? e.expiry = Math.ceil((r.expiry.getTime() - a.getTime()) / 1e3) : e.expiry = r.expiry, e.provider = r.provider || e.provider) : typeof r == "object" ? e.provider = r.provider || e.provider : typeof r == "number" && (e.expiry = r); return e.expiry = e.expiry && e.expiry > 0 ? e.expiry : !1, e; }, t = { // Providers available for use providers: {}, // Hooks hooks: {}, _callHooks: (r, e, a) => { if (u.call(t.hooks, r)) { for (var n in t.hooks[r]) if (typeof t.hooks[r][n] == "function") try { t.hooks[r][n](r, e, a); } catch (l) { console.error("[Gorgon] Hook error for hook: " + r, l); } } }, // Allows for settings on the gorgon cache settings: (r) => (r && (Object.assign(i, r), t._callHooks("settings", r, i)), i), // add a hook or array of hooks addHook: (r, e) => { u.call(t.hooks, r) || (t.hooks[r] = []), Array.isArray(e) ? t.hooks[r] = t.hooks[r].concat(e) : t.hooks[r].push(e); }, // Add a provider addProvider: (r, e) => { e.init(), t.providers[r] = e, t._callHooks("addProvider", { name: r, provider: e }); }, // Place an item into the cache put: async (r, e, a) => { a = s(a); var n = t.providers[a.provider]; return t._callHooks("put", { key: r, value: e, policy: a }, e), n.set(r, e, s(a)); }, // Clear one or all items in the cache clear: async (r, e, a) => { var n = t.providers[e || i.defaultProvider]; return t._callHooks("clear", { key: r, provider: e, identifier: a }), r && r.indexOf("*") > -1 ? n.keys().then(function(l) { var v = l.filter(function(h) { return new RegExp("^" + r.split("*").join(".*") + "$").test(h); }), f = v.map(n.clear); return f.push(n.clear(r)), Promise.all(f); }) : n.clear(r); }, // Clear all keys/values in the cache clearAll: async (r, e) => { var a = t.providers[r || i.defaultProvider]; return t._callHooks("clearAll", { provider: r, identifier: e }), a.clear(); }, // Allows you to instantly overwite a cache object overwrite: async (r, e, a) => { try { const n = await e(), l = await t.put(r, n, s(a)); return t._callHooks("overwrite", { key: r, asyncFunc: e, policy: a }, l), l; } catch (n) { throw n; } }, // Allows you to get from the cache or pull from the promise get: async (r, e, a) => { a = s(a); const l = await t.providers[a.provider].get(r); if (l !== void 0) return i.debug && console.info("[Gorgon] Cache hit for key: " + r, l), t._callHooks("get", { key: r, asyncFunc: e, policy: a, cacheHit: !0, queued: !1 }, l), l; if (u.call(o, r) && Array.isArray(o[r]) && o[r].length > 0) { var v = !1; for (var f in o[r]) o[r][f].queued < new Date(Date.now() - i.retry) && (v = !0); if (!v) { i.debug && console.info("[Gorgon] Cache miss, in progress, adding to current queue for key: " + r); var h = new Promise(function(c, d) { o[r].push({ res: c, rej: d, queued: /* @__PURE__ */ new Date() }); }); return t._callHooks("get", { key: r, asyncFunc: e, policy: a, cacheHit: !1, queued: !0 }, h), h; } } else o[r] = [{ queued: /* @__PURE__ */ new Date() }]; try { i.debug && console.info("[Gorgon] Cache miss, resolving item for: " + r); const c = e(); t._callHooks("get", { key: r, asyncFunc: e, policy: a, cacheHit: !1, queued: !1 }, c); const d = await c; i.debug && console.info("[Gorgon] Cache resolved, resolved item for: " + r, d); const p = await t.put(r, d, s(a)); if (u.call(o, r)) { for (var f in o[r]) o[r][f].res && (i.debug && console.info("[Gorgon] Cache queue resolved for: " + r, d), o[r][f].res(p)); o[r] = [], delete o[r]; } return p; } catch (c) { if (u.call(o, r)) { for (var f in o[r]) o[r][f].rej && o[r][f].rej(c); t._callHooks("valueError", { key: r, asyncFunc: e, policy: a, cacheHit: !1, queued: !1 }, c), o[r] = [], delete o[r]; } throw c; } } }; return t.addProvider("memory", g()), t; })(); export { g as MemoryCache, m as default }; //# sourceMappingURL=index.es.js.map