UNPKG

@gati-framework/runtime

Version:

Gati runtime execution engine for running handler-based applications

160 lines 4.65 kB
/** * @module runtime/global-context * @description Global context implementation for cross-request shared state */ import type { GlobalContext, GlobalContextOptions, LifecycleCoordinator } from './types/context.js'; import type { ModuleLoader } from './module-loader.js'; import { type ConnectionPool, type RPCCallOptions } from './module-rpc.js'; import { type MetricsClient } from './metrics-client.js'; export { type MetricsClient } from './metrics-client.js'; /** * Extended global context options with module loader and coordinator */ export interface ExtendedGlobalContextOptions extends GlobalContextOptions { /** * Optional module loader instance * If not provided, a new one will be created */ moduleLoader?: ModuleLoader; /** * Optional lifecycle coordinator for distributed systems */ coordinator?: LifecycleCoordinator; /** * Optional connection pool for module RPC * If not provided, the global pool will be used */ connectionPool?: ConnectionPool; /** * Optional RPC call options for module clients */ rpcOptions?: RPCCallOptions; /** * Whether to wrap modules with RPC clients * Default: true */ enableRPC?: boolean; /** * Optional metrics client instance * If not provided, a new one will be created */ metricsClient?: MetricsClient; /** * Observability configuration */ observability?: import('./observability-factory.js').ObservabilityConfig; } /** * Creates a global context instance * * @param options - Configuration options for the global context * @returns GlobalContext instance * * @example * ```typescript * const gctx = createGlobalContext({ * modules: { db: databaseModule }, * config: { port: 3000 }, * }); * ``` */ export declare function createGlobalContext(options?: ExtendedGlobalContextOptions): GlobalContext; /** * Registers a module in the global context * * @param gctx - Global context instance * @param name - Module name * @param module - Module instance * @param options - Optional RPC options * @throws {Error} If module with the same name already exists * * @example * ```typescript * registerModule(gctx, 'db', databaseModule); * ``` */ export declare function registerModule(gctx: GlobalContext, name: string, module: unknown, options?: { enableRPC?: boolean; rpcOptions?: RPCCallOptions; connectionPool?: ConnectionPool; }): void; /** * Retrieves a module from the global context * * @param gctx - Global context instance * @param name - Module name * @returns Module instance or undefined if not found * * @example * ```typescript * const db = getModule<DatabaseModule>(gctx, 'db'); * ``` */ export declare function getModule<T = unknown>(gctx: GlobalContext, name: string): T | undefined; /** * Shuts down the global context, calling all registered shutdown hooks * * @param gctx - Global context instance * @returns Promise that resolves when all shutdown hooks complete * * @example * ```typescript * await shutdownGlobalContext(gctx); * ``` */ export declare function shutdownGlobalContext(gctx: GlobalContext): Promise<void>; /** * Get the module loader from global context * * @param gctx - Global context instance * @returns ModuleLoader instance * * @example * ```typescript * const loader = getModuleLoader(gctx); * await loader.register(myModule, gctx); * ``` */ export declare function getModuleLoader(gctx: GlobalContext): ModuleLoader; /** * Wrap all modules in global context with RPC clients * * @param gctx - Global context instance * @param options - Optional RPC options * * @example * ```typescript * wrapModulesWithRPC(gctx, { timeout: 10000 }); * ``` */ export declare function wrapModulesWithRPC(gctx: GlobalContext, options?: { rpcOptions?: RPCCallOptions; connectionPool?: ConnectionPool; }): void; /** * Get the connection pool from global context * * @param gctx - Global context instance * @returns ConnectionPool instance * * @example * ```typescript * const pool = getConnectionPool(gctx); * const stats = pool.getStatistics(); * ``` */ export declare function getConnectionPool(gctx: GlobalContext): ConnectionPool; /** * Get the metrics client from global context * * @param gctx - Global context instance * @returns MetricsClient instance * * @example * ```typescript * const metrics = getMetricsClient(gctx); * metrics.incrementCounter('requests_total', { method: 'GET' }); * ``` */ export declare function getMetricsClient(gctx: GlobalContext): MetricsClient; //# sourceMappingURL=global-context.d.ts.map