UNPKG

shared-timers

Version:

Unthrottleable timers that work across tabs.

80 lines (79 loc) 3.32 kB
var h = Object.defineProperty; var l = (s) => { throw TypeError(s); }; var f = (s, e, t) => e in s ? h(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; var u = (s, e, t) => f(s, typeof e != "symbol" ? e + "" : e, t), b = (s, e, t) => e.has(s) || l("Cannot " + t); var c = (s, e, t) => (b(s, e, "read from private field"), t ? t.call(s) : e.get(s)), p = (s, e, t) => e.has(s) ? l("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(s) : e.set(s, t), d = (s, e, t, a) => (b(s, e, "write to private field"), a ? a.call(s, t) : e.set(s, t), t); const w = `var l=s=>{throw TypeError(s)};var m=(s,e,t)=>e.has(s)||l("Cannot "+t);var n=(s,e,t)=>(m(s,e,"read from private field"),t?t.call(s):e.get(s)),b=(s,e,t)=>e.has(s)?l("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(s):e.set(s,t),u=(s,e,t,r)=>(m(s,e,"write to private field"),r?r.call(s,t):e.set(s,t),t);(function(){"use strict";var t,r;class s{constructor(i){b(this,t);b(this,r);u(this,r,new Set);let a=self.performance.now()+i;const o=()=>{const c={type:"tick",delay:i};n(this,r).forEach(h=>h.postMessage(c));const p=self.performance.now()-a;a+=i,u(this,t,self.setTimeout(o,Math.max(0,i-p)))};u(this,t,self.setTimeout(o,i))}get size(){return n(this,r).size}subscribe(i){n(this,r).add(i)}unsubscribe(i){n(this,r).delete(i)}stop(){self.clearTimeout(n(this,t))}}t=new WeakMap,r=new WeakMap;const e=new Map;self.onconnect=f=>{const i=f.ports[0];i.onmessage=a=>{const{data:o}=a;switch(o.type){case"close":{for(const c of e.values())c.unsubscribe(i);break}case"subscribe":{let c=e.get(o.delay);c||(c=new s(o.delay),e.set(o.delay,c)),c.subscribe(i);break}case"unsubscribe":{const c=e.get(o.delay);c&&(c.unsubscribe(i),c.size===0&&(c.stop(),e.delete(o.delay)));break}}}}})(); //# sourceMappingURL=worker-D5KN6Hbo.js.map `; function y(s) { return new SharedWorker( "data:text/javascript;charset=utf-8," + encodeURIComponent(w), { name: s == null ? void 0 : s.name } ); } var r; class g { constructor(e) { u(this, "delay"); p(this, r); this.delay = e, d(this, r, /* @__PURE__ */ new Map()); } get size() { return c(this, r).size; } call() { for (const e of c(this, r).values()) window.setTimeout(e); } subscribe(e, t) { c(this, r).set(e, t); } unsubscribe(e) { c(this, r).delete(e); } } r = new WeakMap(); const n = /* @__PURE__ */ new Map(), o = new y(); let k = 0; const M = () => ++k, i = /* @__PURE__ */ new Map(); o.port.onmessage = ({ data: s }) => { var e; switch (s.type) { case "tick": { (e = n.get(s.delay)) == null || e.call(); break; } } }; window.addEventListener("beforeunload", () => { const s = { type: "close" }; o.port.postMessage(s); }); function T(s, e) { let t = n.get(e); if (!t) { t = new g(e), n.set(e, t); const m = { type: "subscribe", delay: e }; o.port.postMessage(m); } const a = M(); return t.subscribe(a, s), i.set(a, t), a; } function z(s) { const e = i.get(s); if (e && (i.delete(s), e.unsubscribe(s), e.size === 0)) { n.delete(e.delay); const t = { type: "unsubscribe", delay: e.delay }; o.port.postMessage(t); } } export { z as clearTimer, T as runTimer }; //# sourceMappingURL=shared-timers.js.map