donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
42 lines • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestContextHolder = void 0;
const async_hooks_1 = require("async_hooks");
const RequestContext_1 = require("../models/RequestContext");
class RequestContextHolder {
constructor() {
this.fallbackContext = new RequestContext_1.RequestContext(null);
}
/**
* Returns the current request context if one exists for this async context,
* otherwise returns the fallback context.
*/
get() {
return (RequestContextHolder.requestContextStorage.getStore() ??
this.fallbackContext);
}
/**
* Sets the context-local request context for this request.
* Should be called at the start of handling a request.
*/
setForCurrentContext(context) {
RequestContextHolder.requestContextStorage.enterWith(context);
}
/**
* Clears the context-local request context after a request is finished.
*/
clearForCurrentContext() {
// Note: AsyncLocalStorage doesn't have a direct "remove" equivalent
// The context will be automatically cleaned up when the async context ends
RequestContextHolder.requestContextStorage.disable();
}
/**
* Sets a global fallback request context to use when no request context is available.
*/
setFallbackContext(context) {
this.fallbackContext = context;
}
}
exports.RequestContextHolder = RequestContextHolder;
RequestContextHolder.requestContextStorage = new async_hooks_1.AsyncLocalStorage();
//# sourceMappingURL=RequestContextHolder.js.map