Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 12 additions & 7 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ import {
TransactionContext,
User,
} from '@sentry/types';
import { consoleSandbox, dateTimestampInSeconds, getGlobalObject, isNodeEnv, logger, uuid4 } from '@sentry/utils';
import {
consoleSandbox,
dateTimestampInSeconds,
getGlobalObject,
getGlobalSingleton,
isNodeEnv,
logger,
uuid4,
} from '@sentry/utils';

import { IS_DEBUG_BUILD } from './flags';
import { Scope } from './scope';
Expand Down Expand Up @@ -617,10 +625,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean {
* @hidden
*/
export function getHubFromCarrier(carrier: Carrier): 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;
return getGlobalSingleton<Hub>('hub', () => new Hub(), carrier);
}

/**
Expand All @@ -631,7 +636,7 @@ export function getHubFromCarrier(carrier: Carrier): Hub {
*/
export function setHubOnCarrier(carrier: Carrier, hub: Hub): boolean {
if (!carrier) return false;
carrier.__SENTRY__ = carrier.__SENTRY__ || {};
carrier.__SENTRY__.hub = hub;
const sentry = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const sentry = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});
const sentry = carrier.__SENTRY__ = carrier.__SENTRY__ || {};

Will prettier let you do this without ()? If so, I think it's easier to read without them.

Also, what do you think of calling this variable __SENTRY__, since that's what it is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switched to variable __SENTRY__, can't make the () change because of prettier.

sentry.hub = hub;
return true;
}
9 changes: 2 additions & 7 deletions packages/hub/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Transaction,
User,
} from '@sentry/types';
import { dateTimestampInSeconds, getGlobalObject, isPlainObject, isThenable, SyncPromise } from '@sentry/utils';
import { dateTimestampInSeconds, getGlobalSingleton, isPlainObject, isThenable, SyncPromise } from '@sentry/utils';

import { Session } from './session';

Expand Down Expand Up @@ -522,12 +522,7 @@ export class Scope implements ScopeInterface {
* Returns the global event processors.
*/
function getGlobalEventProcessors(): EventProcessor[] {
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */
const global = getGlobalObject<any>();
global.__SENTRY__ = global.__SENTRY__ || {};
global.__SENTRY__.globalEventProcessors = global.__SENTRY__.globalEventProcessors || [];
return global.__SENTRY__.globalEventProcessors;
/* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */
return getGlobalSingleton<EventProcessor[]>('globalEventProcessors', () => []);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ export function getGlobalObject<T>(): T & SentryGlobal {
: fallbackGlobalObject
) as T & SentryGlobal;
}

/**
* Returns a global singleton contained on the global `__SENTRY__` object.
*
* @param name name of the global singleton on __SENTRY__
* @param creator creation function
* @returns the singleton
*/
export function getGlobalSingleton<T>(name: keyof SentryGlobal['__SENTRY__'], creator: () => T, obj?: unknown): T {
const global = (obj || getGlobalObject()) as SentryGlobal;
const sentry = (global.__SENTRY__ = global.__SENTRY__ || {});
return sentry[name] || (sentry[name] = creator());
}