openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 3.81 kB
JavaScript
import{n as e}from"./rolldown-runtime-8BhlS34s.js";import{Et as t,Ni as n,Tt as r,ii as i}from"./control-ui-foundation-ZY_Flbbd.js";var a,o,s;function c(){return(c=e((()=>{i(),t(),a=1e4,o=class extends Error{},s=class{constructor(e){this.options=e,this.active=!0,this.proxyReady=!1,this.readyTimer=null,this.documentLoaded=!1,this.renderId=null,this.pendingDocument=null,this.activeLoad=null,queueMicrotask(()=>this.start())}get frame(){return this.options.frame}get ready(){return this.proxyReady}get loaded(){return this.documentLoaded}update(e){let t=this.options.frame!==e.frame||this.options.sandboxUrl!==e.sandboxUrl,n=this.options.documentKey!==e.documentKey;this.options=e,(t||n)&&this.reset(),t&&(this.proxyReady=!1),this.start()}setActive(e){if(e!==this.active){if(this.active=e,!e){this.clearReadyTimeout(),this.cancelLoad();return}this.start()}}reset(){this.cancelLoad(),this.clearReadyTimeout(),this.renderId=null,this.documentLoaded=!1,this.pendingDocument=null}dispose(){this.active=!1,this.reset(),this.proxyReady=!1}handleMessage(e){if(e.source===this.frame.contentWindow&&e.origin===this.options.sandboxOrigin){if(e.data?.method===`ui/notifications/sandbox-resource-loaded`){this.renderId&&e.data?.params?.renderId===this.renderId&&(this.clearReadyTimeout(),this.renderId=null,this.options.onRendered?.());return}e.data?.method===`ui/notifications/sandbox-proxy-ready`&&e.data?.params?.sandboxUrl===this.options.sandboxUrl&&(this.proxyReady=!0,this.clearReadyTimeout(),this.start())}}handleFrameError(){this.active&&!this.proxyReady&&this.frame.isConnected&&(this.clearReadyTimeout(),this.retrySandboxFrame())}start(){this.active&&this.frame.isConnected&&(this.scheduleReadyTimeout(),this.deliverDocument(),this.loadDocument())}clearReadyTimeout(){this.readyTimer!==null&&(window.clearTimeout(this.readyTimer),this.readyTimer=null)}scheduleReadyTimeout(){(!this.proxyReady||this.renderId&&this.options.onRendered)&&this.readyTimer===null&&(this.readyTimer=window.setTimeout(()=>{this.readyTimer=null,this.active&&!this.proxyReady&&this.frame.isConnected?this.retrySandboxFrame():this.active&&this.renderId&&this.frame.isConnected&&this.options.onError(new o(`Widget content did not finish loading. Reload the dashboard to try again.`))},a))}retrySandboxFrame(){this.proxyReady=!1,this.reset(),this.frame.src=this.options.sandboxUrl,this.options.onReadyTimeout(),this.start()}cancelLoad(){let e=this.activeLoad;this.activeLoad=null,e&&(window.clearTimeout(e.timeout),e.controller.abort())}async loadDocument(){let{loadDocument:e}=this.options;if(this.loaded||this.pendingDocument!==null||this.activeLoad!==null||!this.frame.contentWindow)return;let t=new AbortController,r={controller:t,timeout:window.setTimeout(()=>t.abort(new DOMException(`The operation timed out.`,`TimeoutError`)),a)};this.activeLoad=r;let i,o=new Promise((e,r)=>{i=()=>r(n(t.signal.reason)),t.signal.addEventListener(`abort`,i,{once:!0})});try{let n=await Promise.race([e(t.signal),o]);if(!this.active||this.activeLoad!==r||!this.frame.isConnected)return;this.pendingDocument=n,this.deliverDocument()}catch(e){this.activeLoad===r&&this.active&&this.frame.isConnected&&this.options.onError(e)}finally{window.clearTimeout(r.timeout),t.signal.removeEventListener(`abort`,i),this.activeLoad===r&&(this.activeLoad=null)}}deliverDocument(){let e=this.pendingDocument;this.active&&this.proxyReady&&e!==null&&this.frame.isConnected&&(this.renderId=r(),this.scheduleReadyTimeout(),this.frame.contentWindow?.postMessage({jsonrpc:`2.0`,method:`ui/notifications/sandbox-resource-ready`,params:{html:e,renderId:this.renderId,...this.options.allowScripts===!1?{allowScripts:!1}:{}}},this.options.sandboxOrigin),this.pendingDocument=null,this.documentLoaded=!0,this.options.onLoaded())}}})))()}export{c as i,o as n,s as r,a as t};