1 | ;
|
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 | var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
18 | for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
19 | to[j] = from[i];
|
20 | return to;
|
21 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | exports.ContextAPI = void 0;
|
24 | var NoopContextManager_1 = require("../context/NoopContextManager");
|
25 | var global_utils_1 = require("../internal/global-utils");
|
26 | var diag_1 = require("./diag");
|
27 | var API_NAME = 'context';
|
28 | var NOOP_CONTEXT_MANAGER = new NoopContextManager_1.NoopContextManager();
|
29 | /**
|
30 | * Singleton object which represents the entry point to the OpenTelemetry Context API
|
31 | */
|
32 | var ContextAPI = /** @class */ (function () {
|
33 | /** Empty private constructor prevents end users from constructing a new instance of the API */
|
34 | function ContextAPI() {
|
35 | }
|
36 | /** Get the singleton instance of the Context API */
|
37 | ContextAPI.getInstance = function () {
|
38 | if (!this._instance) {
|
39 | this._instance = new ContextAPI();
|
40 | }
|
41 | return this._instance;
|
42 | };
|
43 | /**
|
44 | * Set the current context manager.
|
45 | *
|
46 | * @returns true if the context manager was successfully registered, else false
|
47 | */
|
48 | ContextAPI.prototype.setGlobalContextManager = function (contextManager) {
|
49 | return global_utils_1.registerGlobal(API_NAME, contextManager, diag_1.DiagAPI.instance());
|
50 | };
|
51 | /**
|
52 | * Get the currently active context
|
53 | */
|
54 | ContextAPI.prototype.active = function () {
|
55 | return this._getContextManager().active();
|
56 | };
|
57 | /**
|
58 | * Execute a function with an active context
|
59 | *
|
60 | * @param context context to be active during function execution
|
61 | * @param fn function to execute in a context
|
62 | * @param thisArg optional receiver to be used for calling fn
|
63 | * @param args optional arguments forwarded to fn
|
64 | */
|
65 | ContextAPI.prototype.with = function (context, fn, thisArg) {
|
66 | var _a;
|
67 | var args = [];
|
68 | for (var _i = 3; _i < arguments.length; _i++) {
|
69 | args[_i - 3] = arguments[_i];
|
70 | }
|
71 | return (_a = this._getContextManager()).with.apply(_a, __spreadArray([context, fn, thisArg], args));
|
72 | };
|
73 | /**
|
74 | * Bind a context to a target function or event emitter
|
75 | *
|
76 | * @param context context to bind to the event emitter or function. Defaults to the currently active context
|
77 | * @param target function or event emitter to bind
|
78 | */
|
79 | ContextAPI.prototype.bind = function (context, target) {
|
80 | return this._getContextManager().bind(context, target);
|
81 | };
|
82 | ContextAPI.prototype._getContextManager = function () {
|
83 | return global_utils_1.getGlobal(API_NAME) || NOOP_CONTEXT_MANAGER;
|
84 | };
|
85 | /** Disable and remove the global context manager */
|
86 | ContextAPI.prototype.disable = function () {
|
87 | this._getContextManager().disable();
|
88 | global_utils_1.unregisterGlobal(API_NAME, diag_1.DiagAPI.instance());
|
89 | };
|
90 | return ContextAPI;
|
91 | }());
|
92 | exports.ContextAPI = ContextAPI;
|
93 | //# sourceMappingURL=context.js.map |
\ | No newline at end of file |