signalforge
Version:
Fine-grained reactive state management with automatic dependency tracking - Ultra-optimized, zero dependencies
2 lines (1 loc) • 27 kB
JavaScript
;var de=Object.defineProperty;var yt=Object.getOwnPropertyDescriptor;var ht=Object.getOwnPropertyNames;var vt=Object.prototype.hasOwnProperty;var St=(e,t)=>()=>(e&&(t=e(e=0)),t);var xt=(e,t)=>{for(var r in t)de(e,r,{get:t[r],enumerable:!0})},Tt=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ht(t))!vt.call(e,o)&&o!==r&&de(e,o,{get:()=>t[o],enumerable:!(n=yt(t,o))||n.enumerable});return e};var Dt=e=>Tt(de({},"__esModule",{value:!0}),e);function wt(){return`signal_${++Pt}_${Date.now()}`}function Ee(e,t,r){let n={id:wt(),type:e,createdAt:Date.now(),label:r};if(Z.set(n.id,n),ge){for(let o of ue)if(o.onSignalCreate)try{o.onSignalCreate(n,t)}catch(s){console.error(`Error in plugin "${o.name}" onSignalCreate:`,s)}}return n}function Fe(e,t,r,n="set"){if(!ge)return;let o=Z.get(e);if(!o)return;let s={signal:o,oldValue:t,newValue:r,timestamp:Date.now(),source:n};for(let d of ue)if(d.onSignalUpdate)try{d.onSignalUpdate(s)}catch(f){console.error(`Error in plugin "${d.name}" onSignalUpdate:`,f)}}function Ue(e){if(!ge)return;let t=Z.get(e);if(t){for(let r of ue)if(r.onSignalDestroy)try{r.onSignalDestroy(t)}catch(n){console.error(`Error in plugin "${r.name}" onSignalDestroy:`,n)}Z.delete(e)}}var ue,Z,Pt,ge,_e=St(()=>{"use strict";ue=[],Z=new Map,Pt=0,ge=!0});var qt={};xt(qt,{PerformanceTab:()=>bt,SignalGraphVisualizer:()=>ft,batch:()=>Ke,clearPerformanceMetrics:()=>rt,createComputed:()=>We,createEffect:()=>He,createSignal:()=>qe,disableDevTools:()=>Ze,disableProfiler:()=>le,enableDevTools:()=>Qe,enableProfiler:()=>ae,exportSnapshot:()=>ot,flushSync:()=>Je,getBatchStats:()=>dt,getDependencies:()=>tt,getDependencyGraph:()=>z,getPerformanceMetrics:()=>re,getPerformanceSummary:()=>oe,getProfilerData:()=>ce,getSignal:()=>et,getSignalLatencyStats:()=>ct,getSubscribers:()=>nt,isDevToolsEnabled:()=>F,listSignals:()=>Y,printDependencyGraph:()=>it,untrack:()=>Xe});module.exports=Dt(qt);_e();var B=1,fe=2,pe=4,me=8,Re=16,$e=1e4,Le=new Array($e),te=0;function Ct(e){te<$e&&(e.value=void 0,e.subscribers=null,e.listeners=null,e.dependencies=null,e.computeFn=null,e.flags=0,Le[te++]=e)}var ee=new WeakMap,Be=new Map;var he=1e4,Ie=new Array(he),N=0,H=0,be=!1;function Mt(e){if(e.flags&fe)return;let t=(H+1)%he;t===N&&(console.error("[SignalForge] Batch queue overflow \u2192 forcing flush"),ye()),e.flags|=fe,Ie[H]=e,H=t,be||(be=!0,queueMicrotask(ye))}function ye(){for(be=!1;N!==H;){let e=Ie[N];N=(N+1)%he,e.flags&=~fe,e.flags&B&&K(e)}}var Ae=new Array(100),Ve=0,M=null;function kt(e){Ae[Ve++]=M,M=e}function Et(){M=Ae[--Ve]}var Ft=function(){return this._node.value},Ut=function(e){this._node.subscribers||(this._node.subscribers=new Set),this._node.subscribers.add(e)},_t=function(e){this._node.subscribers&&this._node.subscribers.delete(e)};function ve(e,t=null){let r=te>0?Le[--te]:{value:void 0,subscribers:null,listeners:null,dependencies:null,computeFn:null,flags:0};return r.value=e,r.computeFn=t,r.flags=t?Re|B:0,t&&K(r),r}function Ge(e,t){return M?(M!==e&&(Ne(e,M),$t(M,t)),e.flags&B&&e.computeFn&&K(e),e.value):(e.flags&B&&e.computeFn&&K(e),e.value)}function Bt(e,t,r){if(e.flags&Re)throw new Error("Cannot set computed signal");if(typeof r=="function"&&(r=r(e.value)),Object.is(r,e.value))return;let n=e.value;e.value=r;let o=ee.get(t);o&&Fe(o,n,r,"set"),ze(e)}function Ne(e,t){e.subscribers||(e.subscribers=new Set),e.subscribers.add(t)}function Rt(e,t){e.subscribers&&e.subscribers.delete(t)}function $t(e,t){e.dependencies||(e.dependencies=new Set),e.dependencies.has(t)||(e.dependencies.add(t),Ne(t._node,e))}function Oe(e){if(e.dependencies){for(let t of e.dependencies)Rt(t._node,e);e.dependencies.clear()}}function Se(e){if(!(e.flags&B)&&(e.flags|=B,Mt(e),e.subscribers))for(let t of e.subscribers)Se(t)}function K(e){if(e.computeFn&&!(e.flags&pe)){e.flags|=pe,Oe(e),kt(e);try{let t=e.computeFn();e.flags&=~B,e.flags&=~pe,Object.is(t,e.value)||(e.value=t,ze(e))}finally{Et()}}}function ze(e){if(e.subscribers)for(let t of e.subscribers)Se(t);if(e.flags&me&&e.listeners)for(let t of e.listeners)t(e.value)}function je(e,t){return e.listeners||(e.listeners=new Set),e.listeners.add(t),e.flags|=me,()=>{e.listeners.delete(t),e.listeners.size===0&&(e.flags&=~me)}}function xe(e){Oe(e),e.subscribers&&e.subscribers.clear(),e.listeners&&e.listeners.clear(),Ct(e)}function qe(e){let t=ve(e,null),r={get:()=>Ge(t,r),set:o=>Bt(t,r,o),subscribe:o=>je(t,o),destroy:()=>{let o=ee.get(r);o&&(Ue(o),Be.delete(o),ee.delete(r)),xe(t)},_node:t,_peek:Ft,_addSubscriber:Ut,_removeSubscriber:_t},n=Ee("signal",e);return ee.set(r,n.id),Be.set(n.id,r),r}function We(e){let t,r=M;M=null;try{t=e()}finally{M=r}let n=ve(t,e),o={get:()=>Ge(n,o),set:()=>{throw new Error("Cannot set a computed signal")},subscribe:s=>je(n,s),destroy:()=>xe(n),_node:n,_peek:()=>n.value,_addSubscriber:s=>{n.subscribers||(n.subscribers=new Set),n.subscribers.add(s)},_removeSubscriber:s=>{n.subscribers&&n.subscribers.delete(s)},_markDirty:()=>Se(n),_recompute:()=>K(n)};return o}function He(e){let t=ve(void 0,e);return()=>xe(t)}function Ke(e){let t=e();return Ye(),t}function Ye(){N!==H&&ye()}function Xe(e){let t=M;M=null;try{return e()}finally{M=t}}var Je=Ye;var Te=!1,U={enabled:!1,trackPerformance:!0,logToConsole:!1,maxPerformanceSamples:1e3,flipperEnabled:!1},R=new Map;var ne=[];var O=null;function Qe(e={}){Te=!0,Object.assign(U,e),U.enabled=!0,U.logToConsole,U.flipperEnabled&&Lt()}function Ze(){Te=!1,U.enabled=!1,O&&It()}function F(){return Te&&U.enabled}function Y(){return F()?Array.from(R.values()).sort((e,t)=>e.createdAt-t.createdAt):[]}function et(e){if(F())return R.get(e)}function tt(e){return F()?R.get(e)?.dependencies||[]:[]}function nt(e){return F()?R.get(e)?.subscribers||[]:[]}function z(){if(!F())return[];let e=[],t=new Map,r=(n,o=0)=>{let s=t.get(n);if(s!==void 0)return Math.max(s,o);t.set(n,o);let d=R.get(n);if(d)for(let f of d.subscribers)r(f,o+1);return o};for(let[n,o]of R)o.dependencies.length===0&&r(n,0);for(let[n,o]of R)e.push({id:n,type:o.type,dependencies:o.dependencies,subscribers:o.subscribers,depth:t.get(n)||0});return e.sort((n,o)=>n.depth-o.depth)}function re(e){if(!F())return[];let t=[...ne];return e?t.slice(-e):t}function oe(){if(!F())return{totalUpdates:0,totalDuration:0,averageDuration:0,slowestUpdate:null,updatesByType:{}};let e=0,t=null,r={signal:0,computed:0,effect:0};for(let s of ne)s.skipped||(e+=s.duration,r[s.type]++,(!t||s.duration>t.duration)&&(t=s));let n=ne.filter(s=>!s.skipped).length,o=n>0?e/n:0;return{totalUpdates:n,totalDuration:e,averageDuration:o,slowestUpdate:t,updatesByType:r}}function rt(){ne.length=0}function Lt(){try{typeof global<"u"&&global.__FLIPPER__&&(O=global.__FLIPPER__.addPlugin({getId:()=>"signalforge-inspector",onConnect:t=>{t.send("initialState",{signals:Y(),graph:z(),config:U}),t.receive("getSignals",()=>{t.send("signals",Y())}),t.receive("getGraph",()=>{t.send("graph",z())}),t.receive("getPerformance",()=>{t.send("performance",{metrics:re(100),summary:oe()})})},onDisconnect:()=>{O=null}}))}catch(e){}}function It(){if(O){try{O.disconnect()}catch(e){}O=null}}function ot(){return{timestamp:Date.now(),config:{...U},signals:Y(),graph:z(),performance:{metrics:re(),summary:oe()}}}function it(){if(!F())return;let e=z();console.group("SignalForge Dependency Graph");for(let t of e){let r=" ".repeat(t.depth),n=t.dependencies.length>0?` \u2190 [${t.dependencies.join(", ")}]`:"",o=t.subscribers.length>0?` \u2192 [${t.subscribers.join(", ")}]`:"";`${r}${t.id}${t.type}${n}${o}`}console.groupEnd()}var T=require("react");var $=!1,X=null,At=0,ie=new Map,se=[],Vt=new Map,Gt=new Map,De={maxSamplesPerSignal:100,maxBatchRecords:50,autoComputeStats:!1,emitEvents:!0};function ae(e){$||(De={...De,...e},$=!0,X=performance.now())}function le(){$&&($=!1)}function at(){return $}function lt(){ie.clear(),se.length=0,Vt.clear(),Gt.clear(),X=$?performance.now():null,At=0}function ce(){let e=performance.now(),t=X?e-X:0,r=new Map,n=0;for(let[s,d]of ie.entries()){if(d.length===0)continue;let f=ut(s,d);r.set(s,f),n+=d.length}let o=gt(se);return{enabled:$,startedAt:X,duration:t,signalStats:r,batchRecords:[...se],batchStats:o,totalSamples:n,totalSignals:ie.size}}function ct(e){let t=ie.get(e);return!t||t.length===0?null:ut(e,t)}function dt(){return gt(se)}function ut(e,t){if(t.length===0)return{signalId:e,type:"signal",sampleCount:0,min:0,max:0,mean:0,median:0,p95:0,p99:0,stdDev:0,avgSubscribers:0,skippedCount:0};let r=t[0].type,n=t.map(S=>S.latency),o=[...n].sort((S,P)=>S-P),s=o[0],d=o[o.length-1],m=n.reduce((S,P)=>S+P,0)/n.length,u=Math.floor(o.length/2),y=o.length%2===0?(o[u-1]+o[u])/2:o[u],h=st(o,.95),v=st(o,.99),L=n.map(S=>Math.pow(S-m,2)).reduce((S,P)=>S+P,0)/n.length,x=Math.sqrt(L),I=t.reduce((S,P)=>S+P.subscriberCount,0)/t.length,A=t.filter(S=>S.skipped).length;return{signalId:e,type:r,sampleCount:t.length,min:s,max:d,mean:m,median:y,p95:h,p99:v,stdDev:x,avgSubscribers:I,skippedCount:A}}function gt(e){if(e.length===0)return{totalBatches:0,avgDuration:0,minDuration:0,maxDuration:0,avgOperationsPerBatch:0,totalOperations:0};let t=e.map(u=>u.duration),r=e.map(u=>u.operationCount),n=e.length,o=t.reduce((u,y)=>u+y,0)/n,s=Math.min(...t),d=Math.max(...t),f=r.reduce((u,y)=>u+y,0),m=f/n;return{totalBatches:n,avgDuration:o,minDuration:s,maxDuration:d,avgOperationsPerBatch:m,totalOperations:f}}function st(e,t){if(e.length===0)return 0;let r=t*(e.length-1),n=Math.floor(r),o=Math.ceil(r),s=r-n;return n===o?e[n]:e[n]*(1-s)+e[o]*s}var Pe=class{constructor(){this.listeners=new Map;this.sequence=0}on(t,r){let n=t;return this.listeners.has(n)||this.listeners.set(n,new Set),this.listeners.get(n).add(r),()=>this.off(t,r)}off(t,r){let n=t,o=this.listeners.get(n);o&&(o.delete(r),o.size===0&&this.listeners.delete(n))}emit(t,r){let n={type:t,payload:r,timestamp:Date.now(),sequence:++this.sequence},o=this.listeners.get(t);if(o)for(let d of o)try{d(n)}catch(f){console.error(`[DevTools] Error in listener for ${t}:`,f)}let s=this.listeners.get("*");if(s)for(let d of s)try{d(n)}catch(f){console.error("[DevTools] Error in wildcard listener:",f)}}removeAllListeners(){this.listeners.clear(),this.sequence=0}getListenerCount(t){if(t)return this.listeners.get(t)?.size||0;let r=0;for(let n of this.listeners.values())r+=n.size;return r}},Nt=typeof process<"u"?process.env.NODE_ENV!=="production":!0,Ot={enabled:!1,trackPerformance:!0,logToConsole:!1,maxPerformanceSamples:1e3,slowUpdateThreshold:16,emitPerformanceWarnings:!0},zt=new Map;var jt=new Pe;function J(){return Nt&&Ot.enabled}function pt(){return J()?Array.from(zt.values()).sort((e,t)=>e.createdAt-t.createdAt):[]}function j(e,t){return jt.on(e,t)}var l=require("react/jsx-runtime"),we=class{constructor(t,r){this.repulsionForce=3e3;this.attractionForce=.01;this.centerForce=.01;this.damping=.9;this.minDistance=50;this.nodes=new Map,this.edges=[],this.width=t,this.height=r}setGraph(t,r){this.nodes=t,this.edges=r}tick(){for(let t of this.nodes.values())t.fx=0,t.fy=0;this.applyRepulsion(),this.applyAttraction(),this.applyCenterForce(),this.updatePositions()}applyRepulsion(){let t=Array.from(this.nodes.values());for(let r=0;r<t.length;r++)for(let n=r+1;n<t.length;n++){let o=t[r],s=t[n],d=s.x-o.x,f=s.y-o.y,m=d*d+f*f,u=Math.sqrt(m)||1;if(u<this.minDistance){let y=this.repulsionForce/(this.minDistance*this.minDistance),h=d/u*y,v=f/u*y;o.fx-=h,o.fy-=v,s.fx+=h,s.fy+=v}else{let y=this.repulsionForce/m,h=d/u*y,v=f/u*y;o.fx-=h,o.fy-=v,s.fx+=h,s.fy+=v}}}applyAttraction(){for(let t of this.edges){let r=this.nodes.get(t.source),n=this.nodes.get(t.target);if(!r||!n)continue;let o=n.x-r.x,s=n.y-r.y,d=Math.sqrt(o*o+s*s)||1,f=d*this.attractionForce,m=o/d*f,u=s/d*f;r.fx+=m,r.fy+=u,n.fx-=m,n.fy-=u}}applyCenterForce(){let t=this.width/2,r=this.height/2;for(let n of this.nodes.values()){let o=t-n.x,s=r-n.y;n.fx+=o*this.centerForce,n.fy+=s*this.centerForce}}updatePositions(){for(let t of this.nodes.values()){t.vx=(t.vx+t.fx)*this.damping,t.vy=(t.vy+t.fy)*this.damping,t.x+=t.vx,t.y+=t.vy;let r=50;t.x<r?(t.x=r,t.vx*=-.5):t.x>this.width-r&&(t.x=this.width-r,t.vx*=-.5),t.y<r?(t.y=r,t.vy*=-.5):t.y>this.height-r&&(t.y=this.height-r,t.vy*=-.5)}}initializePositions(){let t=this.width/2,r=this.height/2,n=Math.min(this.width,this.height)/4;for(let o of this.nodes.values())if(o.x===0&&o.y===0){let s=Math.random()*Math.PI*2,d=Math.random()*n;o.x=t+Math.cos(s)*d,o.y=r+Math.sin(s)*d,o.vx=0,o.vy=0}}},ft=({width:e=800,height:t=600,showLabels:r=!0,animate:n=!0,updateInterval:o=50,enablePhysics:s=!0,nodeRadius:d=20,highlightDuration:f=1e3})=>{let[m,u]=(0,T.useState)({nodes:new Map,edges:[]}),[y,h]=(0,T.useState)(null),v=(0,T.useRef)(new we(e,t)),k=(0,T.useRef)(null),L=(0,T.useRef)(Date.now()),x=(0,T.useCallback)(()=>{if(!J())return{nodes:new Map,edges:[]};let a=pt(),p=new Map,g=[];for(let b of a){let D=m.nodes.get(b.id);p.set(b.id,{id:b.id,type:b.type,value:b.value,label:b.name||b.id.split("_")[0],x:D?.x||0,y:D?.y||0,vx:D?.vx||0,vy:D?.vy||0,fx:0,fy:0,isUpdating:D?.isUpdating||!1,lastUpdate:b.updatedAt,subscriberCount:b.subscriberCount,dependencies:b.dependencies,subscribers:b.subscribers})}for(let b of p.values())for(let D of b.dependencies)p.has(D)&&g.push({source:b.id,target:D});return{nodes:p,edges:g}},[m.nodes]),I=(0,T.useCallback)(a=>{u(p=>{let g=x();return v.current.setGraph(g.nodes,g.edges),v.current.initializePositions(),g})},[x]),A=(0,T.useCallback)(a=>{let{id:p}=a.payload;u(g=>{let b=g.nodes.get(p);return b&&(b.isUpdating=!0,b.lastUpdate=a.timestamp,setTimeout(()=>{u(D=>{let G=D.nodes.get(p);return G&&(G.isUpdating=!1),{...D}})},f)),{...g}})},[f]),S=(0,T.useCallback)(a=>{u(p=>{let g=x();return v.current.setGraph(g.nodes,g.edges),g})},[x]),P=(0,T.useCallback)(a=>{u(p=>{let g=x();return v.current.setGraph(g.nodes,g.edges),g})},[x]);(0,T.useEffect)(()=>{if(!J())return;let a=x();u(a),v.current.setGraph(a.nodes,a.edges),v.current.initializePositions();let p=[j("signal-created",I),j("signal-updated",A),j("signal-destroyed",S),j("dependency-added",P),j("dependency-removed",P)];return()=>{p.forEach(g=>g())}},[x,I,A,S,P]),(0,T.useEffect)(()=>{if(!n||!s)return;let a=()=>{let p=Date.now();p-L.current>=o&&(v.current.tick(),u(g=>({...g})),L.current=p),k.current=requestAnimationFrame(a)};return k.current=requestAnimationFrame(a),()=>{k.current&&cancelAnimationFrame(k.current)}},[n,s,o]);let _=a=>{if(a.isUpdating)return"#ff4444";switch(a.type){case"signal":return"#4CAF50";case"computed":return"#2196F3";case"effect":return"#FF9800";default:return"#9E9E9E"}},c=a=>y===a.id?"#FFD700":"#ffffff",W=a=>new Date(a).toLocaleTimeString(),V=a=>{if(a===null)return"null";if(a===void 0)return"undefined";if(typeof a=="object")try{return JSON.stringify(a).slice(0,50)}catch{return"[Object]"}return String(a).slice(0,50)};return J()?(0,l.jsxs)("div",{style:{position:"relative",width:e,height:t,background:"#1e1e1e",borderRadius:8},children:[(0,l.jsxs)("svg",{width:e,height:t,children:[(0,l.jsx)("defs",{children:(0,l.jsx)("marker",{id:"arrowhead",markerWidth:"10",markerHeight:"10",refX:"9",refY:"3",orient:"auto",markerUnits:"strokeWidth",children:(0,l.jsx)("path",{d:"M0,0 L0,6 L9,3 z",fill:"#666"})})}),(0,l.jsx)("g",{className:"edges",children:m.edges.map((a,p)=>{let g=m.nodes.get(a.source),b=m.nodes.get(a.target);if(!g||!b)return null;let D=b.x-g.x,G=b.y-g.y,Ce=Math.sqrt(D*D+G*G)||1,Me=D/Ce*d,ke=G/Ce*d;return(0,l.jsx)("line",{x1:g.x+Me,y1:g.y+ke,x2:b.x-Me,y2:b.y-ke,stroke:"#666",strokeWidth:2,markerEnd:"url(#arrowhead)",opacity:y===g.id||y===b.id?1:.3},`${a.source}-${a.target}-${p}`)})}),(0,l.jsx)("g",{className:"nodes",children:Array.from(m.nodes.values()).map(a=>(0,l.jsxs)("g",{onMouseEnter:()=>h(a.id),onMouseLeave:()=>h(null),style:{cursor:"pointer"},children:[(0,l.jsx)("circle",{cx:a.x,cy:a.y,r:d,fill:_(a),stroke:c(a),strokeWidth:y===a.id?3:1,opacity:y&&y!==a.id?.5:1}),r&&(0,l.jsx)("text",{x:a.x,y:a.y+d+15,textAnchor:"middle",fill:"#ffffff",fontSize:"12",fontFamily:"monospace",children:a.label}),a.isUpdating&&(0,l.jsxs)("circle",{cx:a.x,cy:a.y,r:d,fill:"none",stroke:"#ff4444",strokeWidth:2,opacity:.6,children:[(0,l.jsx)("animate",{attributeName:"r",from:d,to:d*2,dur:"0.5s",repeatCount:"indefinite"}),(0,l.jsx)("animate",{attributeName:"opacity",from:"0.6",to:"0",dur:"0.5s",repeatCount:"indefinite"})]})]},a.id))})]}),y&&m.nodes.has(y)&&(0,l.jsx)("div",{style:{position:"absolute",top:10,right:10,background:"rgba(0, 0, 0, 0.9)",color:"#fff",padding:15,borderRadius:8,fontSize:12,fontFamily:"monospace",maxWidth:300,border:"2px solid #FFD700"},children:(()=>{let a=m.nodes.get(y);return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)("div",{style:{fontWeight:"bold",marginBottom:8,fontSize:14},children:a.label}),(0,l.jsxs)("div",{style:{marginBottom:4},children:[(0,l.jsx)("strong",{children:"Type:"})," ",a.type]}),(0,l.jsxs)("div",{style:{marginBottom:4},children:[(0,l.jsx)("strong",{children:"ID:"})," ",a.id]}),(0,l.jsxs)("div",{style:{marginBottom:4},children:[(0,l.jsx)("strong",{children:"Value:"})," ",V(a.value)]}),(0,l.jsxs)("div",{style:{marginBottom:4},children:[(0,l.jsx)("strong",{children:"Last Update:"})," ",W(a.lastUpdate)]}),(0,l.jsxs)("div",{style:{marginBottom:4},children:[(0,l.jsx)("strong",{children:"Subscribers:"})," ",a.subscriberCount]}),a.dependencies.length>0&&(0,l.jsxs)("div",{style:{marginBottom:4},children:[(0,l.jsx)("strong",{children:"Dependencies:"}),(0,l.jsx)("ul",{style:{margin:"4px 0",paddingLeft:20},children:a.dependencies.map(p=>{let g=m.nodes.get(p);return(0,l.jsx)("li",{children:g?.label||p},p)})})]}),a.subscribers.length>0&&(0,l.jsxs)("div",{children:[(0,l.jsx)("strong",{children:"Subscribers:"}),(0,l.jsxs)("ul",{style:{margin:"4px 0",paddingLeft:20},children:[a.subscribers.slice(0,5).map(p=>{let g=m.nodes.get(p);return(0,l.jsx)("li",{children:g?.label||p},p)}),a.subscribers.length>5&&(0,l.jsxs)("li",{children:["... and ",a.subscribers.length-5," more"]})]})]})]})})()}),(0,l.jsxs)("div",{style:{position:"absolute",bottom:10,left:10,background:"rgba(0, 0, 0, 0.8)",color:"#fff",padding:10,borderRadius:8,fontSize:11,fontFamily:"sans-serif"},children:[(0,l.jsx)("div",{style:{marginBottom:5,fontWeight:"bold"},children:"Legend"}),(0,l.jsxs)("div",{style:{display:"flex",alignItems:"center",marginBottom:3},children:[(0,l.jsx)("div",{style:{width:12,height:12,background:"#4CAF50",borderRadius:"50%",marginRight:6}}),"Signal"]}),(0,l.jsxs)("div",{style:{display:"flex",alignItems:"center",marginBottom:3},children:[(0,l.jsx)("div",{style:{width:12,height:12,background:"#2196F3",borderRadius:"50%",marginRight:6}}),"Computed"]}),(0,l.jsxs)("div",{style:{display:"flex",alignItems:"center",marginBottom:3},children:[(0,l.jsx)("div",{style:{width:12,height:12,background:"#FF9800",borderRadius:"50%",marginRight:6}}),"Effect"]}),(0,l.jsxs)("div",{style:{display:"flex",alignItems:"center"},children:[(0,l.jsx)("div",{style:{width:12,height:12,background:"#ff4444",borderRadius:"50%",marginRight:6}}),"Updating"]})]}),(0,l.jsxs)("div",{style:{position:"absolute",top:10,left:10,background:"rgba(0, 0, 0, 0.8)",color:"#fff",padding:10,borderRadius:8,fontSize:11,fontFamily:"monospace"},children:[(0,l.jsxs)("div",{children:["Signals: ",m.nodes.size]}),(0,l.jsxs)("div",{children:["Edges: ",m.edges.length]})]})]}):(0,l.jsxs)("div",{style:{padding:20,color:"#ff0000"},children:["DevTools is not enabled. Call ",(0,l.jsx)("code",{children:"enableDevTools()"})," to use the visualizer."]})};var E=require("react");var i=require("react/jsx-runtime"),bt=({refreshInterval:e=1e3,maxBatchRecords:t=20,showDetails:r=!0,className:n="",style:o={}})=>{let[s,d]=(0,E.useState)(null),[f,m]=(0,E.useState)(null),[u,y]=(0,E.useState)("mean"),[h,v]=(0,E.useState)("desc"),[k,L]=(0,E.useState)(at()),x=()=>{let c=ce();d(c)};(0,E.useEffect)(()=>{if(e>0){let c=setInterval(x,e);return()=>clearInterval(c)}},[e]),(0,E.useEffect)(()=>{x()},[]);let I=()=>{k?le():ae(),L(!k),x()},A=()=>{lt(),x()},S=()=>{if(!s)return;let c=JSON.stringify(s,(p,g)=>g instanceof Map?Object.fromEntries(g):g,2),W=new Blob([c],{type:"application/json"}),V=URL.createObjectURL(W),a=document.createElement("a");a.href=V,a.download=`signalforge-performance-${Date.now()}.json`,a.click(),URL.revokeObjectURL(V)},P=(0,E.useMemo)(()=>s?Array.from(s.signalStats.values()).sort((W,V)=>{let a=W[u],p=V[u],g=a<p?-1:a>p?1:0;return h==="asc"?g:-g}):[],[s,u,h]),_=c=>{u===c?v(h==="asc"?"desc":"asc"):(y(c),v("desc"))};return s?(0,i.jsxs)("div",{className:`performance-tab ${n}`,style:{padding:"20px",fontFamily:"system-ui, sans-serif",...o},children:[(0,i.jsxs)("div",{style:{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:"20px"},children:[(0,i.jsx)("h2",{style:{margin:0},children:"Performance Profiler"}),(0,i.jsxs)("div",{style:{display:"flex",gap:"10px"},children:[(0,i.jsx)("button",{onClick:I,style:Q,children:k?"\u23F8\uFE0F Pause":"\u25B6\uFE0F Start"}),(0,i.jsx)("button",{onClick:A,style:Q,children:"\u{1F504} Reset"}),(0,i.jsx)("button",{onClick:S,style:Q,children:"\u{1F4E5} Export"}),(0,i.jsx)("button",{onClick:x,style:Q,children:"\u27F3 Refresh"})]})]}),(0,i.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"repeat(auto-fit, minmax(200px, 1fr))",gap:"15px",marginBottom:"30px"},children:[(0,i.jsx)(q,{title:"Total Signals",value:s.totalSignals,icon:"\u{1F4CA}"}),(0,i.jsx)(q,{title:"Total Samples",value:s.totalSamples,icon:"\u{1F4C8}"}),(0,i.jsx)(q,{title:"Total Batches",value:s.batchStats.totalBatches,icon:"\u{1F500}"}),(0,i.jsx)(q,{title:"Avg Batch Duration",value:`${s.batchStats.avgDuration.toFixed(2)}ms`,icon:"\u23F1\uFE0F"}),(0,i.jsx)(q,{title:"Session Duration",value:`${(s.duration/1e3).toFixed(1)}s`,icon:"\u23F3"}),(0,i.jsx)(q,{title:"Status",value:k?"Running":"Paused",icon:k?"\u2705":"\u23F8\uFE0F"})]}),(0,i.jsxs)("section",{style:{marginBottom:"30px"},children:[(0,i.jsx)("h3",{children:"Signal Latency Statistics"}),P.length===0?(0,i.jsx)("p",{style:{color:"#666"},children:"No signals profiled yet. Start using signals to see data."}):(0,i.jsxs)("table",{style:mt,children:[(0,i.jsx)("thead",{children:(0,i.jsxs)("tr",{children:[(0,i.jsxs)("th",{style:w,onClick:()=>_("signalId"),children:["Signal ID ",u==="signalId"&&(h==="asc"?"\u2191":"\u2193")]}),(0,i.jsx)("th",{style:w,children:"Type"}),(0,i.jsxs)("th",{style:w,onClick:()=>_("sampleCount"),children:["Samples ",u==="sampleCount"&&(h==="asc"?"\u2191":"\u2193")]}),(0,i.jsxs)("th",{style:w,onClick:()=>_("mean"),children:["Mean ",u==="mean"&&(h==="asc"?"\u2191":"\u2193")]}),(0,i.jsx)("th",{style:w,children:"Median"}),(0,i.jsxs)("th",{style:w,onClick:()=>_("p95"),children:["P95 ",u==="p95"&&(h==="asc"?"\u2191":"\u2193")]}),(0,i.jsxs)("th",{style:w,onClick:()=>_("p99"),children:["P99 ",u==="p99"&&(h==="asc"?"\u2191":"\u2193")]}),(0,i.jsxs)("th",{style:w,onClick:()=>_("max"),children:["Max ",u==="max"&&(h==="asc"?"\u2191":"\u2193")]}),(0,i.jsx)("th",{style:w,children:"Skipped"})]})}),(0,i.jsx)("tbody",{children:P.map(c=>(0,i.jsxs)("tr",{style:{backgroundColor:f===c.signalId?"#e3f2fd":"transparent",cursor:"pointer"},onClick:()=>m(c.signalId),children:[(0,i.jsx)("td",{style:C,children:c.signalId}),(0,i.jsx)("td",{style:C,children:c.type}),(0,i.jsx)("td",{style:C,children:c.sampleCount}),(0,i.jsxs)("td",{style:C,children:[c.mean.toFixed(2),"ms"]}),(0,i.jsxs)("td",{style:C,children:[c.median.toFixed(2),"ms"]}),(0,i.jsxs)("td",{style:C,children:[c.p95.toFixed(2),"ms"]}),(0,i.jsxs)("td",{style:C,children:[c.p99.toFixed(2),"ms"]}),(0,i.jsxs)("td",{style:C,children:[c.max.toFixed(2),"ms"]}),(0,i.jsx)("td",{style:C,children:c.skippedCount})]},c.signalId))})]})]}),s.batchRecords.length>0&&(0,i.jsxs)("section",{style:{marginBottom:"30px"},children:[(0,i.jsx)("h3",{children:"Recent Batch Operations"}),(0,i.jsxs)("table",{style:mt,children:[(0,i.jsx)("thead",{children:(0,i.jsxs)("tr",{children:[(0,i.jsx)("th",{style:w,children:"Batch ID"}),(0,i.jsx)("th",{style:w,children:"Duration"}),(0,i.jsx)("th",{style:w,children:"Operations"}),(0,i.jsx)("th",{style:w,children:"Signals Affected"}),(0,i.jsx)("th",{style:w,children:"Depth"})]})}),(0,i.jsx)("tbody",{children:s.batchRecords.slice(-t).map(c=>(0,i.jsxs)("tr",{children:[(0,i.jsxs)("td",{style:C,children:["#",c.batchId]}),(0,i.jsxs)("td",{style:C,children:[c.duration.toFixed(2),"ms"]}),(0,i.jsx)("td",{style:C,children:c.operationCount}),(0,i.jsx)("td",{style:C,children:c.affectedSignals.length}),(0,i.jsx)("td",{style:C,children:c.depth})]},c.batchId))})]})]}),r&&f&&(0,i.jsxs)("section",{style:{marginTop:"30px",padding:"15px",backgroundColor:"#f5f5f5",borderRadius:"8px"},children:[(0,i.jsxs)("h3",{children:["Signal Details: ",f]}),(()=>{let c=s.signalStats.get(f);return c?(0,i.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:"10px"},children:[(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Type:"})," ",c.type]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Sample Count:"})," ",c.sampleCount]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Min Latency:"})," ",c.min.toFixed(2),"ms"]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Max Latency:"})," ",c.max.toFixed(2),"ms"]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Mean Latency:"})," ",c.mean.toFixed(2),"ms"]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Median Latency:"})," ",c.median.toFixed(2),"ms"]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"P95 Latency:"})," ",c.p95.toFixed(2),"ms"]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"P99 Latency:"})," ",c.p99.toFixed(2),"ms"]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Std Deviation:"})," ",c.stdDev.toFixed(2),"ms"]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Avg Subscribers:"})," ",c.avgSubscribers.toFixed(1)]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Skipped Updates:"})," ",c.skippedCount]}),(0,i.jsxs)("div",{children:[(0,i.jsx)("strong",{children:"Effective Updates:"})," ",c.sampleCount-c.skippedCount]})]}):null})(),(0,i.jsx)("button",{onClick:()=>m(null),style:{...Q,marginTop:"15px"},children:"Close Details"})]})]}):(0,i.jsx)("div",{className:`performance-tab ${n}`,style:{padding:"20px",...o},children:(0,i.jsx)("p",{children:"Loading profiler data..."})})},q=({title:e,value:t,icon:r})=>(0,i.jsxs)("div",{style:{padding:"15px",backgroundColor:"#fff",border:"1px solid #e0e0e0",borderRadius:"8px",boxShadow:"0 2px 4px rgba(0,0,0,0.1)"},children:[(0,i.jsx)("div",{style:{fontSize:"24px",marginBottom:"5px"},children:r}),(0,i.jsx)("div",{style:{fontSize:"12px",color:"#666",marginBottom:"5px"},children:e}),(0,i.jsx)("div",{style:{fontSize:"20px",fontWeight:"bold"},children:t})]}),Q={padding:"8px 16px",backgroundColor:"#2196F3",color:"#fff",border:"none",borderRadius:"4px",cursor:"pointer",fontSize:"14px"},mt={width:"100%",borderCollapse:"collapse",backgroundColor:"#fff",boxShadow:"0 2px 4px rgba(0,0,0,0.1)"},w={padding:"12px",backgroundColor:"#f5f5f5",borderBottom:"2px solid #e0e0e0",textAlign:"left",fontWeight:"bold",cursor:"pointer",userSelect:"none"},C={padding:"10px 12px",borderBottom:"1px solid #e0e0e0"};