askeroo
Version:
A modern CLI prompt library with flow control, history navigation, and conditional prompts
69 lines • 2 kB
JavaScript
/**
* RuntimeContext - Manages global runtime instance and lifecycle
*
* Provides centralized state management for the current runtime instance,
* with proper lifecycle management and error handling.
*/
class RuntimeContextManager {
currentRuntime = null;
/**
* Set the current runtime instance
*/
setCurrentRuntime(runtime) {
this.currentRuntime = runtime;
}
/**
* Get the current runtime instance
* Throws error if no runtime is available
*/
getCurrentRuntime() {
if (!this.currentRuntime) {
const stack = new Error().stack;
throw new Error("No runtime available. Make sure you're calling this from within a runtime context.\n\n" +
"Call stack:\n" +
stack);
}
return this.currentRuntime;
}
/**
* Check if a runtime is currently set
*/
hasRuntime() {
return this.currentRuntime !== null;
}
/**
* Clear the current runtime instance
*/
clearRuntime() {
this.currentRuntime = null;
}
/**
* Get runtime for plugin access with type checking
*/
getPluginRuntime(pluginType) {
const runtime = this.getCurrentRuntime();
if (!runtime[pluginType]) {
throw new Error(`Plugin "${pluginType}" is not available in the current runtime.`);
}
return runtime;
}
}
// Global singleton instance
export const runtimeContext = new RuntimeContextManager();
// Convenience functions
export function setCurrentRuntime(runtime) {
runtimeContext.setCurrentRuntime(runtime);
}
export function getCurrentRuntime() {
return runtimeContext.getCurrentRuntime();
}
export function hasRuntime() {
return runtimeContext.hasRuntime();
}
export function clearRuntime() {
runtimeContext.clearRuntime();
}
export function getPluginRuntime(pluginType) {
return runtimeContext.getPluginRuntime(pluginType);
}
//# sourceMappingURL=runtime-context.js.map