Skip to content

Commit dd8e392

Browse files
authored
ref: Doesnt allow for popping last layer and unify getter methods (#2955)
1 parent 8f66ed8 commit dd8e392

File tree

1 file changed

+33
-67
lines changed

1 file changed

+33
-67
lines changed

packages/hub/src/hub.ts

Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const MAX_BREADCRUMBS = 100;
5050
*/
5151
export class Hub implements HubInterface {
5252
/** Is a {@link Layer}[] containing the client and scope */
53-
private readonly _stack: Layer[] = [];
53+
private readonly _stack: Layer[] = [{}];
5454

5555
/** Contains the last event id of a captured event. */
5656
private _lastEventId?: string;
@@ -64,7 +64,7 @@ export class Hub implements HubInterface {
6464
* @param version number, higher number means higher priority.
6565
*/
6666
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;
6868
this.bindClient(client);
6969
}
7070

@@ -91,9 +91,7 @@ export class Hub implements HubInterface {
9191
*/
9292
public pushScope(): Scope {
9393
// 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());
9795
this.getStack().push({
9896
client: this.getClient(),
9997
scope,
@@ -105,7 +103,8 @@ export class Hub implements HubInterface {
105103
* @inheritDoc
106104
*/
107105
public popScope(): boolean {
108-
return this.getStack().pop() !== undefined;
106+
if (this.getStack().length <= 1) return false;
107+
return !!this.getStack().pop();
109108
}
110109

111110
/**
@@ -228,107 +227,83 @@ export class Hub implements HubInterface {
228227
* @inheritDoc
229228
*/
230229
public addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void {
231-
const top = this.getStackTop();
230+
const { scope, client } = this.getStackTop();
232231

233-
if (!top.scope || !top.client) {
234-
return;
235-
}
232+
if (!scope || !client) return;
236233

237234
// eslint-disable-next-line @typescript-eslint/unbound-method
238235
const { beforeBreadcrumb = null, maxBreadcrumbs = DEFAULT_BREADCRUMBS } =
239-
(top.client.getOptions && top.client.getOptions()) || {};
236+
(client.getOptions && client.getOptions()) || {};
240237

241-
if (maxBreadcrumbs <= 0) {
242-
return;
243-
}
238+
if (maxBreadcrumbs <= 0) return;
244239

245240
const timestamp = dateTimestampInSeconds();
246241
const mergedBreadcrumb = { timestamp, ...breadcrumb };
247242
const finalBreadcrumb = beforeBreadcrumb
248243
? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) as Breadcrumb | null)
249244
: mergedBreadcrumb;
250245

251-
if (finalBreadcrumb === null) {
252-
return;
253-
}
246+
if (finalBreadcrumb === null) return;
254247

255-
top.scope.addBreadcrumb(finalBreadcrumb, Math.min(maxBreadcrumbs, MAX_BREADCRUMBS));
248+
scope.addBreadcrumb(finalBreadcrumb, Math.min(maxBreadcrumbs, MAX_BREADCRUMBS));
256249
}
257250

258251
/**
259252
* @inheritDoc
260253
*/
261254
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);
267257
}
268258

269259
/**
270260
* @inheritDoc
271261
*/
272262
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);
278265
}
279266

280267
/**
281268
* @inheritDoc
282269
*/
283270
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);
289273
}
290274

291275
/**
292276
* @inheritDoc
293277
*/
294278
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);
300281
}
301282

302283
/**
303284
* @inheritDoc
304285
*/
305286
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);
311289
}
312290

313291
/**
314292
* @inheritDoc
315293
*/
316294
// eslint-disable-next-line @typescript-eslint/no-explicit-any
317295
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);
323298
}
324299

325300
/**
326301
* @inheritDoc
327302
*/
328303
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);
332307
}
333308
}
334309

@@ -349,9 +324,7 @@ export class Hub implements HubInterface {
349324
*/
350325
public getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null {
351326
const client = this.getClient();
352-
if (!client) {
353-
return null;
354-
}
327+
if (!client) return null;
355328
try {
356329
return client.getIntegration(integration);
357330
} catch (_oO) {
@@ -389,10 +362,10 @@ export class Hub implements HubInterface {
389362
*/
390363
// eslint-disable-next-line @typescript-eslint/no-explicit-any
391364
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]) {
394367
// 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);
396369
}
397370
}
398371

@@ -500,10 +473,7 @@ function getHubFromActiveDomain(registry: Carrier): Hub {
500473
* @param carrier object
501474
*/
502475
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);
507477
}
508478

509479
/**
@@ -513,9 +483,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean {
513483
* @hidden
514484
*/
515485
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;
519487
carrier.__SENTRY__ = carrier.__SENTRY__ || {};
520488
carrier.__SENTRY__.hub = new Hub();
521489
return carrier.__SENTRY__.hub;
@@ -527,9 +495,7 @@ export function getHubFromCarrier(carrier: Carrier): Hub {
527495
* @param hub Hub
528496
*/
529497
export function setHubOnCarrier(carrier: Carrier, hub: Hub): boolean {
530-
if (!carrier) {
531-
return false;
532-
}
498+
if (!carrier) return false;
533499
carrier.__SENTRY__ = carrier.__SENTRY__ || {};
534500
carrier.__SENTRY__.hub = hub;
535501
return true;

0 commit comments

Comments
 (0)