Skip to content

Commit

Permalink
add hooks types and base implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Mar 8, 2023
1 parent a1dab3b commit 3067925
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import type {
Event,
EventDropReason,
EventHint,
HookCallback,
HookName,
HookStore,
Integration,
IntegrationClass,
Outcome,
Expand Down Expand Up @@ -97,6 +100,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
/** Holds flushable */
private _outcomes: { [key: string]: number } = {};

private _hooks: HookStore = {};

/**
* Initializes this client instance.
*
Expand Down Expand Up @@ -351,6 +356,30 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
}
}

/**
* @inheritDoc
*/
public on(hook: HookName, callback: HookCallback): void {
if (this._hooks[hook]) {
// @ts-ignore we cannot enforce the callback to match the hook
// while saving bundle size
this._hooks[hook].push(callback);
} else {
this._hooks[hook] = [];
}
}

/**
* @inheritDoc
*/
public emit(hook: HookName, ...args: Parameters<HookCallback>): void {
if (this._hooks[hook]) {
// @ts-ignore we cannot enforce the callback to match the hook
// while saving bundle size
this._hooks[hook].forEach((callback: HookCallback) => callback(...args));
}
}

/** Updates existing session based on the provided event */
protected _updateSessionFromEvent(session: Session, event: Event): void {
let crashed = false;
Expand Down
25 changes: 25 additions & 0 deletions packages/types/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { EventDropReason } from './clientreport';
import type { DataCategory } from './datacategory';
import type { DsnComponents } from './dsn';
import type { Event, EventHint } from './event';
import type { EnvelopeHookCallback, EnvelopeHookName, TransactionHookCallback, TransactionHookName } from './hooks';
import type { Integration, IntegrationClass } from './integration';
import type { ClientOptions } from './options';
import type { Scope } from './scope';
Expand Down Expand Up @@ -147,4 +148,28 @@ export interface Client<O extends ClientOptions = ClientOptions> {
* @param event The dropped event.
*/
recordDroppedEvent(reason: EventDropReason, dataCategory: DataCategory, event?: Event): void;

// HOOKS

/**
* Register a callback for transaction start and finish.
*/
on(hook: TransactionHookName, callback: TransactionHookCallback): void;

/**
* Register a callback for envelope creation and sending.
*/
on(hook: EnvelopeHookName, callback: EnvelopeHookCallback): void;

/**
* Fire a hook event for transaction start and finish. Expects to be given a transaction as the
* second argument.
*/
emit(hook: TransactionHookName, ...params: Parameters<TransactionHookCallback>): void;

/*
* Fire a hook event for envelope creation and sending. Expects to be given an envelope as the
* second argument.
*/
emit(hook: EnvelopeHookName, ...params: Parameters<EnvelopeHookCallback>): void;
}
19 changes: 19 additions & 0 deletions packages/types/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Envelope } from './envelope';
import type { Transaction } from './transaction';

export type TransactionHookName = 'startTransaction' | 'transactionFinish';
export type TransactionHookCallback = (transaction: Transaction) => void;

export type EnvelopeHookName = 'beforeEnvelope';
export type EnvelopeHookCallback = (envelope: Envelope) => void;

export type HookName = TransactionHookName | EnvelopeHookName;
export type HookCallback = TransactionHookCallback | EnvelopeHookCallback;

export type HookStoreItem<N extends HookName, C extends HookCallback> = Partial<{ [key in N]: C[] }>;

export type HookStore =
// Hooks related to transaction start/finish
HookStoreItem<TransactionHookName, TransactionHookCallback> &
// Hooks related to envelope create and send
HookStoreItem<EnvelopeHookName, EnvelopeHookCallback>;
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ export type { WrappedFunction } from './wrappedfunction';
export type { Instrumenter } from './instrumenter';

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

0 comments on commit 3067925

Please sign in to comment.