-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref: Doesnt allow for popping last layer and unify getter methods #2955
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ const MAX_BREADCRUMBS = 100; | |
*/ | ||
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; | ||
|
@@ -64,7 +64,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._stack.push({ client, scope }); | ||
this.getStackTop().scope = scope; | ||
this.bindClient(client); | ||
} | ||
|
||
|
@@ -91,9 +91,7 @@ export class Hub implements HubInterface { | |
*/ | ||
public pushScope(): Scope { | ||
// We want to clone the content of prev scope | ||
const stack = this.getStack(); | ||
const parentScope = stack.length > 0 ? stack[stack.length - 1].scope : undefined; | ||
const scope = Scope.clone(parentScope); | ||
const scope = Scope.clone(this.getScope()); | ||
this.getStack().push({ | ||
client: this.getClient(), | ||
scope, | ||
|
@@ -105,7 +103,8 @@ export class Hub implements HubInterface { | |
* @inheritDoc | ||
*/ | ||
public popScope(): boolean { | ||
return this.getStack().pop() !== undefined; | ||
if (this.getStack().length <= 1) return false; | ||
return !!this.getStack().pop(); | ||
} | ||
|
||
/** | ||
|
@@ -228,107 +227,83 @@ export class Hub implements HubInterface { | |
* @inheritDoc | ||
*/ | ||
public addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void { | ||
const top = this.getStackTop(); | ||
const { scope, client } = this.getStackTop(); | ||
|
||
if (!top.scope || !top.client) { | ||
return; | ||
} | ||
if (!scope || !client) return; | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const { beforeBreadcrumb = null, maxBreadcrumbs = DEFAULT_BREADCRUMBS } = | ||
(top.client.getOptions && top.client.getOptions()) || {}; | ||
(client.getOptions && client.getOptions()) || {}; | ||
|
||
if (maxBreadcrumbs <= 0) { | ||
return; | ||
} | ||
if (maxBreadcrumbs <= 0) return; | ||
|
||
const timestamp = dateTimestampInSeconds(); | ||
const mergedBreadcrumb = { timestamp, ...breadcrumb }; | ||
const finalBreadcrumb = beforeBreadcrumb | ||
? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) as Breadcrumb | null) | ||
: mergedBreadcrumb; | ||
|
||
if (finalBreadcrumb === null) { | ||
return; | ||
} | ||
if (finalBreadcrumb === null) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
top.scope.addBreadcrumb(finalBreadcrumb, Math.min(maxBreadcrumbs, MAX_BREADCRUMBS)); | ||
scope.addBreadcrumb(finalBreadcrumb, Math.min(maxBreadcrumbs, MAX_BREADCRUMBS)); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setUser(user: User | null): void { | ||
const top = this.getStackTop(); | ||
if (!top.scope) { | ||
return; | ||
} | ||
top.scope.setUser(user); | ||
const scope = this.getScope(); | ||
if (scope) scope.setUser(user); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setTags(tags: { [key: string]: string }): void { | ||
const top = this.getStackTop(); | ||
if (!top.scope) { | ||
return; | ||
} | ||
top.scope.setTags(tags); | ||
const scope = this.getScope(); | ||
if (scope) scope.setTags(tags); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setExtras(extras: Extras): void { | ||
const top = this.getStackTop(); | ||
if (!top.scope) { | ||
return; | ||
} | ||
top.scope.setExtras(extras); | ||
const scope = this.getScope(); | ||
if (scope) scope.setExtras(extras); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setTag(key: string, value: string): void { | ||
const top = this.getStackTop(); | ||
if (!top.scope) { | ||
return; | ||
} | ||
top.scope.setTag(key, value); | ||
const scope = this.getScope(); | ||
if (scope) scope.setTag(key, value); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setExtra(key: string, extra: Extra): void { | ||
const top = this.getStackTop(); | ||
if (!top.scope) { | ||
return; | ||
} | ||
top.scope.setExtra(key, extra); | ||
const scope = this.getScope(); | ||
if (scope) scope.setExtra(key, extra); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
public setContext(name: string, context: { [key: string]: any } | null): void { | ||
const top = this.getStackTop(); | ||
if (!top.scope) { | ||
return; | ||
} | ||
top.scope.setContext(name, context); | ||
const scope = this.getScope(); | ||
if (scope) scope.setContext(name, context); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public configureScope(callback: (scope: Scope) => void): void { | ||
const top = this.getStackTop(); | ||
if (top.scope && top.client) { | ||
callback(top.scope); | ||
const { scope, client } = this.getStackTop(); | ||
if (scope && client) { | ||
callback(scope); | ||
} | ||
} | ||
|
||
|
@@ -349,9 +324,7 @@ export class Hub implements HubInterface { | |
*/ | ||
public getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null { | ||
const client = this.getClient(); | ||
if (!client) { | ||
return null; | ||
} | ||
if (!client) return null; | ||
try { | ||
return client.getIntegration(integration); | ||
} catch (_oO) { | ||
|
@@ -389,10 +362,10 @@ export class Hub implements HubInterface { | |
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private _invokeClient<M extends keyof Client>(method: M, ...args: any[]): void { | ||
const top = this.getStackTop(); | ||
if (top && top.client && top.client[method]) { | ||
const { scope, client } = this.getStackTop(); | ||
if (client && client[method]) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any | ||
(top.client as any)[method](...args, top.scope); | ||
(client as any)[method](...args, scope); | ||
} | ||
} | ||
|
||
|
@@ -500,10 +473,7 @@ function getHubFromActiveDomain(registry: Carrier): Hub { | |
* @param carrier object | ||
*/ | ||
function hasHubOnCarrier(carrier: Carrier): boolean { | ||
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) { | ||
return true; | ||
} | ||
return false; | ||
return !!(carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub); | ||
} | ||
|
||
/** | ||
|
@@ -513,9 +483,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean { | |
* @hidden | ||
*/ | ||
export function getHubFromCarrier(carrier: Carrier): Hub { | ||
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) { | ||
return carrier.__SENTRY__.hub; | ||
} | ||
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) return carrier.__SENTRY__.hub; | ||
carrier.__SENTRY__ = carrier.__SENTRY__ || {}; | ||
carrier.__SENTRY__.hub = new Hub(); | ||
return carrier.__SENTRY__.hub; | ||
|
@@ -527,9 +495,7 @@ export function getHubFromCarrier(carrier: Carrier): Hub { | |
* @param hub Hub | ||
*/ | ||
export function setHubOnCarrier(carrier: Carrier, hub: Hub): boolean { | ||
if (!carrier) { | ||
return false; | ||
} | ||
if (!carrier) return false; | ||
carrier.__SENTRY__ = carrier.__SENTRY__ || {}; | ||
carrier.__SENTRY__.hub = hub; | ||
return true; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be accessing
this._stack
directly, same inpushScope
.Why is there even a public
getStack()
?! Is there a use case to deal with stack layers from code external to this class?!