From db525cf2936ec84d118613657a13ac87f61c5dc0 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 4 Jan 2023 11:41:42 +0100 Subject: [PATCH 1/2] feat(core): Add `addIntegration` method to client This can be used to add an integration at run time. --- packages/core/src/baseclient.ts | 9 ++++++++- packages/core/src/integration.ts | 19 ++++++++++++------- packages/types/src/client.ts | 8 ++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/core/src/baseclient.ts b/packages/core/src/baseclient.ts index c343c2249a6e..eb674a46ab99 100644 --- a/packages/core/src/baseclient.ts +++ b/packages/core/src/baseclient.ts @@ -37,7 +37,7 @@ import { import { getEnvelopeEndpointWithUrlEncodedAuth } from './api'; import { createEventEnvelope, createSessionEnvelope } from './envelope'; -import { IntegrationIndex, setupIntegrations } from './integration'; +import { IntegrationIndex, setupIntegration, setupIntegrations } from './integration'; import { Scope } from './scope'; import { updateSession } from './session'; import { prepareEvent } from './utils/prepareEvent'; @@ -291,6 +291,13 @@ export abstract class BaseClient implements Client { } } + /** + * @inheritDoc + */ + public addIntegration(integration: Integration): void { + setupIntegration(integration, this._integrations); + } + /** * @inheritDoc */ diff --git a/packages/core/src/integration.ts b/packages/core/src/integration.ts index e80b2a0843fe..b8c9ef16da6d 100644 --- a/packages/core/src/integration.ts +++ b/packages/core/src/integration.ts @@ -88,14 +88,19 @@ export function setupIntegrations(integrations: Integration[]): IntegrationIndex const integrationIndex: IntegrationIndex = {}; integrations.forEach(integration => { - integrationIndex[integration.name] = integration; - - if (installedIntegrations.indexOf(integration.name) === -1) { - integration.setupOnce(addGlobalEventProcessor, getCurrentHub); - installedIntegrations.push(integration.name); - __DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`); - } + setupIntegration(integration, integrationIndex); }); return integrationIndex; } + +/** Setup a single integration. */ +export function setupIntegration(integration: Integration, integrationIndex: IntegrationIndex): void { + integrationIndex[integration.name] = integration; + + if (installedIntegrations.indexOf(integration.name) === -1) { + integration.setupOnce(addGlobalEventProcessor, getCurrentHub); + installedIntegrations.push(integration.name); + __DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`); + } +} diff --git a/packages/types/src/client.ts b/packages/types/src/client.ts index 6dee7e5c9ebb..e10225112f6c 100644 --- a/packages/types/src/client.ts +++ b/packages/types/src/client.ts @@ -108,6 +108,14 @@ export interface Client { /** Returns the client's instance of the given integration class, it any. */ getIntegration(integration: IntegrationClass): T | null; + /** + * Add an integration to the client. + * This can be used to e.g. lazy load integrations. + * In most cases, this should not be necessary, and you're better off just passing the integrations via `integrations: []` at initialization time. + * However, if you find the need to conditionally load & add an integration, you can use `addIntegration` to do so. + * */ + addIntegration?(integration: Integration): void; + /** This is an internal function to setup all integrations that should run on the client */ setupIntegrations(): void; From bd399b30eeafaf45589a4d573595a240c4c9641f Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Thu, 5 Jan 2023 09:27:05 +0100 Subject: [PATCH 2/2] ref: Add todo note --- packages/types/src/client.ts | 2 ++ packages/types/src/options.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/types/src/client.ts b/packages/types/src/client.ts index e10225112f6c..00fe9f5191ed 100644 --- a/packages/types/src/client.ts +++ b/packages/types/src/client.ts @@ -113,6 +113,8 @@ export interface Client { * This can be used to e.g. lazy load integrations. * In most cases, this should not be necessary, and you're better off just passing the integrations via `integrations: []` at initialization time. * However, if you find the need to conditionally load & add an integration, you can use `addIntegration` to do so. + * + * TODO (v8): Make this a required method. * */ addIntegration?(integration: Integration): void; diff --git a/packages/types/src/options.ts b/packages/types/src/options.ts index f8f8f461543b..8e6a8465d298 100644 --- a/packages/types/src/options.ts +++ b/packages/types/src/options.ts @@ -222,7 +222,7 @@ export interface ClientOptions number | boolean; - // TODO v8: Narrow the response type to `ErrorEvent` - this is technically a breaking change. + // TODO (v8): Narrow the response type to `ErrorEvent` - this is technically a breaking change. /** * An event-processing callback for error and message events, guaranteed to be invoked after all other event * processors, which allows an event to be modified or dropped. @@ -236,7 +236,7 @@ export interface ClientOptions PromiseLike | Event | null; - // TODO v8: Narrow the response type to `TransactionEvent` - this is technically a breaking change. + // TODO (v8): Narrow the response type to `TransactionEvent` - this is technically a breaking change. /** * An event-processing callback for transaction events, guaranteed to be invoked after all other event * processors. This allows an event to be modified or dropped before it's sent.