1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
|
|
import{Fn as e,Ln as t,Pn as n,Qt as r,qn as i,rn as a,zn as o}from"./index-CjPx3lfT.js";function s(e){return typeof e==`function`}function c(e,...t){return s(e)?e(...t):e}var l,u,d,f,p,m,h,g,_,v,y,b,x,S,C,w,T=(l=new WeakMap,u=new WeakMap,d=new WeakMap,f=new WeakMap,p=new WeakMap,m=new WeakMap,h=new WeakMap,g=new WeakMap,_=new WeakMap,v=new WeakMap,y=new WeakMap,b=new WeakMap,x=new WeakMap,S=new WeakMap,C=new WeakMap,w=new WeakMap,class{constructor({pluginId:r,debug:i=!1,enabled:a=!0,reconnectEveryMs:o=300}){t(this,l,!0),t(this,u,void 0),t(this,d,void 0),t(this,f,void 0),t(this,p,void 0),t(this,m,void 0),t(this,h,void 0),t(this,g,void 0),t(this,_,0),t(this,v,5),t(this,y,!1),t(this,b,!1),t(this,x,null),t(this,S,()=>{this.debugLog(`Connected to event bus`),n(m,this,!0),n(y,this,!1),this.debugLog(`Emitting queued events`,e(p,this)),e(p,this).forEach(e=>this.emitEventToBus(e)),n(p,this,[]),this.stopConnectLoop(),e(d,this).call(this).removeEventListener(`tanstack-connect-success`,e(S,this))}),t(this,C,()=>{if(e(_,this)<e(v,this)){var t;n(_,this,(t=e(_,this),t++,t)),this.dispatchCustomEvent(`tanstack-connect`,{});return}e(d,this).call(this).removeEventListener(`tanstack-connect`,e(C,this)),n(b,this,!0),this.debugLog(`Max retries reached, giving up on connection`),this.stopConnectLoop()}),t(this,w,()=>{e(y,this)||(n(y,this,!0),e(d,this).call(this).addEventListener(`tanstack-connect-success`,e(S,this)),e(C,this).call(this))}),n(u,this,r),n(l,this,a),n(d,this,this.getGlobalTarget),n(f,this,i),this.debugLog(` Initializing event subscription for plugin`,e(u,this)),n(p,this,[]),n(m,this,!1),n(b,this,!1),n(h,this,null),n(g,this,o)}startConnectLoop(){e(h,this)!==null||e(m,this)||(this.debugLog(`Starting connect loop (every ${e(g,this)}ms)`),n(h,this,setInterval(e(C,this),e(g,this))))}stopConnectLoop(){n(y,this,!1),e(h,this)!==null&&(clearInterval(e(h,this)),n(h,this,null),n(p,this,[]),this.debugLog(`Stopped connect loop`))}debugLog(...t){e(f,this)&&console.log(`🌴 [tanstack-devtools:${e(u,this)}-plugin]`,...t)}getGlobalTarget(){if(typeof globalThis<`u`&&globalThis.__TANSTACK_EVENT_TARGET__)return this.debugLog(`Using global event target`),globalThis.__TANSTACK_EVENT_TARGET__;if(typeof window<`u`&&window.addEventListener!==void 0)return this.debugLog(`Using window as event target`),window;let e=typeof EventTarget<`u`?new EventTarget:void 0;return e===void 0||e.addEventListener===void 0?(this.debugLog(`No event mechanism available, running in non-web environment`),{addEventListener:()=>{},removeEventListener:()=>{},dispatchEvent:()=>!1}):(this.debugLog(`Using new EventTarget as fallback`),e)}getPluginId(){return e(u,this)}dispatchCustomEventShim(t,n){try{let r=new Event(t,{detail:n});e(d,this).call(this).dispatchEvent(r)}catch{this.debugLog(`Failed to dispatch shim event`)}}dispatchCustomEvent(t,n){try{e(d,this).call(this).dispatchEvent(new CustomEvent(t,{detail:n}))}catch{this.dispatchCustomEventShim(t,n)}}emitEventToBus(e){this.debugLog(`Emitting event to client bus`,e),this.dispatchCustomEvent(`tanstack-dispatch-event`,e)}createEventPayload(t,n){return{type:`${e(u,this)}:${t}`,payload:n,pluginId:e(u,this)}}emit(t,n){if(!e(l,this)){this.debugLog(`Event bus client is disabled, not emitting event`,t,n);return}if(e(x,this)&&(this.debugLog(`Emitting event to internal event target`,t,n),e(x,this).dispatchEvent(new CustomEvent(`${e(u,this)}:${t}`,{detail:this.createEventPayload(t,n)}))),e(b,this)){this.debugLog(`Previously failed to connect, not emitting to bus`);return}if(!e(m,this)){this.debugLog(`Bus not available, will be pushed as soon as connected`),e(p,this).push(this.createEventPayload(t,n)),typeof CustomEvent<`u`&&!e(y,this)&&(e(w,this).call(this),this.startConnectLoop());return}return this.emitEventToBus(this.createEventPayload(t,n))}on(t,r,i){let a=(i==null?void 0:i.withEventTarget)??!1,o=`${e(u,this)}:${t}`;if(a&&(e(x,this)||n(x,this,new EventTarget),e(x,this).addEventListener(o,e=>{r(e.detail)})),!e(l,this))return this.debugLog(`Event bus client is disabled, not registering event`,o),()=>{};let s=e=>{this.debugLog(`Received
|