{"version":3,"file":"StackContextManager.mjs","names":[],"sources":["../src/StackContextManager.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Context, ContextManager } from '@opentelemetry/api';\nimport { ROOT_CONTEXT } from '@opentelemetry/api';\n\n/**\n * StackContextManager can track context for synchronous code (the call\n * \"stack\"). It does not track context for asynchronous code (Promises,\n * async/await).\n *\n * This can be useful as a minimal ContextManager in the web/browser\n * environment, which does not provide a native system for async context\n * tracking. Web users might consider `@opentelemetry/context-zone` as\n * an alternative. Node.js users will almost certainly want to use\n * `AsyncLocalStorageContextManager`.\n */\nexport class StackContextManager implements ContextManager {\n  /**\n   * whether the context manager is enabled or not\n   */\n  private _enabled = false;\n\n  /**\n   * Keeps the reference to current context\n   */\n  public _currentContext = ROOT_CONTEXT;\n\n  /**\n   *\n   * @param context\n   * @param target Function to be executed within the context\n   */\n  // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n  private _bindFunction<T extends Function>(\n    context = ROOT_CONTEXT,\n    target: T\n  ): T {\n    const manager = this;\n    const contextWrapper = function (this: unknown, ...args: unknown[]) {\n      return manager.with(context, () => target.apply(this, args));\n    };\n    Object.defineProperty(contextWrapper, 'length', {\n      enumerable: false,\n      configurable: true,\n      writable: false,\n      value: target.length,\n    });\n    return contextWrapper as unknown as T;\n  }\n\n  /**\n   * Returns the active context\n   */\n  active(): Context {\n    return this._currentContext;\n  }\n\n  /**\n   * Binds a the certain context or the active one to the target function and then returns the target\n   * @param context A context (span) to be bind to target\n   * @param target a function or event emitter. When target or one of its callbacks is called,\n   *  the provided context will be used as the active context for the duration of the call.\n   */\n  bind<T>(context: Context, target: T): T {\n    // if no specific context to propagate is given, we use the current one\n    if (context === undefined) {\n      context = this.active();\n    }\n    if (typeof target === 'function') {\n      return this._bindFunction(context, target);\n    }\n    return target;\n  }\n\n  /**\n   * Disable the context manager (clears the current context)\n   */\n  disable(): this {\n    this._currentContext = ROOT_CONTEXT;\n    this._enabled = false;\n    return this;\n  }\n\n  /**\n   * Enables the context manager and creates a default(root) context\n   */\n  enable(): this {\n    if (this._enabled) {\n      return this;\n    }\n    this._enabled = true;\n    this._currentContext = ROOT_CONTEXT;\n    return this;\n  }\n\n  /**\n   * Calls the callback function [fn] with the provided [context]. If [context] is undefined then it will use the window.\n   * The context will be set as active\n   * @param context\n   * @param fn Callback function\n   * @param thisArg optional receiver to be used for calling fn\n   * @param args optional arguments forwarded to fn\n   */\n  with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(\n    context: Context | null,\n    fn: F,\n    thisArg?: ThisParameterType<F>,\n    ...args: A\n  ): ReturnType<F> {\n    const previousContext = this._currentContext;\n    this._currentContext = context || ROOT_CONTEXT;\n\n    try {\n      return fn.call(thisArg, ...args);\n    } finally {\n      this._currentContext = previousContext;\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,IAAa,sBAAb,MAA2D;;;;CAIzD,WAAmB;;;;CAKnB,kBAAyB;;;;;;CAQzB,cACE,UAAU,cACV,QACG;EACH,MAAM,UAAU;EAChB,MAAM,iBAAiB,SAAyB,GAAG,MAAiB;GAClE,OAAO,QAAQ,KAAK,eAAe,OAAO,MAAM,MAAM,IAAI,CAAC;EAC7D;EACA,OAAO,eAAe,gBAAgB,UAAU;GAC9C,YAAY;GACZ,cAAc;GACd,UAAU;GACV,OAAO,OAAO;EAChB,CAAC;EACD,OAAO;CACT;;;;CAKA,SAAkB;EAChB,OAAO,KAAK;CACd;;;;;;;CAQA,KAAQ,SAAkB,QAAc;EAEtC,IAAI,YAAY,KAAA,GACd,UAAU,KAAK,OAAO;EAExB,IAAI,OAAO,WAAW,YACpB,OAAO,KAAK,cAAc,SAAS,MAAM;EAE3C,OAAO;CACT;;;;CAKA,UAAgB;EACd,KAAK,kBAAkB;EACvB,KAAK,WAAW;EAChB,OAAO;CACT;;;;CAKA,SAAe;EACb,IAAI,KAAK,UACP,OAAO;EAET,KAAK,WAAW;EAChB,KAAK,kBAAkB;EACvB,OAAO;CACT;;;;;;;;;CAUA,KACE,SACA,IACA,SACA,GAAG,MACY;EACf,MAAM,kBAAkB,KAAK;EAC7B,KAAK,kBAAkB,WAAW;EAElC,IAAI;GACF,OAAO,GAAG,KAAK,SAAS,GAAG,IAAI;EACjC,UAAU;GACR,KAAK,kBAAkB;EACzB;CACF;AACF"}