{"version":3,"file":"FrequencyTracker.BUCifPf4.mjs","names":["useState","useRef"],"sources":["https:/framerusercontent.com/modules/snsDiK7dI2omxyWfHXcR/AlTCMLeGbHIo6PVVpCbm/FrequencyTracker.js"],"sourcesContent":["import{jsx as _jsx,jsxs as _jsxs}from\"react/jsx-runtime\";import{addPropertyControls,ControlType,RenderTarget}from\"framer\";import{useEffect,useRef,useState}from\"react\";/**\n * GridFrequencyTicker — the live fleet-median grid frequency from\n * https://live.ebx.energy/frequency.json (Linear ENG-502).\n *\n * How it stays smooth at exactly 1 Hz while polling far less often:\n *\n * - The feed publishes the last ~10 seconds as one-second \"slots\", each keyed by\n *   the epoch second `t` it belongs to. Every poll returns mostly slots we have\n *   already shown; they are dropped with one integer compare (`t > lastShownT`).\n *   Dedup is by `t`, never by value — two consecutive identical readings are\n *   common and must both be shown.\n * - Playback is anchored to wall clock: each tick shows the slot for\n *   `now − displayDelayS`. That fixed delay is what guarantees the slot has\n *   already arrived (pipeline lag + slot completion + poll period). Anchoring\n *   also self-heals: after a late poll or a hidden tab the ticker jumps to the\n *   right slot instead of draining a backlog.\n * - `generated_at` in the payload aligns the visitor's clock to the server's.\n * - Nothing is shown when the feed is `unavailable` or stale: never a frozen\n *   number.\n *\n * @framerSupportedLayoutWidth auto\n * @framerSupportedLayoutHeight auto\n */export default function GridFrequencyTicker(props){const{url=\"https://live.ebx.energy/frequency.json\",pollMs=3e3,displayDelayS=6,staleS=15,decimals=3,unit=\" Hz\",placeholder=\"—\",hideWhenUnavailable=true,color=\"#111111\",unitColor=\"#888888\",font}=props;const[hz,setHz]=useState(null);const pending=useRef(new Map);const lastShownT=useRef(0);const skew=useRef(0);const lastOkAt=useRef(0);const isCanvas=RenderTarget.current()===RenderTarget.canvas;useEffect(()=>{if(isCanvas)return;let cancelled=false;let pollTimer;let tickTimer;let controller;const nowS=()=>Math.floor(Date.now()/1e3)+skew.current;async function poll(){if(document.visibilityState!==\"visible\")return;controller?.abort();controller=new AbortController;try{const res=await fetch(url,{cache:\"no-store\",signal:controller.signal});if(!res.ok)return;const body=await res.json();if(cancelled)return;if(typeof body.generated_at===\"number\")skew.current=body.generated_at-Math.floor(Date.now()/1e3);if(body.status!==\"ok\"||!Array.isArray(body.samples)){pending.current.clear();setHz(null);return;}lastOkAt.current=nowS();for(const{t,hz}of body.samples){if(typeof t===\"number\"&&typeof hz===\"number\"&&t>lastShownT.current)pending.current.set(t,hz);}}catch{// Network hiccup: keep playing what we have; staleness handling hides us if it lasts.\n}}function tick(){const target=nowS()-displayDelayS;if(pending.current.size===0){if(target-lastShownT.current>staleS)setHz(null);return;}const oldest=Math.max(lastShownT.current+1,target-staleS);let t=target;while(t>=oldest&&!pending.current.has(t))t--;if(t>=oldest){setHz(pending.current.get(t)??null);lastShownT.current=t;for(const k of pending.current.keys())if(k<=t)pending.current.delete(k);}else if(target-lastShownT.current>staleS){setHz(null);}}function onVisibility(){if(document.visibilityState===\"visible\")poll();}poll();pollTimer=setInterval(poll,Math.max(1e3,pollMs));tickTimer=setInterval(tick,1e3);document.addEventListener(\"visibilitychange\",onVisibility);return()=>{cancelled=true;controller?.abort();if(pollTimer)clearInterval(pollTimer);if(tickTimer)clearInterval(tickTimer);document.removeEventListener(\"visibilitychange\",onVisibility);};},[url,pollMs,displayDelayS,staleS,isCanvas]);const shown=isCanvas?50.012:hz;const unavailable=shown===null;if(unavailable&&hideWhenUnavailable)return /*#__PURE__*/_jsxs(\"span\",{style:{...font,visibility:\"hidden\"},children:[50..toFixed(decimals),unit]});return /*#__PURE__*/_jsxs(\"span\",{style:{...font,color,fontVariantNumeric:\"tabular-nums\",whiteSpace:\"nowrap\"},\"aria-live\":\"off\",title:\"Live grid frequency, median across the EBX fleet\",children:[unavailable?placeholder:shown.toFixed(decimals),unavailable?null:/*#__PURE__*/_jsx(\"span\",{style:{color:unitColor},children:unit})]});}addPropertyControls(GridFrequencyTicker,{url:{type:ControlType.String,title:\"Feed URL\",defaultValue:\"https://live.ebx.energy/frequency.json\"},pollMs:{type:ControlType.Number,title:\"Poll (ms)\",defaultValue:3e3,min:1e3,max:1e4,step:500,description:\"How often to fetch. 3000–5000 is plenty; the feed carries ~10 s of history.\"},displayDelayS:{type:ControlType.Number,title:\"Delay (s)\",defaultValue:6,min:3,max:12,step:1,description:\"Playback runs this far behind real time so the next second has always arrived. Raise it if you raise Poll.\"},staleS:{type:ControlType.Number,title:\"Stale after (s)\",defaultValue:15,min:5,max:60,step:1},decimals:{type:ControlType.Number,title:\"Decimals\",defaultValue:3,min:0,max:3,step:1},unit:{type:ControlType.String,title:\"Unit\",defaultValue:\" Hz\"},placeholder:{type:ControlType.String,title:\"Placeholder\",defaultValue:\"—\"},hideWhenUnavailable:{type:ControlType.Boolean,title:\"Hide if unavailable\",defaultValue:true},font:{type:ControlType.Font,title:\"Font\",controls:\"extended\"},color:{type:ControlType.Color,title:\"Color\",defaultValue:\"#111111\"},unitColor:{type:ControlType.Color,title:\"Unit color\",defaultValue:\"#888888\"}});\nexport const __FramerMetadata__ = {\"exports\":{\"default\":{\"type\":\"reactComponent\",\"name\":\"GridFrequencyTicker\",\"slots\":[],\"annotations\":{\"framerContractVersion\":\"1\",\"framerSupportedLayoutWidth\":\"auto\",\"framerSupportedLayoutHeight\":\"auto\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./FrequencyTracker.map"],"mappings":"sMAsBG,SAAwB,EAAoB,EAAM,CAAC,GAAK,CAAC,MAAI,yCAAyC,SAAO,IAAI,gBAAc,EAAE,SAAO,GAAG,WAAS,EAAE,OAAK,MAAM,cAAY,IAAI,sBAAoB,GAAK,QAAM,UAAU,YAAU,UAAU,QAAM,EAAW,CAAC,EAAG,GAAOA,EAAS,IAAI,EAAQ,EAAQC,EAAO,IAAI,GAAG,EAAQ,EAAWA,EAAO,CAAC,EAAQ,EAAKA,EAAO,CAAC,EAAQ,EAASA,EAAO,CAAC,EAAQ,EAAS,EAAa,QAAQ,IAAI,EAAa,OAAO,MAAc,CAAC,G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