Skip to content

feat(core): Add optional setup hook to integrations #9556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,17 @@ export function setupIntegrations(client: Client, integrations: Integration[]):
export function setupIntegration(client: Client, integration: Integration, integrationIndex: IntegrationIndex): void {
integrationIndex[integration.name] = integration;

// `setupOnce` is only called the first time
if (installedIntegrations.indexOf(integration.name) === -1) {
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
installedIntegrations.push(integration.name);
}

// `setup` is run for each client
if (integration.setup && typeof integration.setup === 'function') {
integration.setup(client);
}

if (client.on && typeof integration.preprocessEvent === 'function') {
const callback = integration.preprocessEvent.bind(integration) as typeof integration.preprocessEvent;
client.on('preprocessEvent', (event, hint) => callback(event, hint, client));
Expand Down
38 changes: 38 additions & 0 deletions packages/core/test/lib/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,44 @@ describe('setupIntegration', () => {
expect(integration4.setupOnce).not.toHaveBeenCalled();
});

it('calls setup for each client', () => {
class CustomIntegration implements Integration {
name = 'test';
setupOnce = jest.fn();
setup = jest.fn();
}

const client1 = getTestClient();
const client2 = getTestClient();

const integrationIndex = {};
const integration1 = new CustomIntegration();
const integration2 = new CustomIntegration();
const integration3 = new CustomIntegration();
const integration4 = new CustomIntegration();

setupIntegration(client1, integration1, integrationIndex);
setupIntegration(client1, integration2, integrationIndex);
setupIntegration(client2, integration3, integrationIndex);
setupIntegration(client2, integration4, integrationIndex);

expect(integrationIndex).toEqual({ test: integration4 });
expect(integration1.setupOnce).toHaveBeenCalledTimes(1);
expect(integration2.setupOnce).not.toHaveBeenCalled();
expect(integration3.setupOnce).not.toHaveBeenCalled();
expect(integration4.setupOnce).not.toHaveBeenCalled();

expect(integration1.setup).toHaveBeenCalledTimes(1);
expect(integration2.setup).toHaveBeenCalledTimes(1);
expect(integration3.setup).toHaveBeenCalledTimes(1);
expect(integration4.setup).toHaveBeenCalledTimes(1);

expect(integration1.setup).toHaveBeenCalledWith(client1);
expect(integration2.setup).toHaveBeenCalledWith(client1);
expect(integration3.setup).toHaveBeenCalledWith(client2);
expect(integration4.setup).toHaveBeenCalledWith(client2);
});

it('binds preprocessEvent for each client', () => {
class CustomIntegration implements Integration {
name = 'test';
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export interface Integration {
*/
setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void;

/**
* Set up an integration for the given client.
* Receives the client as argument.
*
* Whenever possible, prefer this over `setupOnce`, as that is only run for the first client,
* whereas `setup` runs for each client. Only truly global things (e.g. registering global handlers)
* should be done in `setupOnce`.
*/
setup?(client: Client): void;

/**
* An optional hook that allows to preprocess an event _before_ it is passed to all other event processors.
*/
Expand Down