Skip to content

Commit 0b39d59

Browse files
committed
fix: Store processing state for each flush call separately
1 parent 4bfbf99 commit 0b39d59

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

packages/core/src/baseclient.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
5858
/** Is the client still processing a call? */
5959
protected _processing: boolean = false;
6060

61-
/** Processing interval */
62-
protected _processingInterval?: number;
63-
6461
/**
6562
* Initializes this client instance.
6663
*
@@ -166,14 +163,14 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
166163
* @inheritDoc
167164
*/
168165
public flush(timeout?: number): Promise<boolean> {
169-
return this._isClientProcessing(timeout).then(clientReady => {
170-
if (this._processingInterval) {
171-
clearInterval(this._processingInterval);
166+
return this._isClientProcessing(timeout).then(status => {
167+
if (status.interval) {
168+
clearInterval(status.interval);
172169
}
173170
return this._getBackend()
174171
.getTransport()
175172
.close(timeout)
176-
.then(transportFlushed => clientReady && transportFlushed);
173+
.then(transportFlushed => status.ready && transportFlushed);
177174
});
178175
}
179176

@@ -207,20 +204,27 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
207204
}
208205

209206
/** Waits for the client to be done with processing. */
210-
protected _isClientProcessing(timeout?: number): Promise<boolean> {
211-
return new Promise<boolean>(resolve => {
207+
protected _isClientProcessing(timeout?: number): Promise<{ ready: boolean; interval: number }> {
208+
return new Promise<{ ready: boolean; interval: number }>(resolve => {
212209
let ticked: number = 0;
213210
const tick: number = 1;
214-
if (this._processingInterval) {
215-
clearInterval(this._processingInterval);
216-
}
217-
this._processingInterval = (setInterval(() => {
211+
212+
let interval = 0;
213+
clearInterval(interval);
214+
215+
interval = (setInterval(() => {
218216
if (!this._processing) {
219-
resolve(true);
217+
resolve({
218+
interval,
219+
ready: true,
220+
});
220221
} else {
221222
ticked += tick;
222223
if (timeout && ticked >= timeout) {
223-
resolve(false);
224+
resolve({
225+
interval,
226+
ready: false,
227+
});
224228
}
225229
}
226230
}, tick) as unknown) as number;

packages/core/test/lib/base.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,5 +469,18 @@ describe('BaseClient', () => {
469469
// Sends after close shouldn't work anymore
470470
expect(client.captureMessage('test')).toBeFalsy();
471471
});
472+
473+
test('multiple concurrent flush calls should just work', async () => {
474+
jest.useRealTimers();
475+
expect.assertions(3);
476+
477+
const client = new TestClient({ dsn: PUBLIC_DSN });
478+
479+
return Promise.all([
480+
client.flush(1).then(() => expect(true).toEqual(true)),
481+
client.flush(1).then(() => expect(true).toEqual(true)),
482+
client.flush(1).then(() => expect(true).toEqual(true)),
483+
]);
484+
});
472485
});
473486
});

0 commit comments

Comments
 (0)