{"version":3,"file":"context.cjs","names":["NoopContextManager","registerGlobal","DiagAPI","getGlobal"],"sources":["../../src/api/context.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { NoopContextManager } from '../context/NoopContextManager';\nimport type {\n  Context,\n  ContextManager,\n  ContextManagementToken,\n} from '../context/types';\nimport {\n  getGlobal,\n  registerGlobal,\n  unregisterGlobal,\n} from '../internal/global-utils';\nimport { DiagAPI } from './diag';\n\nconst API_NAME = 'context';\nconst NOOP_CONTEXT_MANAGER = new NoopContextManager();\nconst NOOP_TOKEN: ContextManagementToken = Object.freeze({\n  dispose: () => {},\n});\n\n/**\n * Singleton object which represents the entry point to the OpenTelemetry Context API\n *\n * @since 1.0.0\n */\nexport class ContextAPI {\n  private static _instance?: ContextAPI;\n\n  /**\n   * Tracks whether we have already warned that the active ContextManager does\n   * not implement attach(), so we warn at most once instead of on every call.\n   */\n  private _attachUnsupportedWarned = false;\n\n  /** Empty private constructor prevents end users from constructing a new instance of the API */\n  private constructor() {}\n\n  /** Get the singleton instance of the Context API */\n  public static getInstance(): ContextAPI {\n    if (!this._instance) {\n      this._instance = new ContextAPI();\n    }\n\n    return this._instance;\n  }\n\n  /**\n   * Set the current context manager.\n   *\n   * @returns true if the context manager was successfully registered, else false\n   */\n  public setGlobalContextManager(contextManager: ContextManager): boolean {\n    return registerGlobal(API_NAME, contextManager, DiagAPI.instance());\n  }\n\n  /**\n   * Get the currently active context\n   */\n  public active(): Context {\n    return this._getContextManager().active();\n  }\n\n  /**\n   * Execute a function with an active context\n   *\n   * @param context context to be active during function execution\n   * @param fn function to execute in a context\n   * @param thisArg optional receiver to be used for calling fn\n   * @param args optional arguments forwarded to fn\n   */\n  public with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(\n    context: Context,\n    fn: F,\n    thisArg?: ThisParameterType<F>,\n    ...args: A\n  ): ReturnType<F> {\n    return this._getContextManager().with(context, fn, thisArg, ...args);\n  }\n\n  /**\n   * Bind a context to a target function or event emitter\n   *\n   * @param context context to bind to the event emitter or function. Defaults to the currently active context\n   * @param target function or event emitter to bind\n   */\n  public bind<T>(context: Context, target: T): T {\n    return this._getContextManager().bind(context, target);\n  }\n\n  /**\n   * Imperatively sets `context` as active, returning a {@link ContextManagementToken} whose\n   * {@link ContextManagementToken.dispose} restores the previous context.\n   *\n   * This is a delicate, low-level API - prefer {@link with}/{@link bind}, which\n   * restore context automatically. Use `attach` only to bridge callback\n   * boundaries that `with` cannot wrap. Call `token.dispose()` when the\n   * operation is done.\n   *\n   * **Caveat for async functions:** If called inside an async function before\n   * the first `await`, the context change may leak into the caller's context\n   * and remain active there. Prefer {@link with} for async code.\n   *\n   * Support is best-effort and varies by the active ContextManager (see\n   * {@link ContextManager.attach}); if it does not implement `attach`, this logs\n   * a warning and returns a no-op token.\n   *\n   * @param context The Context to attach\n   * @returns A {@link ContextManagementToken} whose dispose() restores the previous Context\n   * @since 1.10.0\n   * @experimental This API is experimental and may change in minor releases without prior notice.\n   */\n  public attach(context: Context): ContextManagementToken {\n    const contextManager = this._getContextManager();\n    if (contextManager.attach) {\n      return contextManager.attach(context);\n    }\n    if (!this._attachUnsupportedWarned) {\n      this._attachUnsupportedWarned = true;\n      DiagAPI.instance().warn(\n        'The current ContextManager does not implement attach(). The context will not be attached. Use a ContextManager that supports attach() (e.g. AsyncLocalStorageContextManager) or use with()/bind() instead.'\n      );\n    }\n    return NOOP_TOKEN;\n  }\n\n  private _getContextManager(): ContextManager {\n    return getGlobal(API_NAME) || NOOP_CONTEXT_MANAGER;\n  }\n\n  /** Disable and remove the global context manager */\n  public disable() {\n    this._getContextManager().disable();\n    unregisterGlobal(API_NAME, DiagAPI.instance());\n  }\n}\n"],"mappings":";;;;AAkBA,MAAM,WAAW;AACjB,MAAM,uBAAuB,IAAIA,2BAAAA,mBAAmB;AACpD,MAAM,aAAqC,OAAO,OAAO,EACvD,eAAe,CAAC,EAClB,CAAC;;;;;;AAOD,IAAa,aAAb,MAAa,WAAW;;CAUtB,cAAsB;EAHtB,KAAQ,2BAA2B;CAGZ;;CAGvB,OAAc,cAA0B;EACtC,IAAI,CAAC,KAAK,WACR,KAAK,YAAY,IAAI,WAAW;EAGlC,OAAO,KAAK;CACd;;;;;;CAOA,wBAA+B,gBAAyC;EACtE,OAAOC,qBAAAA,eAAe,UAAU,gBAAgBC,aAAAA,QAAQ,SAAS,CAAC;CACpE;;;;CAKA,SAAyB;EACvB,OAAO,KAAK,mBAAmB,CAAC,CAAC,OAAO;CAC1C;;;;;;;;;CAUA,KACE,SACA,IACA,SACA,GAAG,MACY;EACf,OAAO,KAAK,mBAAmB,CAAC,CAAC,KAAK,SAAS,IAAI,SAAS,GAAG,IAAI;CACrE;;;;;;;CAQA,KAAe,SAAkB,QAAc;EAC7C,OAAO,KAAK,mBAAmB,CAAC,CAAC,KAAK,SAAS,MAAM;CACvD;;;;;;;;;;;;;;;;;;;;;;;CAwBA,OAAc,SAA0C;EACtD,MAAM,iBAAiB,KAAK,mBAAmB;EAC/C,IAAI,eAAe,QACjB,OAAO,eAAe,OAAO,OAAO;EAEtC,IAAI,CAAC,KAAK,0BAA0B;GAClC,KAAK,2BAA2B;GAChC,aAAA,QAAQ,SAAS,CAAC,CAAC,KACjB,4MACF;EACF;EACA,OAAO;CACT;CAEA,qBAA6C;EAC3C,OAAOC,qBAAAA,UAAU,QAAQ,KAAK;CAChC;;CAGA,UAAiB;EACf,KAAK,mBAAmB,CAAC,CAAC,QAAQ;EAClC,qBAAA,iBAAiB,UAAUD,aAAAA,QAAQ,SAAS,CAAC;CAC/C;AACF"}