UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines 3.37 kB
import{n as e}from"./rolldown-runtime-DkW27tQK.js";import{bn as t,nr as n}from"./control-ui-foundation-Cydpkyqa.js";var r,i;function a(){return(a=e((()=>{t(),r=1e4,i=class{constructor(e){this.options=e,this.active=!0,this.proxyReady=!1,this.readyTimer=null,this.loadedDocumentKey=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.loadedDocumentKey===this.options.documentKey}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.clearReadyTimeout()),this.start()}setActive(e){if(e!==this.active){if(this.active=e,!e){this.clearReadyTimeout(),this.cancelLoad();return}this.start()}}reset(){this.cancelLoad(),this.loadedDocumentKey=null,this.pendingDocument=null}dispose(){this.active=!1,this.clearReadyTimeout(),this.reset(),this.proxyReady=!1}handleMessage(e){e.source===this.frame.contentWindow&&e.origin===this.options.sandboxOrigin&&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.readyTimer!==null||(this.readyTimer=window.setTimeout(()=>{this.readyTimer=null,this.active&&!this.proxyReady&&this.frame.isConnected&&this.retrySandboxFrame()},r))}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{documentKey:e,loadDocument:t}=this.options;if(this.loaded||this.pendingDocument?.key===e||this.activeLoad?.key===e||!this.frame.contentWindow)return;this.cancelLoad();let i=new AbortController,a={key:e,controller:i,timeout:window.setTimeout(()=>i.abort(new DOMException(`The operation timed out.`,`TimeoutError`)),r)};this.activeLoad=a;let o,s=new Promise((e,t)=>{o=()=>t(n(i.signal.reason)),i.signal.addEventListener(`abort`,o,{once:!0})});try{let n=await Promise.race([t(i.signal),s]);if(!this.active||this.activeLoad!==a||!this.frame.isConnected)return;this.pendingDocument={key:e,html:n},this.deliverDocument()}catch(e){this.activeLoad===a&&this.active&&this.frame.isConnected&&this.options.onError(e)}finally{window.clearTimeout(a.timeout),i.signal.removeEventListener(`abort`,o),this.activeLoad===a&&(this.activeLoad=null)}}deliverDocument(){let e=this.pendingDocument;!this.active||!this.proxyReady||!e||!this.frame.isConnected||(this.frame.contentWindow?.postMessage({jsonrpc:`2.0`,method:`ui/notifications/sandbox-resource-ready`,params:{html:e.html}},this.options.sandboxOrigin),this.pendingDocument=null,this.loadedDocumentKey=e.key,this.options.onLoaded())}}})))()}export{i as n,a as r,r as t}; //# sourceMappingURL=widget-sandbox-host-BBp_TNUu.js.map