Skip to content

Commit d7a49d7

Browse files
committed
feat: move functions out of the class
1 parent f553fef commit d7a49d7

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

packages/hub/src/hub.ts

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,35 @@ export interface DomainAsCarrier extends Carrier {
8282
members: { [key: string]: any }[];
8383
}
8484

85+
86+
/**
87+
* Returns the topmost scope layer in the order domain > local > process.
88+
*
89+
* @hidden
90+
* */
91+
export function getStackTop(hub: Hub): Layer {
92+
return hub._stack[hub._stack.length - 1];
93+
}
94+
95+
/**
96+
* Checks if this hub's version is older than the given version.
97+
*
98+
* @param hub The hub to check the version on.
99+
* @param version A version number to compare to.
100+
* @return True if the given version is newer; otherwise false.
101+
*
102+
* @hidden
103+
*/
104+
export function isOlderThan(hub: Hub, version: number): boolean {
105+
return hub._version < version;
106+
}
107+
85108
/**
86109
* @inheritDoc
87110
*/
88111
export class Hub implements HubInterface {
89112
/** Is a {@link Layer}[] containing the client and scope */
90-
private readonly _stack: Layer[] = [{}];
113+
public readonly _stack: Layer[] = [{}];
91114

92115
/** Contains the last event id of a captured event. */
93116
private _lastEventId?: string;
@@ -100,25 +123,18 @@ export class Hub implements HubInterface {
100123
* @param scope bound to the hub.
101124
* @param version number, higher number means higher priority.
102125
*/
103-
public constructor(client?: Client, scope: Scope = new Scope(), private readonly _version: number = API_VERSION) {
104-
this.getStackTop().scope = scope;
126+
public constructor(client?: Client, scope: Scope = new Scope(), public readonly _version: number = API_VERSION) {
127+
getStackTop(this).scope = scope;
105128
if (client) {
106129
this.bindClient(client);
107130
}
108131
}
109132

110-
/**
111-
* @inheritDoc
112-
*/
113-
public isOlderThan(version: number): boolean {
114-
return this._version < version;
115-
}
116-
117133
/**
118134
* @inheritDoc
119135
*/
120136
public bindClient(client?: Client): void {
121-
const top = this.getStackTop();
137+
const top = getStackTop(this);
122138
top.client = client;
123139
if (client && client.setupIntegrations) {
124140
client.setupIntegrations();
@@ -162,23 +178,19 @@ export class Hub implements HubInterface {
162178
* @inheritDoc
163179
*/
164180
public getClient<C extends Client>(): C | undefined {
165-
return this.getStackTop().client as C;
181+
return getStackTop(this).client as C;
166182
}
167183

168184
/** Returns the scope of the top stack. */
169185
public getScope(): Scope | undefined {
170-
return this.getStackTop().scope;
186+
return getStackTop(this).scope;
171187
}
172188

173189
/** Returns the scope stack for domains or the process. */
174190
public getStack(): Layer[] {
175191
return this._stack;
176192
}
177193

178-
/** Returns the topmost scope layer in the order domain > local > process. */
179-
public getStackTop(): Layer {
180-
return this._stack[this._stack.length - 1];
181-
}
182194

183195
/**
184196
* @inheritDoc
@@ -270,7 +282,7 @@ export class Hub implements HubInterface {
270282
* @inheritDoc
271283
*/
272284
public addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void {
273-
const { scope, client } = this.getStackTop();
285+
const { scope, client } = getStackTop(this);
274286

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

@@ -344,7 +356,7 @@ export class Hub implements HubInterface {
344356
* @inheritDoc
345357
*/
346358
public configureScope(callback: (scope: Scope) => void): void {
347-
const { scope, client } = this.getStackTop();
359+
const { scope, client } = getStackTop(this);
348360
if (scope && client) {
349361
callback(scope);
350362
}
@@ -414,7 +426,7 @@ export class Hub implements HubInterface {
414426
* @inheritDoc
415427
*/
416428
public endSession(): void {
417-
const layer = this.getStackTop();
429+
const layer = getStackTop(this);
418430
const scope = layer && layer.scope;
419431
const session = scope && scope.getSession();
420432
if (session) {
@@ -432,7 +444,7 @@ export class Hub implements HubInterface {
432444
* @inheritDoc
433445
*/
434446
public startSession(context?: SessionContext): Session {
435-
const { scope, client } = this.getStackTop();
447+
const { scope, client } = getStackTop(this);
436448
const { release, environment } = (client && client.getOptions()) || {};
437449

438450
// Will fetch userAgent if called from browser sdk
@@ -466,7 +478,7 @@ export class Hub implements HubInterface {
466478
* Sends the current Session on the scope
467479
*/
468480
private _sendSessionUpdate(): void {
469-
const { scope, client } = this.getStackTop();
481+
const { scope, client } = getStackTop(this);
470482
if (!scope) return;
471483

472484
const session = scope.getSession && scope.getSession();
@@ -485,7 +497,7 @@ export class Hub implements HubInterface {
485497
*/
486498
// eslint-disable-next-line @typescript-eslint/no-explicit-any
487499
private _invokeClient<M extends keyof Client>(method: M, ...args: any[]): void {
488-
const { scope, client } = this.getStackTop();
500+
const { scope, client } = getStackTop(this);
489501
if (client && client[method]) {
490502
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
491503
(client as any)[method](...args, scope);
@@ -547,7 +559,7 @@ export function getCurrentHub(): Hub {
547559
const registry = getMainCarrier();
548560

549561
// If there's no hub, or its an old API, assign a new one
550-
if (!hasHubOnCarrier(registry) || getHubFromCarrier(registry).isOlderThan(API_VERSION)) {
562+
if (!hasHubOnCarrier(registry) || isOlderThan(getHubFromCarrier(registry), API_VERSION)) {
551563
setHubOnCarrier(registry, new Hub());
552564
}
553565

@@ -588,8 +600,8 @@ function getHubFromActiveDomain(registry: Carrier): Hub {
588600
}
589601

590602
// If there's no hub on current domain, or it's an old API, assign a new one
591-
if (!hasHubOnCarrier(activeDomain) || getHubFromCarrier(activeDomain).isOlderThan(API_VERSION)) {
592-
const registryHubTopStack = getHubFromCarrier(registry).getStackTop();
603+
if (!hasHubOnCarrier(activeDomain) || isOlderThan(getHubFromCarrier(activeDomain), API_VERSION)) {
604+
const registryHubTopStack = getStackTop(getHubFromCarrier(registry));
593605
setHubOnCarrier(activeDomain, new Hub(registryHubTopStack.client, Scope.clone(registryHubTopStack.scope)));
594606
}
595607

packages/hub/test/hub.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Event } from '@sentry/types';
22

33
import { getCurrentHub, Hub, Scope } from '../src';
4+
import { getStackTop, isOlderThan } from '../src/hub';
45

56
const clientFn: any = jest.fn();
67

@@ -36,7 +37,7 @@ describe('Hub', () => {
3637

3738
test('isOlderThan', () => {
3839
const hub = new Hub();
39-
expect(hub.isOlderThan(0)).toBeFalsy();
40+
expect(isOlderThan(hub, 0)).toBeFalsy();
4041
});
4142

4243
describe('pushScope', () => {
@@ -104,7 +105,7 @@ describe('Hub', () => {
104105
});
105106

106107
hub.pushScope();
107-
const pushedScope = hub.getStackTop().scope;
108+
const pushedScope = getStackTop(hub).scope;
108109

109110
return pushedScope!.applyToEvent(event).then(final => {
110111
expect(final!.dist).toEqual('1');
@@ -172,7 +173,7 @@ describe('Hub', () => {
172173
hub.pushScope();
173174
hub.pushScope();
174175
hub.bindClient(testClient);
175-
expect(hub.getStackTop().client).toEqual({ bla: 'a' });
176+
expect(getStackTop(hub).client).toEqual({ bla: 'a' });
176177
});
177178

178179
describe('configureScope', () => {

packages/types/src/hub.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ import { User } from './user';
1616
* working in case we have a version conflict.
1717
*/
1818
export interface Hub {
19-
/**
20-
* Checks if this hub's version is older than the given version.
21-
*
22-
* @param version A version number to compare to.
23-
* @return True if the given version is newer; otherwise false.
24-
*
25-
* @hidden
26-
*/
27-
isOlderThan(version: number): boolean;
28-
2919
/**
3020
* This binds the given client to the current scope.
3121
* @param client An SDK client (client) instance.

0 commit comments

Comments
 (0)