Skip to content

Commit 4d1f2c1

Browse files
committed
fix client changes
1 parent fec78dd commit 4d1f2c1

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

packages/core/src/client.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { parseSampleRate } from './utils/parseSampleRate';
4444
import { prepareEvent } from './utils/prepareEvent';
4545
import { reparentChildSpans, shouldIgnoreSpan } from './utils/should-ignore-span';
4646
import { getActiveSpan, showSpanDropWarning, spanToTraceContext } from './utils/spanUtils';
47-
import { rejectedSyncPromise, resolvedSyncPromise, SyncPromise } from './utils/syncpromise';
47+
import { rejectedSyncPromise } from './utils/syncpromise';
4848
import { convertSpanJsonToTransactionEvent, convertTransactionEventToSpanJson } from './utils/transactionEvent';
4949

5050
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
@@ -316,16 +316,19 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
316316
* @returns A promise that will resolve with `true` if all events are sent before the timeout, or `false` if there are
317317
* still events in the queue when the timeout is reached.
318318
*/
319-
public flush(timeout?: number): PromiseLike<boolean> {
319+
// @ts-expect-error - PromiseLike is a subset of Promise
320+
public async flush(timeout?: number): PromiseLike<boolean> {
320321
const transport = this._transport;
321-
if (transport) {
322-
this.emit('flush');
323-
return this._isClientDoneProcessing(timeout).then(clientFinished => {
324-
return transport.flush(timeout).then(transportFlushed => clientFinished && transportFlushed);
325-
});
326-
} else {
327-
return resolvedSyncPromise(true);
322+
if (!transport) {
323+
return true;
328324
}
325+
326+
this.emit('flush');
327+
328+
const clientFinished = await this._isClientDoneProcessing(timeout);
329+
const transportFlushed = await transport.flush(timeout);
330+
331+
return clientFinished && transportFlushed;
329332
}
330333

331334
/**
@@ -336,12 +339,12 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
336339
* @returns {Promise<boolean>} A promise which resolves to `true` if the flush completes successfully before the timeout, or `false` if
337340
* it doesn't.
338341
*/
339-
public close(timeout?: number): PromiseLike<boolean> {
340-
return this.flush(timeout).then(result => {
341-
this.getOptions().enabled = false;
342-
this.emit('close');
343-
return result;
344-
});
342+
// @ts-expect-error - PromiseLike is a subset of Promise
343+
public async close(timeout?: number): PromiseLike<boolean> {
344+
const result = await this.flush(timeout);
345+
this.getOptions().enabled = false;
346+
this.emit('close');
347+
return result;
345348
}
346349

347350
/**
@@ -872,18 +875,21 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
872875
/**
873876
* Send an envelope to Sentry.
874877
*/
875-
public sendEnvelope(envelope: Envelope): PromiseLike<TransportMakeRequestResponse> {
878+
// @ts-expect-error - PromiseLike is a subset of Promise
879+
public async sendEnvelope(envelope: Envelope): PromiseLike<TransportMakeRequestResponse> {
876880
this.emit('beforeEnvelope', envelope);
877881

878882
if (this._isEnabled() && this._transport) {
879-
return this._transport.send(envelope).then(null, reason => {
883+
try {
884+
return await this._transport.send(envelope);
885+
} catch (reason) {
880886
DEBUG_BUILD && debug.error('Error while sending envelope:', reason);
881887
return {};
882-
});
888+
}
883889
}
884890

885891
DEBUG_BUILD && debug.error('Transport disabled');
886-
return resolvedSyncPromise({});
892+
return {};
887893
}
888894

889895
/* eslint-enable @typescript-eslint/unified-signatures */
@@ -938,24 +944,20 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
938944
* @returns A promise which will resolve to `true` if processing is already done or finishes before the timeout, and
939945
* `false` otherwise
940946
*/
941-
protected _isClientDoneProcessing(timeout?: number): PromiseLike<boolean> {
942-
return new SyncPromise(resolve => {
943-
let ticked: number = 0;
944-
const tick: number = 1;
947+
protected async _isClientDoneProcessing(timeout?: number): Promise<boolean> {
948+
let ticked = 0;
945949

946-
const interval = setInterval(() => {
947-
if (this._numProcessing == 0) {
948-
clearInterval(interval);
949-
resolve(true);
950-
} else {
951-
ticked += tick;
952-
if (timeout && ticked >= timeout) {
953-
clearInterval(interval);
954-
resolve(false);
955-
}
956-
}
957-
}, tick);
958-
});
950+
// if no timeout is provided, we wait "forever" until everything is processed
951+
while (!timeout || ticked < timeout) {
952+
await new Promise(resolve => setTimeout(resolve, 1));
953+
954+
if (!this._numProcessing) {
955+
return true;
956+
}
957+
ticked++;
958+
}
959+
960+
return false;
959961
}
960962

961963
/** Determines whether this SDK is enabled and a transport is present. */

0 commit comments

Comments
 (0)