@@ -50,7 +50,7 @@ const MAX_BREADCRUMBS = 100;
50
50
*/
51
51
export class Hub implements HubInterface {
52
52
/** Is a {@link Layer}[] containing the client and scope */
53
- private readonly _stack : Layer [ ] = [ ] ;
53
+ private readonly _stack : Layer [ ] = [ { } ] ;
54
54
55
55
/** Contains the last event id of a captured event. */
56
56
private _lastEventId ?: string ;
@@ -64,7 +64,7 @@ export class Hub implements HubInterface {
64
64
* @param version number, higher number means higher priority.
65
65
*/
66
66
public constructor ( client ?: Client , scope : Scope = new Scope ( ) , private readonly _version : number = API_VERSION ) {
67
- this . _stack . push ( { client , scope } ) ;
67
+ this . getStackTop ( ) . scope = scope ;
68
68
this . bindClient ( client ) ;
69
69
}
70
70
@@ -91,9 +91,7 @@ export class Hub implements HubInterface {
91
91
*/
92
92
public pushScope ( ) : Scope {
93
93
// We want to clone the content of prev scope
94
- const stack = this . getStack ( ) ;
95
- const parentScope = stack . length > 0 ? stack [ stack . length - 1 ] . scope : undefined ;
96
- const scope = Scope . clone ( parentScope ) ;
94
+ const scope = Scope . clone ( this . getScope ( ) ) ;
97
95
this . getStack ( ) . push ( {
98
96
client : this . getClient ( ) ,
99
97
scope,
@@ -105,7 +103,8 @@ export class Hub implements HubInterface {
105
103
* @inheritDoc
106
104
*/
107
105
public popScope ( ) : boolean {
108
- return this . getStack ( ) . pop ( ) !== undefined ;
106
+ if ( this . getStack ( ) . length <= 1 ) return false ;
107
+ return ! ! this . getStack ( ) . pop ( ) ;
109
108
}
110
109
111
110
/**
@@ -228,107 +227,83 @@ export class Hub implements HubInterface {
228
227
* @inheritDoc
229
228
*/
230
229
public addBreadcrumb ( breadcrumb : Breadcrumb , hint ?: BreadcrumbHint ) : void {
231
- const top = this . getStackTop ( ) ;
230
+ const { scope , client } = this . getStackTop ( ) ;
232
231
233
- if ( ! top . scope || ! top . client ) {
234
- return ;
235
- }
232
+ if ( ! scope || ! client ) return ;
236
233
237
234
// eslint-disable-next-line @typescript-eslint/unbound-method
238
235
const { beforeBreadcrumb = null , maxBreadcrumbs = DEFAULT_BREADCRUMBS } =
239
- ( top . client . getOptions && top . client . getOptions ( ) ) || { } ;
236
+ ( client . getOptions && client . getOptions ( ) ) || { } ;
240
237
241
- if ( maxBreadcrumbs <= 0 ) {
242
- return ;
243
- }
238
+ if ( maxBreadcrumbs <= 0 ) return ;
244
239
245
240
const timestamp = dateTimestampInSeconds ( ) ;
246
241
const mergedBreadcrumb = { timestamp, ...breadcrumb } ;
247
242
const finalBreadcrumb = beforeBreadcrumb
248
243
? ( consoleSandbox ( ( ) => beforeBreadcrumb ( mergedBreadcrumb , hint ) ) as Breadcrumb | null )
249
244
: mergedBreadcrumb ;
250
245
251
- if ( finalBreadcrumb === null ) {
252
- return ;
253
- }
246
+ if ( finalBreadcrumb === null ) return ;
254
247
255
- top . scope . addBreadcrumb ( finalBreadcrumb , Math . min ( maxBreadcrumbs , MAX_BREADCRUMBS ) ) ;
248
+ scope . addBreadcrumb ( finalBreadcrumb , Math . min ( maxBreadcrumbs , MAX_BREADCRUMBS ) ) ;
256
249
}
257
250
258
251
/**
259
252
* @inheritDoc
260
253
*/
261
254
public setUser ( user : User | null ) : void {
262
- const top = this . getStackTop ( ) ;
263
- if ( ! top . scope ) {
264
- return ;
265
- }
266
- top . scope . setUser ( user ) ;
255
+ const scope = this . getScope ( ) ;
256
+ if ( scope ) scope . setUser ( user ) ;
267
257
}
268
258
269
259
/**
270
260
* @inheritDoc
271
261
*/
272
262
public setTags ( tags : { [ key : string ] : string } ) : void {
273
- const top = this . getStackTop ( ) ;
274
- if ( ! top . scope ) {
275
- return ;
276
- }
277
- top . scope . setTags ( tags ) ;
263
+ const scope = this . getScope ( ) ;
264
+ if ( scope ) scope . setTags ( tags ) ;
278
265
}
279
266
280
267
/**
281
268
* @inheritDoc
282
269
*/
283
270
public setExtras ( extras : Extras ) : void {
284
- const top = this . getStackTop ( ) ;
285
- if ( ! top . scope ) {
286
- return ;
287
- }
288
- top . scope . setExtras ( extras ) ;
271
+ const scope = this . getScope ( ) ;
272
+ if ( scope ) scope . setExtras ( extras ) ;
289
273
}
290
274
291
275
/**
292
276
* @inheritDoc
293
277
*/
294
278
public setTag ( key : string , value : string ) : void {
295
- const top = this . getStackTop ( ) ;
296
- if ( ! top . scope ) {
297
- return ;
298
- }
299
- top . scope . setTag ( key , value ) ;
279
+ const scope = this . getScope ( ) ;
280
+ if ( scope ) scope . setTag ( key , value ) ;
300
281
}
301
282
302
283
/**
303
284
* @inheritDoc
304
285
*/
305
286
public setExtra ( key : string , extra : Extra ) : void {
306
- const top = this . getStackTop ( ) ;
307
- if ( ! top . scope ) {
308
- return ;
309
- }
310
- top . scope . setExtra ( key , extra ) ;
287
+ const scope = this . getScope ( ) ;
288
+ if ( scope ) scope . setExtra ( key , extra ) ;
311
289
}
312
290
313
291
/**
314
292
* @inheritDoc
315
293
*/
316
294
// eslint-disable-next-line @typescript-eslint/no-explicit-any
317
295
public setContext ( name : string , context : { [ key : string ] : any } | null ) : void {
318
- const top = this . getStackTop ( ) ;
319
- if ( ! top . scope ) {
320
- return ;
321
- }
322
- top . scope . setContext ( name , context ) ;
296
+ const scope = this . getScope ( ) ;
297
+ if ( scope ) scope . setContext ( name , context ) ;
323
298
}
324
299
325
300
/**
326
301
* @inheritDoc
327
302
*/
328
303
public configureScope ( callback : ( scope : Scope ) => void ) : void {
329
- const top = this . getStackTop ( ) ;
330
- if ( top . scope && top . client ) {
331
- callback ( top . scope ) ;
304
+ const { scope , client } = this . getStackTop ( ) ;
305
+ if ( scope && client ) {
306
+ callback ( scope ) ;
332
307
}
333
308
}
334
309
@@ -349,9 +324,7 @@ export class Hub implements HubInterface {
349
324
*/
350
325
public getIntegration < T extends Integration > ( integration : IntegrationClass < T > ) : T | null {
351
326
const client = this . getClient ( ) ;
352
- if ( ! client ) {
353
- return null ;
354
- }
327
+ if ( ! client ) return null ;
355
328
try {
356
329
return client . getIntegration ( integration ) ;
357
330
} catch ( _oO ) {
@@ -389,10 +362,10 @@ export class Hub implements HubInterface {
389
362
*/
390
363
// eslint-disable-next-line @typescript-eslint/no-explicit-any
391
364
private _invokeClient < M extends keyof Client > ( method : M , ...args : any [ ] ) : void {
392
- const top = this . getStackTop ( ) ;
393
- if ( top && top . client && top . client [ method ] ) {
365
+ const { scope , client } = this . getStackTop ( ) ;
366
+ if ( client && client [ method ] ) {
394
367
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
395
- ( top . client as any ) [ method ] ( ...args , top . scope ) ;
368
+ ( client as any ) [ method ] ( ...args , scope ) ;
396
369
}
397
370
}
398
371
@@ -500,10 +473,7 @@ function getHubFromActiveDomain(registry: Carrier): Hub {
500
473
* @param carrier object
501
474
*/
502
475
function hasHubOnCarrier ( carrier : Carrier ) : boolean {
503
- if ( carrier && carrier . __SENTRY__ && carrier . __SENTRY__ . hub ) {
504
- return true ;
505
- }
506
- return false ;
476
+ return ! ! ( carrier && carrier . __SENTRY__ && carrier . __SENTRY__ . hub ) ;
507
477
}
508
478
509
479
/**
@@ -513,9 +483,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean {
513
483
* @hidden
514
484
*/
515
485
export function getHubFromCarrier ( carrier : Carrier ) : Hub {
516
- if ( carrier && carrier . __SENTRY__ && carrier . __SENTRY__ . hub ) {
517
- return carrier . __SENTRY__ . hub ;
518
- }
486
+ if ( carrier && carrier . __SENTRY__ && carrier . __SENTRY__ . hub ) return carrier . __SENTRY__ . hub ;
519
487
carrier . __SENTRY__ = carrier . __SENTRY__ || { } ;
520
488
carrier . __SENTRY__ . hub = new Hub ( ) ;
521
489
return carrier . __SENTRY__ . hub ;
@@ -527,9 +495,7 @@ export function getHubFromCarrier(carrier: Carrier): Hub {
527
495
* @param hub Hub
528
496
*/
529
497
export function setHubOnCarrier ( carrier : Carrier , hub : Hub ) : boolean {
530
- if ( ! carrier ) {
531
- return false ;
532
- }
498
+ if ( ! carrier ) return false ;
533
499
carrier . __SENTRY__ = carrier . __SENTRY__ || { } ;
534
500
carrier . __SENTRY__ . hub = hub ;
535
501
return true ;
0 commit comments