@@ -82,12 +82,35 @@ export interface DomainAsCarrier extends Carrier {
82
82
members : { [ key : string ] : any } [ ] ;
83
83
}
84
84
85
+
86
+ /**
87
+ * Returns the topmost scope layer in the order domain > local > process.
88
+ *
89
+ * @hidden
90
+ * */
91
+ export function getStackTop ( hub : Hub ) : Layer {
92
+ return hub . _stack [ hub . _stack . length - 1 ] ;
93
+ }
94
+
95
+ /**
96
+ * Checks if this hub's version is older than the given version.
97
+ *
98
+ * @param hub The hub to check the version on.
99
+ * @param version A version number to compare to.
100
+ * @return True if the given version is newer; otherwise false.
101
+ *
102
+ * @hidden
103
+ */
104
+ export function isOlderThan ( hub : Hub , version : number ) : boolean {
105
+ return hub . _version < version ;
106
+ }
107
+
85
108
/**
86
109
* @inheritDoc
87
110
*/
88
111
export class Hub implements HubInterface {
89
112
/** Is a {@link Layer}[] containing the client and scope */
90
- private readonly _stack : Layer [ ] = [ { } ] ;
113
+ public readonly _stack : Layer [ ] = [ { } ] ;
91
114
92
115
/** Contains the last event id of a captured event. */
93
116
private _lastEventId ?: string ;
@@ -100,25 +123,18 @@ export class Hub implements HubInterface {
100
123
* @param scope bound to the hub.
101
124
* @param version number, higher number means higher priority.
102
125
*/
103
- public constructor ( client ?: Client , scope : Scope = new Scope ( ) , private readonly _version : number = API_VERSION ) {
104
- this . getStackTop ( ) . scope = scope ;
126
+ public constructor ( client ?: Client , scope : Scope = new Scope ( ) , public readonly _version : number = API_VERSION ) {
127
+ getStackTop ( this ) . scope = scope ;
105
128
if ( client ) {
106
129
this . bindClient ( client ) ;
107
130
}
108
131
}
109
132
110
- /**
111
- * @inheritDoc
112
- */
113
- public isOlderThan ( version : number ) : boolean {
114
- return this . _version < version ;
115
- }
116
-
117
133
/**
118
134
* @inheritDoc
119
135
*/
120
136
public bindClient ( client ?: Client ) : void {
121
- const top = this . getStackTop ( ) ;
137
+ const top = getStackTop ( this ) ;
122
138
top . client = client ;
123
139
if ( client && client . setupIntegrations ) {
124
140
client . setupIntegrations ( ) ;
@@ -162,23 +178,19 @@ export class Hub implements HubInterface {
162
178
* @inheritDoc
163
179
*/
164
180
public getClient < C extends Client > ( ) : C | undefined {
165
- return this . getStackTop ( ) . client as C ;
181
+ return getStackTop ( this ) . client as C ;
166
182
}
167
183
168
184
/** Returns the scope of the top stack. */
169
185
public getScope ( ) : Scope | undefined {
170
- return this . getStackTop ( ) . scope ;
186
+ return getStackTop ( this ) . scope ;
171
187
}
172
188
173
189
/** Returns the scope stack for domains or the process. */
174
190
public getStack ( ) : Layer [ ] {
175
191
return this . _stack ;
176
192
}
177
193
178
- /** Returns the topmost scope layer in the order domain > local > process. */
179
- public getStackTop ( ) : Layer {
180
- return this . _stack [ this . _stack . length - 1 ] ;
181
- }
182
194
183
195
/**
184
196
* @inheritDoc
@@ -270,7 +282,7 @@ export class Hub implements HubInterface {
270
282
* @inheritDoc
271
283
*/
272
284
public addBreadcrumb ( breadcrumb : Breadcrumb , hint ?: BreadcrumbHint ) : void {
273
- const { scope, client } = this . getStackTop ( ) ;
285
+ const { scope, client } = getStackTop ( this ) ;
274
286
275
287
if ( ! scope || ! client ) return ;
276
288
@@ -344,7 +356,7 @@ export class Hub implements HubInterface {
344
356
* @inheritDoc
345
357
*/
346
358
public configureScope ( callback : ( scope : Scope ) => void ) : void {
347
- const { scope, client } = this . getStackTop ( ) ;
359
+ const { scope, client } = getStackTop ( this ) ;
348
360
if ( scope && client ) {
349
361
callback ( scope ) ;
350
362
}
@@ -414,7 +426,7 @@ export class Hub implements HubInterface {
414
426
* @inheritDoc
415
427
*/
416
428
public endSession ( ) : void {
417
- const layer = this . getStackTop ( ) ;
429
+ const layer = getStackTop ( this ) ;
418
430
const scope = layer && layer . scope ;
419
431
const session = scope && scope . getSession ( ) ;
420
432
if ( session ) {
@@ -432,7 +444,7 @@ export class Hub implements HubInterface {
432
444
* @inheritDoc
433
445
*/
434
446
public startSession ( context ?: SessionContext ) : Session {
435
- const { scope, client } = this . getStackTop ( ) ;
447
+ const { scope, client } = getStackTop ( this ) ;
436
448
const { release, environment } = ( client && client . getOptions ( ) ) || { } ;
437
449
438
450
// Will fetch userAgent if called from browser sdk
@@ -466,7 +478,7 @@ export class Hub implements HubInterface {
466
478
* Sends the current Session on the scope
467
479
*/
468
480
private _sendSessionUpdate ( ) : void {
469
- const { scope, client } = this . getStackTop ( ) ;
481
+ const { scope, client } = getStackTop ( this ) ;
470
482
if ( ! scope ) return ;
471
483
472
484
const session = scope . getSession && scope . getSession ( ) ;
@@ -485,7 +497,7 @@ export class Hub implements HubInterface {
485
497
*/
486
498
// eslint-disable-next-line @typescript-eslint/no-explicit-any
487
499
private _invokeClient < M extends keyof Client > ( method : M , ...args : any [ ] ) : void {
488
- const { scope, client } = this . getStackTop ( ) ;
500
+ const { scope, client } = getStackTop ( this ) ;
489
501
if ( client && client [ method ] ) {
490
502
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
491
503
( client as any ) [ method ] ( ...args , scope ) ;
@@ -547,7 +559,7 @@ export function getCurrentHub(): Hub {
547
559
const registry = getMainCarrier ( ) ;
548
560
549
561
// If there's no hub, or its an old API, assign a new one
550
- if ( ! hasHubOnCarrier ( registry ) || getHubFromCarrier ( registry ) . isOlderThan ( API_VERSION ) ) {
562
+ if ( ! hasHubOnCarrier ( registry ) || isOlderThan ( getHubFromCarrier ( registry ) , API_VERSION ) ) {
551
563
setHubOnCarrier ( registry , new Hub ( ) ) ;
552
564
}
553
565
@@ -588,8 +600,8 @@ function getHubFromActiveDomain(registry: Carrier): Hub {
588
600
}
589
601
590
602
// If there's no hub on current domain, or it's an old API, assign a new one
591
- if ( ! hasHubOnCarrier ( activeDomain ) || getHubFromCarrier ( activeDomain ) . isOlderThan ( API_VERSION ) ) {
592
- const registryHubTopStack = getHubFromCarrier ( registry ) . getStackTop ( ) ;
603
+ if ( ! hasHubOnCarrier ( activeDomain ) || isOlderThan ( getHubFromCarrier ( activeDomain ) , API_VERSION ) ) {
604
+ const registryHubTopStack = getStackTop ( getHubFromCarrier ( registry ) ) ;
593
605
setHubOnCarrier ( activeDomain , new Hub ( registryHubTopStack . client , Scope . clone ( registryHubTopStack . scope ) ) ) ;
594
606
}
595
607
0 commit comments