@@ -75,8 +75,8 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
75
75
/** Array of used integrations. */
76
76
protected _integrations : IntegrationIndex = { } ;
77
77
78
- /** Is the client still processing a call? */
79
- protected _processing : boolean = false ;
78
+ /** Number of call being processed */
79
+ protected _processing : number = 0 ;
80
80
81
81
/**
82
82
* Initializes this client instance.
@@ -99,14 +99,15 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
99
99
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
100
100
public captureException ( exception : any , hint ?: EventHint , scope ?: Scope ) : string | undefined {
101
101
let eventId : string | undefined = hint && hint . event_id ;
102
- this . _processing = true ;
103
102
104
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
105
- this . _getBackend ( )
106
- . eventFromException ( exception , hint )
107
- . then ( event => {
108
- eventId = this . captureEvent ( event , hint , scope ) ;
109
- } ) ;
103
+ this . _process (
104
+ this . _getBackend ( )
105
+ . eventFromException ( exception , hint )
106
+ . then ( event => this . _captureEvent ( event , hint , scope ) )
107
+ . then ( result => {
108
+ eventId = result ;
109
+ } ) ,
110
+ ) ;
110
111
111
112
return eventId ;
112
113
}
@@ -116,16 +117,18 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
116
117
*/
117
118
public captureMessage ( message : string , level ?: Severity , hint ?: EventHint , scope ?: Scope ) : string | undefined {
118
119
let eventId : string | undefined = hint && hint . event_id ;
119
- this . _processing = true ;
120
120
121
121
const promisedEvent = isPrimitive ( message )
122
122
? this . _getBackend ( ) . eventFromMessage ( `${ message } ` , level , hint )
123
123
: this . _getBackend ( ) . eventFromException ( message , hint ) ;
124
124
125
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
126
- promisedEvent . then ( event => {
127
- eventId = this . captureEvent ( event , hint , scope ) ;
128
- } ) ;
125
+ this . _process (
126
+ promisedEvent
127
+ . then ( event => this . _captureEvent ( event , hint , scope ) )
128
+ . then ( result => {
129
+ eventId = result ;
130
+ } ) ,
131
+ ) ;
129
132
130
133
return eventId ;
131
134
}
@@ -135,17 +138,12 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
135
138
*/
136
139
public captureEvent ( event : Event , hint ?: EventHint , scope ?: Scope ) : string | undefined {
137
140
let eventId : string | undefined = hint && hint . event_id ;
138
- this . _processing = true ;
139
141
140
- this . _processEvent ( event , hint , scope )
141
- . then ( finalEvent => {
142
- eventId = finalEvent . event_id ;
143
- this . _processing = false ;
144
- } )
145
- . then ( null , reason => {
146
- logger . error ( reason ) ;
147
- this . _processing = false ;
148
- } ) ;
142
+ this . _process (
143
+ this . _captureEvent ( event , hint , scope ) . then ( result => {
144
+ eventId = result ;
145
+ } ) ,
146
+ ) ;
149
147
150
148
return eventId ;
151
149
}
@@ -179,12 +177,11 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
179
177
* @inheritDoc
180
178
*/
181
179
public flush ( timeout ?: number ) : PromiseLike < boolean > {
182
- return this . _isClientProcessing ( timeout ) . then ( status => {
183
- clearInterval ( status . interval ) ;
180
+ return this . _isClientProcessing ( timeout ) . then ( ready => {
184
181
return this . _getBackend ( )
185
182
. getTransport ( )
186
183
. close ( timeout )
187
- . then ( transportFlushed => status . ready && transportFlushed ) ;
184
+ . then ( transportFlushed => ready && transportFlushed ) ;
188
185
} ) ;
189
186
}
190
187
@@ -263,30 +260,23 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
263
260
}
264
261
265
262
/** Waits for the client to be done with processing. */
266
- protected _isClientProcessing ( timeout ?: number ) : PromiseLike < { ready : boolean ; interval : number } > {
267
- return new SyncPromise < { ready : boolean ; interval : number } > ( resolve => {
263
+ protected _isClientProcessing ( timeout ?: number ) : PromiseLike < boolean > {
264
+ return new SyncPromise ( resolve => {
268
265
let ticked : number = 0 ;
269
266
const tick : number = 1 ;
270
267
271
- let interval = 0 ;
272
- clearInterval ( interval ) ;
273
-
274
- interval = ( setInterval ( ( ) => {
275
- if ( ! this . _processing ) {
276
- resolve ( {
277
- interval,
278
- ready : true ,
279
- } ) ;
268
+ const interval = setInterval ( ( ) => {
269
+ if ( this . _processing == 0 ) {
270
+ clearInterval ( interval ) ;
271
+ resolve ( true ) ;
280
272
} else {
281
273
ticked += tick ;
282
274
if ( timeout && ticked >= timeout ) {
283
- resolve ( {
284
- interval,
285
- ready : false ,
286
- } ) ;
275
+ clearInterval ( interval ) ;
276
+ resolve ( false ) ;
287
277
}
288
278
}
289
- } , tick ) as unknown ) as number ;
279
+ } , tick ) ;
290
280
} ) ;
291
281
}
292
282
@@ -455,6 +445,24 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
455
445
this . _getBackend ( ) . sendEvent ( event ) ;
456
446
}
457
447
448
+ /**
449
+ * Processes the event and logs an error in case of rejection
450
+ * @param event
451
+ * @param hint
452
+ * @param scope
453
+ */
454
+ protected _captureEvent ( event : Event , hint ?: EventHint , scope ?: Scope ) : PromiseLike < string | undefined > {
455
+ return this . _processEvent ( event , hint , scope ) . then (
456
+ finalEvent => {
457
+ return finalEvent . event_id ;
458
+ } ,
459
+ reason => {
460
+ logger . error ( reason ) ;
461
+ return undefined ;
462
+ } ,
463
+ ) ;
464
+ }
465
+
458
466
/**
459
467
* Processes an event (either error or message) and sends it to Sentry.
460
468
*
@@ -537,4 +545,21 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
537
545
) ;
538
546
} ) ;
539
547
}
548
+
549
+ /**
550
+ * Occupies the client with processing and event
551
+ */
552
+ protected _process < T > ( promise : PromiseLike < T > ) : void {
553
+ this . _processing += 1 ;
554
+ promise . then (
555
+ value => {
556
+ this . _processing -= 1 ;
557
+ return value ;
558
+ } ,
559
+ reason => {
560
+ this . _processing -= 1 ;
561
+ return reason ;
562
+ } ,
563
+ ) ;
564
+ }
540
565
}
0 commit comments