1
1
import { anySignal , createAbortController } from '../../common/AbortController.js' ;
2
+ import { Events } from '../../common/Events.js' ;
2
3
import { IdGenerator } from '../../common/IdGenerator.js' ;
3
4
import { unrefInterval } from '../../common/intervalHelper.js' ;
4
5
import { TimeHelper } from '../../common/TimeHelper.js' ;
@@ -9,6 +10,7 @@ import { BacktraceReportSubmission } from '../../model/http/BacktraceReportSubmi
9
10
import { BacktraceModule , BacktraceModuleBindData } from '../BacktraceModule.js' ;
10
11
import { SessionFiles } from '../storage/index.js' ;
11
12
import { BacktraceDatabaseContext } from './BacktraceDatabaseContext.js' ;
13
+ import { BacktraceDatabaseEvents } from './BacktraceDatabaseEvents.js' ;
12
14
import { BacktraceDatabaseStorageProvider } from './BacktraceDatabaseStorageProvider.js' ;
13
15
import {
14
16
AttachmentBacktraceDatabaseRecord ,
@@ -17,7 +19,7 @@ import {
17
19
ReportBacktraceDatabaseRecord ,
18
20
} from './model/BacktraceDatabaseRecord.js' ;
19
21
20
- export class BacktraceDatabase implements BacktraceModule {
22
+ export class BacktraceDatabase extends Events < BacktraceDatabaseEvents > implements BacktraceModule {
21
23
/**
22
24
* Determines if the database is enabled.
23
25
*/
@@ -46,6 +48,8 @@ export class BacktraceDatabase implements BacktraceModule {
46
48
private readonly _requestHandler : BacktraceReportSubmission ,
47
49
private readonly _sessionFiles ?: SessionFiles ,
48
50
) {
51
+ super ( ) ;
52
+
49
53
this . _databaseRecordContext = new BacktraceDatabaseContext ( this . _options ?. maximumRetries ) ;
50
54
this . _recordLimits = {
51
55
report : this . _options ?. maximumNumberOfRecords ?? 8 ,
@@ -83,7 +87,7 @@ export class BacktraceDatabase implements BacktraceModule {
83
87
return true ;
84
88
}
85
89
86
- public bind ( { reportEvents } : BacktraceModuleBindData ) : void {
90
+ public bind ( { client } : BacktraceModuleBindData ) : void {
87
91
if ( this . _enabled ) {
88
92
return ;
89
93
}
@@ -92,7 +96,7 @@ export class BacktraceDatabase implements BacktraceModule {
92
96
return ;
93
97
}
94
98
95
- reportEvents . on ( 'before-send' , ( _ , data , attachments ) => {
99
+ client . on ( 'before-send' , ( _ , data , attachments ) => {
96
100
const record = this . add ( data , attachments ) ;
97
101
98
102
if ( ! record || record . locked ) {
@@ -102,7 +106,7 @@ export class BacktraceDatabase implements BacktraceModule {
102
106
record . locked = true ;
103
107
} ) ;
104
108
105
- reportEvents . on ( 'after-send' , ( _ , data , __ , submissionResult ) => {
109
+ client . on ( 'after-send' , ( _ , data , __ , submissionResult ) => {
106
110
const record = this . _databaseRecordContext . find (
107
111
( record ) => record . type === 'report' && record . data . uuid === data . uuid ,
108
112
) ;
@@ -152,6 +156,8 @@ export class BacktraceDatabase implements BacktraceModule {
152
156
this . _databaseRecordContext . add ( record ) ;
153
157
this . lockSessionWithRecord ( record ) ;
154
158
159
+ this . emit ( 'added' , record ) ;
160
+
155
161
return record ;
156
162
}
157
163
@@ -190,6 +196,8 @@ export class BacktraceDatabase implements BacktraceModule {
190
196
this . _databaseRecordContext . add ( record ) ;
191
197
this . lockSessionWithRecord ( record ) ;
192
198
199
+ this . emit ( 'added' , record ) ;
200
+
193
201
return record ;
194
202
}
195
203
@@ -239,6 +247,8 @@ export class BacktraceDatabase implements BacktraceModule {
239
247
this . _databaseRecordContext . remove ( record ) ;
240
248
this . _storageProvider . delete ( record ) ;
241
249
this . _sessionFiles ?. unlockPreviousSessions ( record . id ) ;
250
+
251
+ this . emit ( 'removed' , record ) ;
242
252
}
243
253
}
244
254
@@ -288,11 +298,15 @@ export class BacktraceDatabase implements BacktraceModule {
288
298
try {
289
299
record . locked = true ;
290
300
301
+ this . emit ( 'before-send' , record ) ;
302
+
291
303
const result =
292
304
record . type === 'report'
293
305
? await this . _requestHandler . send ( record . data , record . attachments , signal )
294
306
: await this . _requestHandler . sendAttachment ( record . rxid , record . attachment , signal ) ;
295
307
308
+ this . emit ( 'after-send' , record , result ) ;
309
+
296
310
if (
297
311
result . status === 'Ok' ||
298
312
result . status === 'Unsupported' ||
@@ -357,10 +371,15 @@ export class BacktraceDatabase implements BacktraceModule {
357
371
358
372
for ( const record of recordsToAdd ) {
359
373
this . lockSessionWithRecord ( record ) ;
374
+ this . emit ( 'added' , record ) ;
360
375
}
361
376
}
362
377
363
378
private async setupDatabaseAutoSend ( ) {
379
+ if ( ! this . _enabled ) {
380
+ return ;
381
+ }
382
+
364
383
if ( this . _options ?. autoSend === false ) {
365
384
return ;
366
385
}
0 commit comments