openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 3.41 kB
JavaScript
import{i as resolveGlobalSingleton}from"./global-singleton-CTnqrmaI.js";import{F as runWithAsyncStore}from"./subsystem-ovCC_dQD.js";import{a as raceWithTimeout}from"./fs-safe-BnA-Wq_f.js";import"./boundary-path-93ijeUSt.js";import{i as createDeferredCore,n as materializeErrorStack}from"./error-graph-internal-CWqvi3Vy.js";import{AsyncLocalStorage}from"node:async_hooks";
//!src/shared/settle-within.ts
async function settlesWithin(e,t){return await raceWithTimeout(e.then(()=>true),t,()=>false,{ref:false})}
//!src/shared/async-work-scope.ts
const currentWorkScope=resolveGlobalSingleton(Symbol.for(`openclaw.asyncWorkScope`),()=>new AsyncLocalStorage);const currentWorkScopeAncestry=resolveGlobalSingleton(Symbol.for(`openclaw.asyncWorkScopeAncestry`),()=>new AsyncLocalStorage);const AsyncWorkScopeClosedError=resolveGlobalSingleton(Symbol.for(`openclaw.asyncWorkScopeClosedError`),()=>class ClosedError extends Error{constructor(){super(`Async work scope is closed`);this.name=`AsyncWorkScopeClosedError`}});var AsyncWorkScope=class AsyncWorkScope{constructor(e){this.failures=e;this.pending=/* @__PURE__ */ new Set;this.controller=new AbortController;this.phase=`open`}get signal(){return this.controller.signal}get hasPendingWork(){return this.pending.size>0}get isClosing(){return this.phase!==`open`}enter(e){const t=currentWorkScope.getStore();if(t===this)return e();const n=currentWorkScopeAncestry.getStore();const r=t?n?.scope===t?n:{scope:t}:void 0;return runWithAsyncStore(currentWorkScope,this,()=>{currentWorkScopeAncestry.enterWith({scope:this,parent:r});return e()})}run(e){if(this.phase===`closed`)throw new AsyncWorkScopeClosedError;if(this.pending.size>0)return this.enter(e);const t=createDeferredCore();this.pending.add(t.promise);try{return this.enter(e)}finally{t.resolve();this.pending.delete(t.promise)}}track(e){if(this.phase===`closed`)return Promise.reject(new AsyncWorkScopeClosedError);const t=createDeferredCore();this.pending.add(t.promise);t.promise.then(()=>this.pending.delete(t.promise),e=>{this.pending.delete(t.promise);if(this.failures){materializeErrorStack(e);this.failures.add(e)}});try{t.resolve(this.enter(e))}catch(e){t.reject(e)}return t.promise}beginClose(e){if(this.phase!==`open`)return;this.phase=`closing`;this.controller.abort(e);materializeErrorStack(this.controller.signal.reason)}runWhenIdle(e){return AsyncWorkScope.runWhenAllIdle(()=>[this],()=>this.track(e))}static async runWhenAllIdle(e,t){let n=e();while(n.some(e=>e.pending.size>0)){await Promise.allSettled(n.flatMap(e=>Array.from(e.pending)));n=e()}return t()}async drain(){this.beginClose();while(this.pending.size>0)await Promise.allSettled(this.pending);this.phase=`closed`}};const GatewayRetainedWorkTimeoutError=resolveGlobalSingleton(Symbol.for(`openclaw.gatewayRetainedWorkTimeoutError`),()=>class RetainedWorkTimeoutError extends Error{constructor(){super(`Retained Gateway work did not settle after abort; resources remain retained`)}});function runOutsideAsyncWorkScope(e){if(currentWorkScope.getStore()===void 0)return currentWorkScopeAncestry.getStore()===void 0?e():currentWorkScopeAncestry.exit(e);return runWithAsyncStore(currentWorkScope,void 0,()=>{currentWorkScopeAncestry.enterWith(void 0);return e()})}function getAsyncWorkSignal(){return currentWorkScope.getStore()?.signal}export{settlesWithin as i,getAsyncWorkSignal as n,runOutsideAsyncWorkScope as r,AsyncWorkScope as t};