Skip to content

Commit 52ef19e

Browse files
lforstAbhiPrasad
authored andcommitted
ref(core): Simplify the setup logic of integrations (#4952)
1 parent 2b74d14 commit 52ef19e

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

packages/core/src/baseclient.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
7575
/** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */
7676
protected readonly _dsn?: DsnComponents;
7777

78-
/** Array of used integrations. */
78+
/** Array of set up integrations. */
7979
protected _integrations: IntegrationIndex = {};
8080

81+
/** Indicates whether this client's integrations have been set up. */
82+
protected _integrationsInitialized: boolean = false;
83+
8184
/** Number of calls being processed */
8285
protected _numProcessing: number = 0;
8386

@@ -251,8 +254,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
251254
* Sets up the integrations
252255
*/
253256
public setupIntegrations(): void {
254-
if (this._isEnabled() && !this._integrations.initialized) {
255-
this._integrations = setupIntegrations(this._options);
257+
if (this._isEnabled() && !this._integrationsInitialized) {
258+
this._integrations = setupIntegrations(this._options.integrations);
259+
this._integrationsInitialized = true;
256260
}
257261
}
258262

packages/core/src/integration.ts

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
2-
import { ClientOptions, Integration, Options } from '@sentry/types';
3-
import { addNonEnumerableProperty, logger } from '@sentry/utils';
2+
import { Integration, Options } from '@sentry/types';
3+
import { logger } from '@sentry/utils';
44

55
import { IS_DEBUG_BUILD } from './flags';
66

@@ -9,7 +9,7 @@ export const installedIntegrations: string[] = [];
99
/** Map of integrations assigned to a client */
1010
export type IntegrationIndex = {
1111
[key: string]: Integration;
12-
} & { initialized?: boolean };
12+
};
1313

1414
/**
1515
* @private
@@ -54,31 +54,24 @@ export function getIntegrationsToSetup(options: Options): Integration[] {
5454
return integrations;
5555
}
5656

57-
/** Setup given integration */
58-
export function setupIntegration(integration: Integration): void {
59-
if (installedIntegrations.indexOf(integration.name) !== -1) {
60-
return;
61-
}
62-
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
63-
installedIntegrations.push(integration.name);
64-
IS_DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);
65-
}
66-
6757
/**
6858
* Given a list of integration instances this installs them all. When `withDefaults` is set to `true` then all default
6959
* integrations are added unless they were already provided before.
7060
* @param integrations array of integration instances
7161
* @param withDefault should enable default integrations
7262
*/
73-
export function setupIntegrations<O extends ClientOptions>(options: O): IntegrationIndex {
74-
const integrations: IntegrationIndex = {};
75-
options.integrations.forEach(integration => {
76-
integrations[integration.name] = integration;
77-
setupIntegration(integration);
63+
export function setupIntegrations(integrations: Integration[]): IntegrationIndex {
64+
const integrationIndex: IntegrationIndex = {};
65+
66+
integrations.forEach(integration => {
67+
integrationIndex[integration.name] = integration;
68+
69+
if (installedIntegrations.indexOf(integration.name) === -1) {
70+
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
71+
installedIntegrations.push(integration.name);
72+
IS_DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);
73+
}
7874
});
79-
// set the `initialized` flag so we don't run through the process again unecessarily; use `Object.defineProperty`
80-
// because by default it creates a property which is nonenumerable, which we want since `initialized` shouldn't be
81-
// considered a member of the index the way the actual integrations are
82-
addNonEnumerableProperty(integrations, 'initialized', true);
83-
return integrations;
75+
76+
return integrationIndex;
8477
}

0 commit comments

Comments
 (0)