Skip to content

feat(hub): Make scope always defined on the hub #7551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const DEFAULT_BREADCRUMBS = 100;
*/
export interface Layer {
client?: Client;
scope?: Scope;
scope: Scope;
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ export interface Carrier {
*/
export class Hub implements HubInterface {
/** Is a {@link Layer}[] containing the client and scope */
private readonly _stack: Layer[] = [{}];
private readonly _stack: Layer[];

/** Contains the last event id of a captured event. */
private _lastEventId?: string;
Expand All @@ -101,7 +101,7 @@ export class Hub implements HubInterface {
* @param version number, higher number means higher priority.
*/
public constructor(client?: Client, scope: Scope = new Scope(), private readonly _version: number = API_VERSION) {
this.getStackTop().scope = scope;
this._stack = [{ scope }];
if (client) {
this.bindClient(client);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ export class Hub implements HubInterface {
}

/** Returns the scope of the top stack. */
public getScope(): Scope | undefined {
public getScope(): Scope {
return this.getStackTop().scope;
}

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

if (!scope || !client) return;
if (!client) return;

const { beforeBreadcrumb = null, maxBreadcrumbs = DEFAULT_BREADCRUMBS } =
(client.getOptions && client.getOptions()) || {};
Expand All @@ -282,49 +282,43 @@ export class Hub implements HubInterface {
* @inheritDoc
*/
public setUser(user: User | null): void {
const scope = this.getScope();
if (scope) scope.setUser(user);
this.getScope().setUser(user);
}

/**
* @inheritDoc
*/
public setTags(tags: { [key: string]: Primitive }): void {
const scope = this.getScope();
if (scope) scope.setTags(tags);
this.getScope().setTags(tags);
}

/**
* @inheritDoc
*/
public setExtras(extras: Extras): void {
const scope = this.getScope();
if (scope) scope.setExtras(extras);
this.getScope().setExtras(extras);
}

/**
* @inheritDoc
*/
public setTag(key: string, value: Primitive): void {
const scope = this.getScope();
if (scope) scope.setTag(key, value);
this.getScope().setTag(key, value);
}

/**
* @inheritDoc
*/
public setExtra(key: string, extra: Extra): void {
const scope = this.getScope();
if (scope) scope.setExtra(key, extra);
this.getScope().setExtra(key, extra);
}

/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setContext(name: string, context: { [key: string]: any } | null): void {
const scope = this.getScope();
if (scope) scope.setContext(name, context);
this.getScope().setContext(name, context);
}

/**
Expand Down Expand Up @@ -395,17 +389,15 @@ export class Hub implements HubInterface {
*/
public endSession(): void {
const layer = this.getStackTop();
const scope = layer && layer.scope;
const session = scope && scope.getSession();
const scope = layer.scope;
const session = scope.getSession();
if (session) {
closeSession(session);
}
this._sendSessionUpdate();

// the session is over; take it off of the scope
if (scope) {
scope.setSession();
}
scope.setSession();
}

/**
Expand All @@ -426,17 +418,15 @@ export class Hub implements HubInterface {
...context,
});

if (scope) {
// End existing session if there's one
const currentSession = scope.getSession && scope.getSession();
if (currentSession && currentSession.status === 'ok') {
updateSession(currentSession, { status: 'exited' });
}
this.endSession();

// Afterwards we set the new session on the scope
scope.setSession(session);
// End existing session if there's one
const currentSession = scope.getSession && scope.getSession();
if (currentSession && currentSession.status === 'ok') {
updateSession(currentSession, { status: 'exited' });
}
this.endSession();

// Afterwards we set the new session on the scope
scope.setSession(session);

return session;
}
Expand Down Expand Up @@ -472,7 +462,7 @@ export class Hub implements HubInterface {
* @param method The method to call on the client.
* @param args Arguments to pass to the client function.
*/
private _withClient(callback: (client: Client, scope: Scope | undefined) => void): void {
private _withClient(callback: (client: Client, scope: Scope) => void): void {
const { scope, client } = this.getStackTop();
if (client) {
callback(client, scope);
Expand Down