@@ -6,7 +6,6 @@ import { DEBUG_BUILD } from './debug-build';
66import { createEventEnvelope , createSessionEnvelope } from './envelope' ;
77import type { IntegrationIndex } from './integration' ;
88import { afterSetupIntegrations , setupIntegration , setupIntegrations } from './integration' ;
9- import { stripMetadataFromStackFrames } from './metadata' ;
109import type { Scope } from './scope' ;
1110import { updateSession } from './session' ;
1211import {
@@ -45,7 +44,7 @@ import { parseSampleRate } from './utils/parseSampleRate';
4544import { prepareEvent } from './utils/prepareEvent' ;
4645import { reparentChildSpans , shouldIgnoreSpan } from './utils/should-ignore-span' ;
4746import { getActiveSpan , showSpanDropWarning , spanToTraceContext } from './utils/spanUtils' ;
48- import { rejectedSyncPromise } from './utils/syncpromise' ;
47+ import { rejectedSyncPromise , resolvedSyncPromise , SyncPromise } from './utils/syncpromise' ;
4948import { convertSpanJsonToTransactionEvent , convertTransactionEventToSpanJson } from './utils/transactionEvent' ;
5049
5150const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured." ;
@@ -317,19 +316,16 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
317316 * @returns A promise that will resolve with `true` if all events are sent before the timeout, or `false` if there are
318317 * still events in the queue when the timeout is reached.
319318 */
320- // @ts -expect-error - PromiseLike is a subset of Promise
321- public async flush ( timeout ?: number ) : PromiseLike < boolean > {
319+ public flush ( timeout ?: number ) : PromiseLike < boolean > {
322320 const transport = this . _transport ;
323- if ( ! transport ) {
324- return true ;
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 ) ;
325328 }
326-
327- this . emit ( 'flush' ) ;
328-
329- const clientFinished = await this . _isClientDoneProcessing ( timeout ) ;
330- const transportFlushed = await transport . flush ( timeout ) ;
331-
332- return clientFinished && transportFlushed ;
333329 }
334330
335331 /**
@@ -340,12 +336,12 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
340336 * @returns {Promise<boolean> } A promise which resolves to `true` if the flush completes successfully before the timeout, or `false` if
341337 * it doesn't.
342338 */
343- // @ts -expect-error - PromiseLike is a subset of Promise
344- public async close ( timeout ?: number ) : PromiseLike < boolean > {
345- const result = await this . flush ( timeout ) ;
346- this . getOptions ( ) . enabled = false ;
347- this . emit ( 'close' ) ;
348- return result ;
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+ } ) ;
349345 }
350346
351347 /**
@@ -876,21 +872,18 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
876872 /**
877873 * Send an envelope to Sentry.
878874 */
879- // @ts -expect-error - PromiseLike is a subset of Promise
880- public async sendEnvelope ( envelope : Envelope ) : PromiseLike < TransportMakeRequestResponse > {
875+ public sendEnvelope ( envelope : Envelope ) : PromiseLike < TransportMakeRequestResponse > {
881876 this . emit ( 'beforeEnvelope' , envelope ) ;
882877
883878 if ( this . _isEnabled ( ) && this . _transport ) {
884- try {
885- return await this . _transport . send ( envelope ) ;
886- } catch ( reason ) {
879+ return this . _transport . send ( envelope ) . then ( null , reason => {
887880 DEBUG_BUILD && debug . error ( 'Error while sending envelope:' , reason ) ;
888881 return { } ;
889- }
882+ } ) ;
890883 }
891884
892885 DEBUG_BUILD && debug . error ( 'Transport disabled' ) ;
893- return { } ;
886+ return resolvedSyncPromise ( { } ) ;
894887 }
895888
896889 /* eslint-enable @typescript-eslint/unified-signatures */
@@ -945,20 +938,24 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
945938 * @returns A promise which will resolve to `true` if processing is already done or finishes before the timeout, and
946939 * `false` otherwise
947940 */
948- protected async _isClientDoneProcessing ( timeout ?: number ) : Promise < boolean > {
949- let ticked = 0 ;
950-
951- // if no timeout is provided, we wait "forever" until everything is processed
952- while ( ! timeout || ticked < timeout ) {
953- await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ;
941+ protected _isClientDoneProcessing ( timeout ?: number ) : PromiseLike < boolean > {
942+ return new SyncPromise ( resolve => {
943+ let ticked : number = 0 ;
944+ const tick : number = 1 ;
954945
955- if ( ! this . _numProcessing ) {
956- return true ;
957- }
958- ticked ++ ;
959- }
960-
961- return false ;
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+ } ) ;
962959 }
963960
964961 /** Determines whether this SDK is enabled and a transport is present. */
@@ -1125,13 +1122,9 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
11251122 throw _makeDoNotSendEventError ( `${ beforeSendLabel } returned \`null\`, will not send event.` ) ;
11261123 }
11271124
1128- if ( isError ) {
1129- const session = currentScope . getSession ( ) || isolationScope . getSession ( ) ;
1130- if ( session ) {
1131- this . _updateSessionFromEvent ( session , processedEvent ) ;
1132- }
1133-
1134- stripMetadataFromStackFrames ( processedEvent ) ;
1125+ const session = currentScope . getSession ( ) || isolationScope . getSession ( ) ;
1126+ if ( isError && session ) {
1127+ this . _updateSessionFromEvent ( session , processedEvent ) ;
11351128 }
11361129
11371130 if ( isTransaction ) {
0 commit comments