-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcurrentScopes.ts
More file actions
93 lines (83 loc) · 3.17 KB
/
currentScopes.ts
File metadata and controls
93 lines (83 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { Scope } from '@sentry/types';
import type { Client } from '@sentry/types';
import { getGlobalSingleton } from '@sentry/utils';
import { getMainCarrier } from './asyncContext';
import { getAsyncContextStrategy } from './hub';
import { Scope as ScopeClass } from './scope';
/**
* Get the currently active scope.
*/
export function getCurrentScope(): Scope {
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
return acs.getCurrentScope();
}
/**
* Get the currently active isolation scope.
* The isolation scope is active for the current exection context.
*/
export function getIsolationScope(): Scope {
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
return acs.getIsolationScope();
}
/**
* Get the global scope.
* This scope is applied to _all_ events.
*/
export function getGlobalScope(): Scope {
return getGlobalSingleton('globalScope', () => new ScopeClass());
}
/**
* Creates a new scope with and executes the given operation within.
* The scope is automatically removed once the operation
* finishes or throws.
*/
export function withScope<T>(callback: (scope: Scope) => T): T;
/**
* Set the given scope as the active scope in the callback.
*/
export function withScope<T>(scope: Scope | undefined, callback: (scope: Scope) => T): T;
/**
* Either creates a new active scope, or sets the given scope as active scope in the given callback.
*/
export function withScope<T>(
...rest: [callback: (scope: Scope) => T] | [scope: Scope | undefined, callback: (scope: Scope) => T]
): T {
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
// If a scope is defined, we want to make this the active scope instead of the default one
if (rest.length === 2) {
const [scope, callback] = rest;
if (!scope) {
return acs.withScope(callback);
}
return acs.withSetScope(scope, callback);
}
return acs.withScope(rest[0]);
}
/**
* Attempts to fork the current isolation scope and the current scope based on the current async context strategy. If no
* async context strategy is set, the isolation scope and the current scope will not be forked (this is currently the
* case, for example, in the browser).
*
* Usage of this function in environments without async context strategy is discouraged and may lead to unexpected behaviour.
*
* This function is intended for Sentry SDK and SDK integration development. It is not recommended to be used in "normal"
* applications directly because it comes with pitfalls. Use at your own risk!
*
* @param callback The callback in which the passed isolation scope is active. (Note: In environments without async
* context strategy, the currently active isolation scope may change within execution of the callback.)
* @returns The same value that `callback` returns.
*/
export function withIsolationScope<T>(callback: (isolationScope: Scope) => T): T {
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
return acs.withIsolationScope(callback);
}
/**
* Get the currently active client.
*/
export function getClient<C extends Client>(): C | undefined {
return getCurrentScope().getClient<C>();
}