openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
45 lines (44 loc) • 1.08 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/shared/async-context.ts
const context = resolveGlobalSingleton(Symbol.for("openclaw.asyncContext"), () => new AsyncLocalStorage());
/** A field in the immutable async frame, retaining its owner's value by reference. */
var AsyncContext = class {
constructor(e) {
this.key = e;
}
getStore() {
return context.getStore()?.[this.key];
}
run(e, t) {
const n = context.getStore();
const r = n?.[this.key];
if (Object.is(r, e)) return t();
const i = {
...n,
[this.key]: e
};
context.enterWith(i);
try {
return t();
} finally {
const e = context.getStore();
context.enterWith(e === i ? n : {
...e,
[this.key]: r
});
}
}
exit(e) {
return this.run(void 0, e);
}
/** Used by the single-copy runner, which owns restoration of the entire frame. */
enterWith(e) {
context.enterWith({
...context.getStore(),
[this.key]: e
});
}
};
//#endregion
export { AsyncContext as t };