shared-timers
Version:
Unthrottleable timers that work across tabs.
5 lines (4 loc) • 3.09 kB
JavaScript
(function(e,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(e=typeof globalThis<"u"?globalThis:e||self,s(e.SharedTimers={}))})(this,function(e){"use strict";var v=Object.defineProperty;var f=e=>{throw TypeError(e)};var z=(e,s,r)=>s in e?v(e,s,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[s]=r;var p=(e,s,r)=>z(e,typeof s!="symbol"?s+"":s,r),m=(e,s,r)=>s.has(e)||f("Cannot "+r);var o=(e,s,r)=>(m(e,s,"read from private field"),r?r.call(e):s.get(e)),h=(e,s,r)=>s.has(e)?f("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(e):s.set(e,r),w=(e,s,r,a)=>(m(e,s,"write to private field"),a?a.call(e,r):s.set(e,r),r);var c;const s=`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 r(n){return new SharedWorker("data:text/javascript;charset=utf-8,"+encodeURIComponent(s),{name:n==null?void 0:n.name})}class a{constructor(t){p(this,"delay");h(this,c);this.delay=t,w(this,c,new Map)}get size(){return o(this,c).size}call(){for(const t of o(this,c).values())window.setTimeout(t)}subscribe(t,i){o(this,c).set(t,i)}unsubscribe(t){o(this,c).delete(t)}}c=new WeakMap;const l=new Map,u=new r;let y=0;const g=()=>++y,b=new Map;u.port.onmessage=({data:n})=>{var t;switch(n.type){case"tick":{(t=l.get(n.delay))==null||t.call();break}}},window.addEventListener("beforeunload",()=>{const n={type:"close"};u.port.postMessage(n)});function k(n,t){let i=l.get(t);if(!i){i=new a(t),l.set(t,i);const M={type:"subscribe",delay:t};u.port.postMessage(M)}const d=g();return i.subscribe(d,n),b.set(d,i),d}function T(n){const t=b.get(n);if(t&&(b.delete(n),t.unsubscribe(n),t.size===0)){l.delete(t.delay);const i={type:"unsubscribe",delay:t.delay};u.port.postMessage(i)}}e.clearTimer=T,e.runTimer=k,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=shared-timers.umd.cjs.map