@@ -56,7 +56,7 @@ const DEFAULT_BREADCRUMBS = 100;
56
56
*/
57
57
export interface Layer {
58
58
client ?: Client ;
59
- scope ? : Scope ;
59
+ scope : Scope ;
60
60
}
61
61
62
62
/**
@@ -87,7 +87,7 @@ export interface Carrier {
87
87
*/
88
88
export class Hub implements HubInterface {
89
89
/** Is a {@link Layer}[] containing the client and scope */
90
- private readonly _stack : Layer [ ] = [ { } ] ;
90
+ private readonly _stack : Layer [ ] ;
91
91
92
92
/** Contains the last event id of a captured event. */
93
93
private _lastEventId ?: string ;
@@ -101,7 +101,7 @@ export class Hub implements HubInterface {
101
101
* @param version number, higher number means higher priority.
102
102
*/
103
103
public constructor ( client ?: Client , scope : Scope = new Scope ( ) , private readonly _version : number = API_VERSION ) {
104
- this . getStackTop ( ) . scope = scope ;
104
+ this . _stack = [ { scope } ] ;
105
105
if ( client ) {
106
106
this . bindClient ( client ) ;
107
107
}
@@ -166,7 +166,7 @@ export class Hub implements HubInterface {
166
166
}
167
167
168
168
/** Returns the scope of the top stack. */
169
- public getScope ( ) : Scope | undefined {
169
+ public getScope ( ) : Scope {
170
170
return this . getStackTop ( ) . scope ;
171
171
}
172
172
@@ -256,7 +256,7 @@ export class Hub implements HubInterface {
256
256
public addBreadcrumb ( breadcrumb : Breadcrumb , hint ?: BreadcrumbHint ) : void {
257
257
const { scope, client } = this . getStackTop ( ) ;
258
258
259
- if ( ! scope || ! client ) return ;
259
+ if ( ! client ) return ;
260
260
261
261
const { beforeBreadcrumb = null , maxBreadcrumbs = DEFAULT_BREADCRUMBS } =
262
262
( client . getOptions && client . getOptions ( ) ) || { } ;
@@ -282,49 +282,43 @@ export class Hub implements HubInterface {
282
282
* @inheritDoc
283
283
*/
284
284
public setUser ( user : User | null ) : void {
285
- const scope = this . getScope ( ) ;
286
- if ( scope ) scope . setUser ( user ) ;
285
+ this . getScope ( ) . setUser ( user ) ;
287
286
}
288
287
289
288
/**
290
289
* @inheritDoc
291
290
*/
292
291
public setTags ( tags : { [ key : string ] : Primitive } ) : void {
293
- const scope = this . getScope ( ) ;
294
- if ( scope ) scope . setTags ( tags ) ;
292
+ this . getScope ( ) . setTags ( tags ) ;
295
293
}
296
294
297
295
/**
298
296
* @inheritDoc
299
297
*/
300
298
public setExtras ( extras : Extras ) : void {
301
- const scope = this . getScope ( ) ;
302
- if ( scope ) scope . setExtras ( extras ) ;
299
+ this . getScope ( ) . setExtras ( extras ) ;
303
300
}
304
301
305
302
/**
306
303
* @inheritDoc
307
304
*/
308
305
public setTag ( key : string , value : Primitive ) : void {
309
- const scope = this . getScope ( ) ;
310
- if ( scope ) scope . setTag ( key , value ) ;
306
+ this . getScope ( ) . setTag ( key , value ) ;
311
307
}
312
308
313
309
/**
314
310
* @inheritDoc
315
311
*/
316
312
public setExtra ( key : string , extra : Extra ) : void {
317
- const scope = this . getScope ( ) ;
318
- if ( scope ) scope . setExtra ( key , extra ) ;
313
+ this . getScope ( ) . setExtra ( key , extra ) ;
319
314
}
320
315
321
316
/**
322
317
* @inheritDoc
323
318
*/
324
319
// eslint-disable-next-line @typescript-eslint/no-explicit-any
325
320
public setContext ( name : string , context : { [ key : string ] : any } | null ) : void {
326
- const scope = this . getScope ( ) ;
327
- if ( scope ) scope . setContext ( name , context ) ;
321
+ this . getScope ( ) . setContext ( name , context ) ;
328
322
}
329
323
330
324
/**
@@ -395,17 +389,15 @@ export class Hub implements HubInterface {
395
389
*/
396
390
public endSession ( ) : void {
397
391
const layer = this . getStackTop ( ) ;
398
- const scope = layer && layer . scope ;
399
- const session = scope && scope . getSession ( ) ;
392
+ const scope = layer . scope ;
393
+ const session = scope . getSession ( ) ;
400
394
if ( session ) {
401
395
closeSession ( session ) ;
402
396
}
403
397
this . _sendSessionUpdate ( ) ;
404
398
405
399
// the session is over; take it off of the scope
406
- if ( scope ) {
407
- scope . setSession ( ) ;
408
- }
400
+ scope . setSession ( ) ;
409
401
}
410
402
411
403
/**
@@ -426,17 +418,15 @@ export class Hub implements HubInterface {
426
418
...context ,
427
419
} ) ;
428
420
429
- if ( scope ) {
430
- // End existing session if there's one
431
- const currentSession = scope . getSession && scope . getSession ( ) ;
432
- if ( currentSession && currentSession . status === 'ok' ) {
433
- updateSession ( currentSession , { status : 'exited' } ) ;
434
- }
435
- this . endSession ( ) ;
436
-
437
- // Afterwards we set the new session on the scope
438
- scope . setSession ( session ) ;
421
+ // End existing session if there's one
422
+ const currentSession = scope . getSession && scope . getSession ( ) ;
423
+ if ( currentSession && currentSession . status === 'ok' ) {
424
+ updateSession ( currentSession , { status : 'exited' } ) ;
439
425
}
426
+ this . endSession ( ) ;
427
+
428
+ // Afterwards we set the new session on the scope
429
+ scope . setSession ( session ) ;
440
430
441
431
return session ;
442
432
}
@@ -472,7 +462,7 @@ export class Hub implements HubInterface {
472
462
* @param method The method to call on the client.
473
463
* @param args Arguments to pass to the client function.
474
464
*/
475
- private _withClient ( callback : ( client : Client , scope : Scope | undefined ) => void ) : void {
465
+ private _withClient ( callback : ( client : Client , scope : Scope ) => void ) : void {
476
466
const { scope, client } = this . getStackTop ( ) ;
477
467
if ( client ) {
478
468
callback ( client , scope ) ;
0 commit comments