context.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. /*
  3. * Copyright The OpenTelemetry Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * https://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.ROOT_CONTEXT = exports.createContextKey = void 0;
  19. /** Get a key to uniquely identify a context value */
  20. function createContextKey(description) {
  21. // The specification states that for the same input, multiple calls should
  22. // return different keys. Due to the nature of the JS dependency management
  23. // system, this creates problems where multiple versions of some package
  24. // could hold different keys for the same property.
  25. //
  26. // Therefore, we use Symbol.for which returns the same key for the same input.
  27. return Symbol.for(description);
  28. }
  29. exports.createContextKey = createContextKey;
  30. class BaseContext {
  31. /**
  32. * Construct a new context which inherits values from an optional parent context.
  33. *
  34. * @param parentContext a context from which to inherit values
  35. */
  36. constructor(parentContext) {
  37. // for minification
  38. const self = this;
  39. self._currentContext = parentContext ? new Map(parentContext) : new Map();
  40. self.getValue = (key) => self._currentContext.get(key);
  41. self.setValue = (key, value) => {
  42. const context = new BaseContext(self._currentContext);
  43. context._currentContext.set(key, value);
  44. return context;
  45. };
  46. self.deleteValue = (key) => {
  47. const context = new BaseContext(self._currentContext);
  48. context._currentContext.delete(key);
  49. return context;
  50. };
  51. }
  52. }
  53. /** The root context is used as the default parent context when there is no active context */
  54. exports.ROOT_CONTEXT = new BaseContext();
  55. //# sourceMappingURL=context.js.map