@opentelemetry/api
Version:
Public API for OpenTelemetry
1 lines • 3.09 kB
Source Map (JSON)
{"version":3,"file":"context.cjs","names":[],"sources":["../../src/context/context.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Context } from './types';\n\n/**\n * Get a key to uniquely identify a context value\n *\n * @since 1.0.0\n */\nexport function createContextKey(description: string) {\n // The specification states that for the same input, multiple calls should\n // return different keys. Due to the nature of the JS dependency management\n // system, this creates problems where multiple versions of some package\n // could hold different keys for the same property.\n //\n // Therefore, we use Symbol.for which returns the same key for the same input.\n return Symbol.for(description);\n}\n\nclass BaseContext implements Context {\n private _currentContext!: Map<symbol, unknown>;\n\n /**\n * Construct a new context which inherits values from an optional parent context.\n *\n * @param parentContext a context from which to inherit values\n */\n constructor(parentContext?: Map<symbol, unknown>) {\n // for minification\n const self = this;\n\n self._currentContext = parentContext ? new Map(parentContext) : new Map();\n\n self.getValue = (key: symbol) => self._currentContext.get(key);\n\n self.setValue = (key: symbol, value: unknown): Context => {\n const context = new BaseContext(self._currentContext);\n context._currentContext.set(key, value);\n return context;\n };\n\n self.deleteValue = (key: symbol): Context => {\n const context = new BaseContext(self._currentContext);\n context._currentContext.delete(key);\n return context;\n };\n }\n\n /**\n * Get a value from the context.\n *\n * @param key key which identifies a context value\n */\n public getValue!: (key: symbol) => unknown;\n\n /**\n * Create a new context which inherits from this context and has\n * the given key set to the given value.\n *\n * @param key context key for which to set the value\n * @param value value to set for the given key\n */\n public setValue!: (key: symbol, value: unknown) => Context;\n\n /**\n * Return a new context which inherits from this context but does\n * not contain a value for the given key.\n *\n * @param key context key for which to clear a value\n */\n public deleteValue!: (key: symbol) => Context;\n}\n\n/**\n * The root context is used as the default parent context when there is no active context\n *\n * @since 1.0.0\n */\nexport const ROOT_CONTEXT: Context = new BaseContext();\n"],"mappings":";;;;;;AAYA,SAAgB,iBAAiB,aAAqB;CAOpD,OAAO,OAAO,IAAI,WAAW;AAC/B;;;;;;AA6DA,MAAa,eAAwB,IAAI,MA3DnC,YAA+B;;;;;;CAQnC,YAAY,eAAsC;EAEhD,MAAM,OAAO;EAEb,KAAK,kBAAkB,gBAAgB,IAAI,IAAI,aAAa,oBAAI,IAAI,IAAI;EAExE,KAAK,YAAY,QAAgB,KAAK,gBAAgB,IAAI,GAAG;EAE7D,KAAK,YAAY,KAAa,UAA4B;GACxD,MAAM,UAAU,IAAI,YAAY,KAAK,eAAe;GACpD,QAAQ,gBAAgB,IAAI,KAAK,KAAK;GACtC,OAAO;EACT;EAEA,KAAK,eAAe,QAAyB;GAC3C,MAAM,UAAU,IAAI,YAAY,KAAK,eAAe;GACpD,QAAQ,gBAAgB,OAAO,GAAG;GAClC,OAAO;EACT;CACF;AAyBF,EAOqD"}