Skip to content

Commit a71dea7

Browse files
committed
feat(core): Add addIntegration method to client
This can be used to add an integration at run time.
1 parent c157f86 commit a71dea7

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

packages/core/src/baseclient.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737

3838
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
3939
import { createEventEnvelope, createSessionEnvelope } from './envelope';
40-
import { IntegrationIndex, setupIntegrations } from './integration';
40+
import { IntegrationIndex, setupIntegration, setupIntegrations } from './integration';
4141
import { Scope } from './scope';
4242
import { updateSession } from './session';
4343
import { prepareEvent } from './utils/prepareEvent';
@@ -291,6 +291,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
291291
}
292292
}
293293

294+
/**
295+
* @inheritDoc
296+
*/
297+
public addIntegration(integration: Integration): void {
298+
setupIntegration(integration, this._integrations);
299+
}
300+
294301
/**
295302
* @inheritDoc
296303
*/

packages/core/src/integration.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ export function setupIntegrations(integrations: Integration[]): IntegrationIndex
8888
const integrationIndex: IntegrationIndex = {};
8989

9090
integrations.forEach(integration => {
91-
integrationIndex[integration.name] = integration;
92-
93-
if (installedIntegrations.indexOf(integration.name) === -1) {
94-
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
95-
installedIntegrations.push(integration.name);
96-
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
97-
}
91+
setupIntegration(integration, integrationIndex);
9892
});
9993

10094
return integrationIndex;
10195
}
96+
97+
/** Setup a single integration. */
98+
export function setupIntegration(integration: Integration, integrationIndex: IntegrationIndex): void {
99+
if (installedIntegrations.indexOf(integration.name) === -1) {
100+
integrationIndex[integration.name] = integration;
101+
102+
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
103+
installedIntegrations.push(integration.name);
104+
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
105+
}
106+
}

packages/types/src/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ export interface Client<O extends ClientOptions = ClientOptions> {
108108
/** Returns the client's instance of the given integration class, it any. */
109109
getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null;
110110

111+
/**
112+
* Add an integration to the client.
113+
* This can be used to e.g. lazy load integrations.
114+
* In most cases, this should not be necessary, and you're better off just passing the integrations via `integrations: []` at initialization time.
115+
* However, if you find the need to conditionally load & add an integration, you can use `addIntegration` to do so.
116+
* */
117+
addIntegration?(integration: Integration): void;
118+
111119
/** This is an internal function to setup all integrations that should run on the client */
112120
setupIntegrations(): void;
113121

0 commit comments

Comments
 (0)