openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 3.49 kB
JavaScript
import{n as e}from"./rolldown-runtime-8BhlS34s.js";function t(e){return e?.refreshing===!0}var n,r,i;function a(){return(a=e((()=>{n=5e3,r=3,i=class{constructor(e){this.options=e,this.timer=null,this.retryInFlight=null,this.pendingIncomplete=!1,this.attempts=0,this.cycle=0,this.exhaustionReported=!1}get exhausted(){return this.exhaustionReported}observe(e,t){return this.useConnection(t),e?this.retryInFlight===null?this.timer===null?this.armRetry():`retrying`:(this.pendingIncomplete=!0,`retrying`):(this.resetCycle(),`complete`)}armRetry(){if(this.attempts>=(this.options.limit??r))return this.exhaustionReported||(this.exhaustionReported=!0,this.options.onExhausted?.()),`exhausted`;this.attempts+=1,this.pendingIncomplete=!1;let e=this.cycle,t=typeof this.options.retryMs==`function`?this.options.retryMs(this.attempts):this.options.retryMs??n;return this.timer=window.setTimeout(()=>{this.timer=null;let t;try{t=this.options.retry()}catch{return}if(!t)return;let n=Promise.resolve(t).then(()=>void 0,()=>void 0);this.retryInFlight=n,n.finally(()=>{this.cycle===e&&this.retryInFlight===n&&(this.retryInFlight=null,this.pendingIncomplete&&(this.pendingIncomplete=!1,this.armRetry()))})},t),`retrying`}startCycle(){this.resetCycle()}useConnection(e){e!==this.connection&&(this.connection=e,this.startCycle())}dispose(){this.resetCycle()}resetCycle(){this.cycle+=1,this.attempts=0,this.pendingIncomplete=!1,this.retryInFlight=null,this.exhaustionReported=!1,this.timer!==null&&(window.clearTimeout(this.timer),this.timer=null)}}})))()}function o(e){if(e.reason===`manual`)return`fetch`;if(!e.visible)return`defer`;if(e.interrupted)return`fetch`;let t=e.ttlMs??s;return e.lastLoadedAtMs!==null&&e.nowMs-e.lastLoadedAtMs<t?`skip`:`fetch`}var s,c;function l(){return(l=e((()=>{a(),s=3e5,c=class{constructor(e){this.options=e,this.lastLoadedAtMs=null,this.pendingAutomaticRefresh=!1,this.publicationPending=!1,this.reloadPending=!1,this.incompleteUsageRetry=new i({retry:()=>this.requestAndWait(`poll`),retryMs:e=>5e3*2**(e-1),onExhausted:()=>this.options.onIncompleteUsageExhausted?.()})}get incompleteUsageExhausted(){return this.incompleteUsageRetry.exhausted}setLastLoadedAtMs(e,t){return this.applyLoadState(e,t?.incomplete===!0,t?.connection)}markProviderUsage(e,n,r){let i=e?.ok===!1||e?.ok===!0&&t(e.value);return this.applyLoadState(n,i,r)}resetPayload(){this.applyLoadState(null,!1),this.reloadPending=!1,this.publicationPending=!1}dispose(){this.incompleteUsageRetry.dispose()}applyLoadState(e,t,n){let r=this.incompleteUsageRetry.observe(t,n);return this.lastLoadedAtMs=r===`complete`?e:null,r}interrupt(){this.reloadPending||=this.options.isLoading()}markLoadDeferred(){this.reloadPending=!0}beginLoad(){this.reloadPending=!1}request(e){this.requestAndWait(e)}async requestAndWait(e){if(e===`publication`&&(this.publicationPending=!0,this.reloadPending=!0),this.options.isLoading()&&e!==`manual`){this.pendingAutomaticRefresh=!0;return}this.pendingAutomaticRefresh=!1,o({reason:e,visible:document.visibilityState===`visible`&&document.hasFocus(),interrupted:this.reloadPending,nowMs:Date.now(),lastLoadedAtMs:this.lastLoadedAtMs})===`fetch`&&((e===`manual`||e!==`poll`&&!this.publicationPending)&&this.incompleteUsageRetry.startCycle(),this.publicationPending=!1,await this.options.reload(e))}flushPending(){this.pendingAutomaticRefresh&&(this.pendingAutomaticRefresh=!1,this.request(`focus`))}}})))()}function u(){return(u=e((()=>{})))()}export{t as a,a as i,c as n,l as r,u as t};