import { AsyncLocalStorage } from "async_hooks"; //#region ../@warlock.js/context/src/base-context.d.ts /** * Base class for all AsyncLocalStorage-based contexts * * Provides a consistent API for managing context across async operations. * All framework contexts (request, storage, database) extend this class. * * @template TStore - The type of data stored in context * * @example * ```typescript * interface MyContextStore { * userId: string; * tenant: string; * } * * class MyContext extends Context {} * const myContext = new MyContext(); * * // Use it * await myContext.run({ userId: '123', tenant: 'acme' }, async () => { * const userId = myContext.get('userId'); // '123' * }); * ``` */ declare abstract class Context> { protected readonly storage: AsyncLocalStorage; /** * Run a callback within a new context * * Creates a new async context with the provided store data. * All operations within the callback will have access to this context. * * @param store - Initial context data * @param callback - Async function to execute * @returns Result of the callback */ run(store: TStore, callback: () => Promise): Promise; /** * Enter a new context without a callback * * Useful for middleware where you want to set context for the rest of the request. * Unlike `run()`, this doesn't require a callback. * * @param store - Context data to set */ enter(store: TStore): void; /** * Update the current context * * Merges new data into existing context, or enters new context if none exists. * * @param updates - Partial context data to merge */ update(updates: Partial): void; /** * Get the current context store * * @returns Current context or undefined if not in context */ getStore(): TStore | undefined; /** * Get a specific value from context * * @param key - Key to retrieve * @returns Value or undefined */ get(key: K): TStore[K] | undefined; /** * Set a specific value in context * * @param key - Key to set * @param value - Value to store */ set(key: K, value: TStore[K]): void; /** * Clear the context */ clear(): void; /** * Check if currently in a context */ hasContext(): boolean; /** * Build the initial store for this context * * Override this method to provide custom initialization logic. * Called by ContextManager.buildStores() for each registered context. * * @param payload - Generic payload (e.g., { request, response } for HTTP contexts) * @returns Initial store data */ abstract buildStore(payload?: Record): TStore; } //#endregion export { Context }; //# sourceMappingURL=base-context.d.mts.map