Skip to content

Commit 3067925

Browse files
committed
add hooks types and base implementation
1 parent a1dab3b commit 3067925

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

packages/core/src/baseclient.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import type {
99
Event,
1010
EventDropReason,
1111
EventHint,
12+
HookCallback,
13+
HookName,
14+
HookStore,
1215
Integration,
1316
IntegrationClass,
1417
Outcome,
@@ -97,6 +100,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
97100
/** Holds flushable */
98101
private _outcomes: { [key: string]: number } = {};
99102

103+
private _hooks: HookStore = {};
104+
100105
/**
101106
* Initializes this client instance.
102107
*
@@ -351,6 +356,30 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
351356
}
352357
}
353358

359+
/**
360+
* @inheritDoc
361+
*/
362+
public on(hook: HookName, callback: HookCallback): void {
363+
if (this._hooks[hook]) {
364+
// @ts-ignore we cannot enforce the callback to match the hook
365+
// while saving bundle size
366+
this._hooks[hook].push(callback);
367+
} else {
368+
this._hooks[hook] = [];
369+
}
370+
}
371+
372+
/**
373+
* @inheritDoc
374+
*/
375+
public emit(hook: HookName, ...args: Parameters<HookCallback>): void {
376+
if (this._hooks[hook]) {
377+
// @ts-ignore we cannot enforce the callback to match the hook
378+
// while saving bundle size
379+
this._hooks[hook].forEach((callback: HookCallback) => callback(...args));
380+
}
381+
}
382+
354383
/** Updates existing session based on the provided event */
355384
protected _updateSessionFromEvent(session: Session, event: Event): void {
356385
let crashed = false;

packages/types/src/client.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { EventDropReason } from './clientreport';
22
import type { DataCategory } from './datacategory';
33
import type { DsnComponents } from './dsn';
44
import type { Event, EventHint } from './event';
5+
import type { EnvelopeHookCallback, EnvelopeHookName, TransactionHookCallback, TransactionHookName } from './hooks';
56
import type { Integration, IntegrationClass } from './integration';
67
import type { ClientOptions } from './options';
78
import type { Scope } from './scope';
@@ -147,4 +148,28 @@ export interface Client<O extends ClientOptions = ClientOptions> {
147148
* @param event The dropped event.
148149
*/
149150
recordDroppedEvent(reason: EventDropReason, dataCategory: DataCategory, event?: Event): void;
151+
152+
// HOOKS
153+
154+
/**
155+
* Register a callback for transaction start and finish.
156+
*/
157+
on(hook: TransactionHookName, callback: TransactionHookCallback): void;
158+
159+
/**
160+
* Register a callback for envelope creation and sending.
161+
*/
162+
on(hook: EnvelopeHookName, callback: EnvelopeHookCallback): void;
163+
164+
/**
165+
* Fire a hook event for transaction start and finish. Expects to be given a transaction as the
166+
* second argument.
167+
*/
168+
emit(hook: TransactionHookName, ...params: Parameters<TransactionHookCallback>): void;
169+
170+
/*
171+
* Fire a hook event for envelope creation and sending. Expects to be given an envelope as the
172+
* second argument.
173+
*/
174+
emit(hook: EnvelopeHookName, ...params: Parameters<EnvelopeHookCallback>): void;
150175
}

packages/types/src/hooks.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { Envelope } from './envelope';
2+
import type { Transaction } from './transaction';
3+
4+
export type TransactionHookName = 'startTransaction' | 'transactionFinish';
5+
export type TransactionHookCallback = (transaction: Transaction) => void;
6+
7+
export type EnvelopeHookName = 'beforeEnvelope';
8+
export type EnvelopeHookCallback = (envelope: Envelope) => void;
9+
10+
export type HookName = TransactionHookName | EnvelopeHookName;
11+
export type HookCallback = TransactionHookCallback | EnvelopeHookCallback;
12+
13+
export type HookStoreItem<N extends HookName, C extends HookCallback> = Partial<{ [key in N]: C[] }>;
14+
15+
export type HookStore =
16+
// Hooks related to transaction start/finish
17+
HookStoreItem<TransactionHookName, TransactionHookCallback> &
18+
// Hooks related to envelope create and send
19+
HookStoreItem<EnvelopeHookName, EnvelopeHookCallback>;

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ export type { WrappedFunction } from './wrappedfunction';
9696
export type { Instrumenter } from './instrumenter';
9797

9898
export type { BrowserClientReplayOptions } from './browseroptions';
99+
export type { HookStore, HookName, HookCallback } from './hooks';

0 commit comments

Comments
 (0)