Skip to content

Commit 7ab43df

Browse files
committed
sdk-core: use core client as event emitter
1 parent edb832c commit 7ab43df

File tree

9 files changed

+45
-36
lines changed

9 files changed

+45
-36
lines changed

packages/sdk-core/src/BacktraceCoreClient.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CoreClientSetup } from './builder/CoreClientSetup.js';
22
import { Events } from './common/Events.js';
3-
import { ReportEvents } from './events/ReportEvents.js';
3+
import { ClientEvents } from './events/ClientEvents.js';
44
import {
55
BacktraceAttachment,
66
BacktraceAttributeProvider,
@@ -34,7 +34,9 @@ import { MetricsBuilder } from './modules/metrics/MetricsBuilder.js';
3434
import { SingleSessionProvider } from './modules/metrics/SingleSessionProvider.js';
3535
import { RateLimitWatcher } from './modules/rateLimiter/RateLimitWatcher.js';
3636

37-
export abstract class BacktraceCoreClient<O extends BacktraceConfiguration = BacktraceConfiguration> {
37+
export abstract class BacktraceCoreClient<
38+
O extends BacktraceConfiguration = BacktraceConfiguration,
39+
> extends Events<ClientEvents> {
3840
/**
3941
* Backtrace client instance
4042
*/
@@ -112,7 +114,11 @@ export abstract class BacktraceCoreClient<O extends BacktraceConfiguration = Bac
112114
}
113115

114116
protected readonly options: O;
115-
protected readonly reportEvents: Events<ReportEvents>;
117+
118+
/**
119+
* @deprecated use client instance directly
120+
*/
121+
protected readonly reportEvents: Events<ClientEvents>;
116122
protected readonly attributeManager: AttributeManager;
117123
protected readonly attachmentManager: AttachmentManager;
118124
protected readonly fileSystem?: FileSystem;
@@ -128,7 +134,9 @@ export abstract class BacktraceCoreClient<O extends BacktraceConfiguration = Bac
128134
private _enabled = false;
129135

130136
protected constructor(setup: CoreClientSetup<O>) {
131-
this.reportEvents = new Events();
137+
super();
138+
139+
this.reportEvents = this;
132140

133141
this.options = setup.options;
134142
this.fileSystem = setup.fileSystem;
@@ -329,12 +337,12 @@ export abstract class BacktraceCoreClient<O extends BacktraceConfiguration = Bac
329337

330338
const submissionAttachments = this.generateSubmissionAttachments(report, reportAttachments);
331339

332-
this.reportEvents.emit('before-send', report, backtraceData, submissionAttachments);
340+
this.emit('before-send', report, backtraceData, submissionAttachments);
333341

334342
return this._reportSubmission
335343
.send(backtraceData, submissionAttachments, abortSignal)
336344
.then((submissionResult) => {
337-
this.reportEvents.emit('after-send', report, backtraceData, submissionAttachments, submissionResult);
345+
this.emit('after-send', report, backtraceData, submissionAttachments, submissionResult);
338346
return submissionResult;
339347
});
340348
}
@@ -403,7 +411,7 @@ export abstract class BacktraceCoreClient<O extends BacktraceConfiguration = Bac
403411
return {
404412
client: this,
405413
options: this.options,
406-
reportEvents: this.reportEvents,
414+
reportEvents: this,
407415
attributeManager: this.attributeManager,
408416
attachmentManager: this.attachmentManager,
409417
reportSubmission: this._reportSubmission,

packages/sdk-core/src/common/Events.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
interface EventCallback {
3-
callback: (...args: any[]) => unknown;
2+
interface EventCallback<A extends any[] = any[]> {
3+
callback: (...args: A) => unknown;
44
once?: boolean;
55
}
66

7-
export class Events<
8-
E extends Record<string | number | symbol, (...args: any[]) => unknown> = Record<
9-
string | number | symbol,
10-
(...args: any[]) => unknown
11-
>,
12-
> {
7+
/* eslint-disable @typescript-eslint/no-explicit-any */
8+
export type EventMap = Record<string, any[]>;
9+
10+
export class Events<E extends EventMap = EventMap> {
1311
private readonly _callbacks: Partial<Record<keyof E, EventCallback[]>> = {};
1412

15-
public on<N extends keyof E>(event: N, callback: E[N]): this {
13+
public on<N extends keyof E>(event: N, callback: (...args: E[N]) => unknown): this {
1614
this.addCallback(event, { callback });
1715
return this;
1816
}
1917

20-
public once<N extends keyof E>(event: N, callback: E[N]): this {
18+
public once<N extends keyof E>(event: N, callback: (...args: E[N]) => unknown): this {
2119
this.addCallback(event, { callback, once: true });
2220
return this;
2321
}
2422

25-
public off<N extends keyof E>(event: N, callback: E[N]): this {
23+
public off<N extends keyof E>(event: N, callback: (...args: E[N]) => unknown): this {
2624
this.removeCallback(event, callback);
2725
return this;
2826
}
2927

30-
public emit<N extends keyof E>(event: N, ...args: Parameters<E[N]>): boolean {
28+
public emit<N extends keyof E>(event: N, ...args: E[N]): boolean {
3129
const callbacks = this._callbacks[event];
3230
if (!callbacks || !callbacks.length) {
3331
return false;
@@ -48,7 +46,7 @@ export class Events<
4846
return true;
4947
}
5048

51-
private addCallback(event: keyof E, callback: EventCallback) {
49+
private addCallback<A extends unknown[]>(event: keyof E, callback: EventCallback<A>) {
5250
const list = this._callbacks[event];
5351
if (list) {
5452
list.push(callback);
@@ -57,7 +55,7 @@ export class Events<
5755
}
5856
}
5957

60-
private removeCallback(event: keyof E, callback: EventCallback['callback']) {
58+
private removeCallback<A extends unknown[]>(event: keyof E, callback: EventCallback<A>['callback']) {
6159
const list = this._callbacks[event];
6260
if (!list) {
6361
return;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BacktraceAttachment } from '../model/attachment/index.js';
22

33
export type AttachmentEvents = {
4-
'scoped-attachments-updated'(attachments: BacktraceAttachment[]): void;
4+
'scoped-attachments-updated': [attachments: BacktraceAttachment[]];
55
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReportData } from '../model/report/ReportData.js';
22

33
export type AttributeEvents = {
4-
'scoped-attributes-updated'(attributes: ReportData): void;
4+
'scoped-attributes-updated': [attributes: ReportData];
55
};

packages/sdk-core/src/events/ReportEvents.ts renamed to packages/sdk-core/src/events/ClientEvents.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { BacktraceData } from '../model/data/index.js';
33
import { BacktraceReportSubmissionResult, BacktraceSubmissionResponse } from '../model/http/index.js';
44
import { BacktraceReport } from '../model/report/BacktraceReport.js';
55

6-
export type ReportEvents = {
7-
'before-skip'(report: BacktraceReport): void;
8-
'before-send'(report: BacktraceReport, data: BacktraceData, attachments: BacktraceAttachment[]): void;
9-
'after-send'(
6+
export type ClientEvents = {
7+
'before-skip': [report: BacktraceReport];
8+
'before-send': [report: BacktraceReport, data: BacktraceData, attachments: BacktraceAttachment[]];
9+
'after-send': [
1010
report: BacktraceReport,
1111
data: BacktraceData,
1212
attachments: BacktraceAttachment[],
1313
result: BacktraceReportSubmissionResult<BacktraceSubmissionResponse>,
14-
): void;
14+
];
1515
};

packages/sdk-core/src/modules/BacktraceModule.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Events } from '../common/Events.js';
2-
import { ReportEvents } from '../events/ReportEvents.js';
2+
import { ClientEvents } from '../events/ClientEvents.js';
33
import {
44
BacktraceConfiguration,
55
BacktraceCoreClient,
@@ -17,7 +17,10 @@ export interface BacktraceModuleBindData {
1717
readonly options: BacktraceConfiguration;
1818
readonly attributeManager: AttributeManager;
1919
readonly attachmentManager: AttachmentManager;
20-
readonly reportEvents: Events<ReportEvents>;
20+
/**
21+
* @deprecated use client instance directly
22+
*/
23+
readonly reportEvents: Events<ClientEvents>;
2124
readonly reportSubmission: BacktraceReportSubmission;
2225
readonly requestHandler: BacktraceRequestHandler;
2326
readonly database?: BacktraceDatabase;

packages/sdk-core/src/modules/breadcrumbs/BreadcrumbsManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class BreadcrumbsManager implements BacktraceBreadcrumbs, BacktraceModule
6666
}
6767
}
6868

69-
public bind({ client, reportEvents, attachmentManager }: BacktraceModuleBindData): void {
69+
public bind({ client, attachmentManager }: BacktraceModuleBindData): void {
7070
if (this._storage.getAttachmentProviders) {
7171
attachmentManager.addProviders(...this._storage.getAttachmentProviders());
7272
} else {
@@ -77,7 +77,7 @@ export class BreadcrumbsManager implements BacktraceBreadcrumbs, BacktraceModule
7777
[BREADCRUMB_ATTRIBUTE_NAME]: this._storage.lastBreadcrumbId,
7878
}));
7979

80-
reportEvents.on('before-skip', (report) => this.logReport(report));
80+
client.on('before-skip', (report) => this.logReport(report));
8181
}
8282

8383
public initialize() {

packages/sdk-core/src/modules/database/BacktraceDatabase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class BacktraceDatabase implements BacktraceModule {
8282
return true;
8383
}
8484

85-
public bind({ reportEvents }: BacktraceModuleBindData): void {
85+
public bind({ client }: BacktraceModuleBindData): void {
8686
if (this._enabled) {
8787
return;
8888
}
@@ -91,7 +91,7 @@ export class BacktraceDatabase implements BacktraceModule {
9191
return;
9292
}
9393

94-
reportEvents.on('before-send', (_, data, attachments) => {
94+
client.on('before-send', (_, data, attachments) => {
9595
const record = this.add(data, attachments);
9696

9797
if (!record || record.locked) {
@@ -101,7 +101,7 @@ export class BacktraceDatabase implements BacktraceModule {
101101
record.locked = true;
102102
});
103103

104-
reportEvents.on('after-send', (_, data, __, submissionResult) => {
104+
client.on('after-send', (_, data, __, submissionResult) => {
105105
const record = this._databaseRecordContext.find(
106106
(record) => record.type === 'report' && record.data.uuid === data.uuid,
107107
);

packages/sdk-core/src/modules/storage/SessionFiles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface FileSession {
1111
}
1212

1313
type SessionEvents = {
14-
unlocked(): void;
14+
unlocked: [];
1515
};
1616

1717
const SESSION_MARKER_PREFIX = 'bt-session';

0 commit comments

Comments
 (0)