{"version":3,"file":"base-context.mjs","names":[],"sources":["../../../../../../@warlock.js/context/src/base-context.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"async_hooks\";\n\n/**\n * Base class for all AsyncLocalStorage-based contexts\n *\n * Provides a consistent API for managing context across async operations.\n * All framework contexts (request, storage, database) extend this class.\n *\n * @template TStore - The type of data stored in context\n *\n * @example\n * ```typescript\n * interface MyContextStore {\n *   userId: string;\n *   tenant: string;\n * }\n *\n * class MyContext extends Context<MyContextStore> {}\n * const myContext = new MyContext();\n *\n * // Use it\n * await myContext.run({ userId: '123', tenant: 'acme' }, async () => {\n *   const userId = myContext.get('userId'); // '123'\n * });\n * ```\n */\nexport abstract class Context<TStore extends Record<string, any>> {\n  protected readonly storage: AsyncLocalStorage<TStore> = new AsyncLocalStorage<TStore>();\n\n  /**\n   * Run a callback within a new context\n   *\n   * Creates a new async context with the provided store data.\n   * All operations within the callback will have access to this context.\n   *\n   * @param store - Initial context data\n   * @param callback - Async function to execute\n   * @returns Result of the callback\n   */\n  public run<T>(store: TStore, callback: () => Promise<T>): Promise<T> {\n    return this.storage.run(store, callback);\n  }\n\n  /**\n   * Enter a new context without a callback\n   *\n   * Useful for middleware where you want to set context for the rest of the request.\n   * Unlike `run()`, this doesn't require a callback.\n   *\n   * @param store - Context data to set\n   */\n  public enter(store: TStore): void {\n    this.storage.enterWith(store);\n  }\n\n  /**\n   * Update the current context\n   *\n   * Merges new data into existing context, or enters new context if none exists.\n   *\n   * @param updates - Partial context data to merge\n   */\n  public update(updates: Partial<TStore>): void {\n    const current = this.storage.getStore();\n\n    if (current) {\n      Object.assign(current, updates);\n    } else {\n      this.enter(updates as TStore);\n    }\n  }\n\n  /**\n   * Get the current context store\n   *\n   * @returns Current context or undefined if not in context\n   */\n  public getStore(): TStore | undefined {\n    return this.storage.getStore();\n  }\n\n  /**\n   * Get a specific value from context\n   *\n   * @param key - Key to retrieve\n   * @returns Value or undefined\n   */\n  public get<K extends keyof TStore>(key: K): TStore[K] | undefined {\n    return this.storage.getStore()?.[key];\n  }\n\n  /**\n   * Set a specific value in context\n   *\n   * @param key - Key to set\n   * @param value - Value to store\n   */\n  public set<K extends keyof TStore>(key: K, value: TStore[K]): void {\n    this.update({ [key]: value } as any);\n  }\n\n  /**\n   * Clear the context\n   */\n  public clear(): void {\n    this.storage.enterWith({} as TStore);\n  }\n\n  /**\n   * Check if currently in a context\n   */\n  public hasContext(): boolean {\n    return this.storage.getStore() !== undefined;\n  }\n\n  /**\n   * Build the initial store for this context\n   *\n   * Override this method to provide custom initialization logic.\n   * Called by ContextManager.buildStores() for each registered context.\n   *\n   * @param payload - Generic payload (e.g., { request, response } for HTTP contexts)\n   * @returns Initial store data\n   */\n  public abstract buildStore(payload?: Record<string, any>): TStore;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAsB,UAAtB,MAAkE;;iBACR,IAAI,kBAA0B;;;;;;;;;;;;CAYtF,AAAO,IAAO,OAAe,UAAwC;EACnE,OAAO,KAAK,QAAQ,IAAI,OAAO,QAAQ;CACzC;;;;;;;;;CAUA,AAAO,MAAM,OAAqB;EAChC,KAAK,QAAQ,UAAU,KAAK;CAC9B;;;;;;;;CASA,AAAO,OAAO,SAAgC;EAC5C,MAAM,UAAU,KAAK,QAAQ,SAAS;EAEtC,IAAI,SACF,OAAO,OAAO,SAAS,OAAO;OAE9B,KAAK,MAAM,OAAiB;CAEhC;;;;;;CAOA,AAAO,WAA+B;EACpC,OAAO,KAAK,QAAQ,SAAS;CAC/B;;;;;;;CAQA,AAAO,IAA4B,KAA+B;EAChE,OAAO,KAAK,QAAQ,SAAS,CAAC,GAAG;CACnC;;;;;;;CAQA,AAAO,IAA4B,KAAQ,OAAwB;EACjE,KAAK,OAAO,GAAG,MAAM,MAAM,CAAQ;CACrC;;;;CAKA,AAAO,QAAc;EACnB,KAAK,QAAQ,UAAU,CAAC,CAAW;CACrC;;;;CAKA,AAAO,aAAsB;EAC3B,OAAO,KAAK,QAAQ,SAAS,MAAM;CACrC;AAYF"}