|
1 | 1 | import type { Scope } from '../scope'; |
2 | 2 | import type { Span } from '../types-hoist/span'; |
3 | 3 | import { addNonEnumerableProperty } from '../utils/object'; |
4 | | -import { GLOBAL_OBJ } from '../utils/worldwide'; |
5 | 4 |
|
6 | 5 | const SCOPE_ON_START_SPAN_FIELD = '_sentryScope'; |
7 | 6 | const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope'; |
8 | 7 |
|
9 | | -type ScopeWeakRef = { deref(): Scope | undefined } | Scope; |
10 | | - |
11 | 8 | type SpanWithScopes = Span & { |
12 | 9 | [SCOPE_ON_START_SPAN_FIELD]?: Scope; |
13 | | - [ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: ScopeWeakRef; |
| 10 | + [ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope; |
14 | 11 | }; |
15 | 12 |
|
16 | | -/** Wrap a scope with a WeakRef if available, falling back to a direct scope. */ |
17 | | -function wrapScopeWithWeakRef(scope: Scope): ScopeWeakRef { |
18 | | - try { |
19 | | - // @ts-expect-error - WeakRef is not available in all environments |
20 | | - const WeakRefClass = GLOBAL_OBJ.WeakRef; |
21 | | - if (typeof WeakRefClass === 'function') { |
22 | | - return new WeakRefClass(scope); |
23 | | - } |
24 | | - } catch { |
25 | | - // WeakRef not available or failed to create |
26 | | - // We'll fall back to a direct scope |
27 | | - } |
28 | | - |
29 | | - return scope; |
30 | | -} |
31 | | - |
32 | | -/** Try to unwrap a scope from a potential WeakRef wrapper. */ |
33 | | -function unwrapScopeFromWeakRef(scopeRef: ScopeWeakRef | undefined): Scope | undefined { |
34 | | - if (!scopeRef) { |
35 | | - return undefined; |
36 | | - } |
37 | | - |
38 | | - if (typeof scopeRef === 'object' && 'deref' in scopeRef && typeof scopeRef.deref === 'function') { |
39 | | - try { |
40 | | - return scopeRef.deref(); |
41 | | - } catch { |
42 | | - return undefined; |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - // Fallback to a direct scope |
47 | | - return scopeRef as Scope; |
48 | | -} |
49 | | - |
50 | 13 | /** Store the scope & isolation scope for a span, which can the be used when it is finished. */ |
51 | 14 | export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void { |
52 | 15 | if (span) { |
53 | | - addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, wrapScopeWithWeakRef(isolationScope)); |
54 | | - // We don't wrap the scope with a WeakRef here because webkit aggressively garbage collects |
55 | | - // and scopes are not held in memory for long periods of time. |
| 16 | + addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope); |
56 | 17 | addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope); |
57 | 18 | } |
58 | 19 | } |
59 | 20 |
|
60 | 21 | /** |
61 | 22 | * Grabs the scope and isolation scope off a span that were active when the span was started. |
62 | | - * If WeakRef was used and scopes have been garbage collected, returns undefined for those scopes. |
63 | 23 | */ |
64 | 24 | export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } { |
65 | | - const spanWithScopes = span as SpanWithScopes; |
66 | | - |
67 | 25 | return { |
68 | | - scope: spanWithScopes[SCOPE_ON_START_SPAN_FIELD], |
69 | | - isolationScope: unwrapScopeFromWeakRef(spanWithScopes[ISOLATION_SCOPE_ON_START_SPAN_FIELD]), |
| 26 | + scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD], |
| 27 | + isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD], |
70 | 28 | }; |
71 | 29 | } |
0 commit comments