UNPKG

cozy-iiif

Version:

A developer-friendly collection of abstractions and utilities built on top of @iiif/presentation-3 and @iiif/parser

151 lines (150 loc) 4.59 kB
//#region node_modules/p-throttle/index.js var e = /* @__PURE__ */ new WeakMap(), t = /* @__PURE__ */ new WeakMap(), n = new FinalizationRegistry(({ signalWeakRef: e, weakReference: n }) => { let r = e.deref(); if (!r) return; let i = t.get(r); i && (i.throttleds.delete(n), i.throttleds.size === 0 && (r.removeEventListener("abort", i.listener), t.delete(r))); }); function r({ limit: r, interval: i, strict: a, signal: o, onDelay: s, weight: c }) { if (!Number.isFinite(r)) throw TypeError("Expected `limit` to be a finite number"); if (!Number.isFinite(i)) throw TypeError("Expected `interval` to be a finite number"); if (r < 0) throw TypeError("Expected `limit` to be >= 0"); if (i < 0) throw TypeError("Expected `interval` to be >= 0"); if (c !== void 0 && typeof c != "function") throw TypeError("Expected `weight` to be a function"); if (c && i === 0) throw TypeError("The `weight` option cannot be used with `interval` of 0"); let l = { queue: /* @__PURE__ */ new Map(), strictTicks: [], currentTick: 0, activeWeight: 0 }, u = Math.max(r, 1), d = (e) => { if (l.strictTicks.length === 0 || e.time >= l.strictTicks.at(-1).time) l.strictTicks.push(e); else { let t = l.strictTicks.findIndex((t) => t.time > e.time); l.strictTicks.splice(t, 0, e); } }; function f(e) { let t = Date.now(); return t - l.currentTick > i ? (l.activeWeight = e, l.currentTick = t, 0) : (l.activeWeight + e <= r ? l.activeWeight += e : (l.currentTick += i, l.activeWeight = e), l.currentTick - t); } function p(e) { let t = Date.now(); if (l.strictTicks.length > 0 && t - l.strictTicks.at(-1).time > i && (l.strictTicks.length = 0), c) { for (; l.strictTicks.length > 0 && t - l.strictTicks[0].time >= i;) l.strictTicks.shift(); let n = (e) => { let t = 0; for (let n of l.strictTicks) n.time <= e && e - n.time < i && (t += n.weight); return t; }; if (n(t) + e <= r) return d({ time: t, weight: e }), { delay: 0 }; let a = t; for (; n(a) + e > r;) { let e = l.strictTicks.find((e) => e.time <= a && a - e.time < i); if (!e) break; a = e.time + i; } let o = { time: a, weight: e }; return d(o), { delay: Math.max(0, a - t), tickRecord: o }; } if (l.strictTicks.length < u) return l.strictTicks.push({ time: t, weight: e }), { delay: 0 }; let n = l.strictTicks[0].time, a = l.strictTicks.at(-1).time, o = n + i, s = i > 0 ? Math.ceil(i / u) : 0, f = o <= a ? a + s : o; l.strictTicks.shift(); let p = { time: f, weight: e }; return l.strictTicks.push(p), { delay: Math.max(0, f - t), tickRecord: p }; } let m = a ? p : f; return (i) => { let u = function(...e) { if (!u.isEnabled) return (async () => i.apply(this, e))(); let t; return new Promise((n, o) => { let u = 1; if (c) { try { u = c(...e); } catch (e) { o(e); return; } if (!Number.isFinite(u) || u < 0) { o(/* @__PURE__ */ TypeError("Expected `weight` to be a finite non-negative number")); return; } if (u > r) { o(/* @__PURE__ */ TypeError(`Expected \`weight\` (${u}) to be <= \`limit\` (${r})`)); return; } } let f = m(u), p = a ? f.delay : f, h = a ? f.tickRecord : void 0, g = () => { if (h) { let e = Date.now(); if (c && h.time !== e) { h.time = e; let t = l.strictTicks.indexOf(h); l.strictTicks.splice(t, 1), d(h); } else h.time = e; } try { n(i.apply(this, e)); } catch (e) { o(e); } l.queue.delete(t); }; if (p > 0) { t = setTimeout(g, p), l.queue.set(t, o); try { s?.(...e); } catch {} } else g(); }); }; if (o?.throwIfAborted(), o) { let r = t.get(o); r || (r = { throttleds: /* @__PURE__ */ new Set(), listener: null }, r.listener = () => { for (let t of r.throttleds) { let n = t.deref(); if (!n) continue; let r = e.get(n); if (r) { for (let e of r.queue.keys()) clearTimeout(e), r.queue.get(e)(o.reason); r.queue.clear(), r.strictTicks.length = 0, r.currentTick = 0, r.activeWeight = 0; } } t.delete(o); }, t.set(o, r), o.addEventListener("abort", r.listener, { once: !0 })); let i = new WeakRef(u); r.throttleds.add(i), n.register(u, { signalWeakRef: new WeakRef(o), weakReference: i }); } return u.isEnabled = !0, Object.defineProperty(u, "queueSize", { get() { return l.queue.size; } }), e.set(u, l), u; }; } //#endregion export { r as t };