Skip to content

Commit 6426b58

Browse files
authored
feat(hub): Make scope always defined on the hub (#7551)
1 parent a11283c commit 6426b58

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

packages/core/src/hub.ts

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const DEFAULT_BREADCRUMBS = 100;
5656
*/
5757
export interface Layer {
5858
client?: Client;
59-
scope?: Scope;
59+
scope: Scope;
6060
}
6161

6262
/**
@@ -87,7 +87,7 @@ export interface Carrier {
8787
*/
8888
export class Hub implements HubInterface {
8989
/** Is a {@link Layer}[] containing the client and scope */
90-
private readonly _stack: Layer[] = [{}];
90+
private readonly _stack: Layer[];
9191

9292
/** Contains the last event id of a captured event. */
9393
private _lastEventId?: string;
@@ -101,7 +101,7 @@ export class Hub implements HubInterface {
101101
* @param version number, higher number means higher priority.
102102
*/
103103
public constructor(client?: Client, scope: Scope = new Scope(), private readonly _version: number = API_VERSION) {
104-
this.getStackTop().scope = scope;
104+
this._stack = [{ scope }];
105105
if (client) {
106106
this.bindClient(client);
107107
}
@@ -166,7 +166,7 @@ export class Hub implements HubInterface {
166166
}
167167

168168
/** Returns the scope of the top stack. */
169-
public getScope(): Scope | undefined {
169+
public getScope(): Scope {
170170
return this.getStackTop().scope;
171171
}
172172

@@ -256,7 +256,7 @@ export class Hub implements HubInterface {
256256
public addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void {
257257
const { scope, client } = this.getStackTop();
258258

259-
if (!scope || !client) return;
259+
if (!client) return;
260260

261261
const { beforeBreadcrumb = null, maxBreadcrumbs = DEFAULT_BREADCRUMBS } =
262262
(client.getOptions && client.getOptions()) || {};
@@ -282,49 +282,43 @@ export class Hub implements HubInterface {
282282
* @inheritDoc
283283
*/
284284
public setUser(user: User | null): void {
285-
const scope = this.getScope();
286-
if (scope) scope.setUser(user);
285+
this.getScope().setUser(user);
287286
}
288287

289288
/**
290289
* @inheritDoc
291290
*/
292291
public setTags(tags: { [key: string]: Primitive }): void {
293-
const scope = this.getScope();
294-
if (scope) scope.setTags(tags);
292+
this.getScope().setTags(tags);
295293
}
296294

297295
/**
298296
* @inheritDoc
299297
*/
300298
public setExtras(extras: Extras): void {
301-
const scope = this.getScope();
302-
if (scope) scope.setExtras(extras);
299+
this.getScope().setExtras(extras);
303300
}
304301

305302
/**
306303
* @inheritDoc
307304
*/
308305
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);
311307
}
312308

313309
/**
314310
* @inheritDoc
315311
*/
316312
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);
319314
}
320315

321316
/**
322317
* @inheritDoc
323318
*/
324319
// eslint-disable-next-line @typescript-eslint/no-explicit-any
325320
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);
328322
}
329323

330324
/**
@@ -395,17 +389,15 @@ export class Hub implements HubInterface {
395389
*/
396390
public endSession(): void {
397391
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();
400394
if (session) {
401395
closeSession(session);
402396
}
403397
this._sendSessionUpdate();
404398

405399
// the session is over; take it off of the scope
406-
if (scope) {
407-
scope.setSession();
408-
}
400+
scope.setSession();
409401
}
410402

411403
/**
@@ -426,17 +418,15 @@ export class Hub implements HubInterface {
426418
...context,
427419
});
428420

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' });
439425
}
426+
this.endSession();
427+
428+
// Afterwards we set the new session on the scope
429+
scope.setSession(session);
440430

441431
return session;
442432
}
@@ -472,7 +462,7 @@ export class Hub implements HubInterface {
472462
* @param method The method to call on the client.
473463
* @param args Arguments to pass to the client function.
474464
*/
475-
private _withClient(callback: (client: Client, scope: Scope | undefined) => void): void {
465+
private _withClient(callback: (client: Client, scope: Scope) => void): void {
476466
const { scope, client } = this.getStackTop();
477467
if (client) {
478468
callback(client, scope);

0 commit comments

Comments
 (0)