@@ -32,6 +32,7 @@ export function useFlytrapFunction<
3232 args,
3333 timestamp : Date . now ( )
3434 } )
35+ _executionCursor --
3536
3637 // Replaying code
3738 if ( getMode ( ) === 'replay' ) {
@@ -49,17 +50,16 @@ export function useFlytrapFunction<
4950
5051 // Merge replay args & real args
5152 const mergedArgs = fillUnserializableFlytrapValues ( replayArgs , args )
52- // const realOutput = await fn(...mergedArgs)
5353 // @ts -expect-error for some reason `this as any` still gives error
5454 const realOutput = await executeFunctionAsync ( fn , this as any , mergedArgs )
5555 return fillUnserializableFlytrapValues ( replayOutput , realOutput )
5656 }
5757 // Capturing bugs
5858 try {
59- // const output = await fn(...args)
6059 // @ts -expect-error for some reason `this as any` still gives error
6160 const output = await executeFunctionAsync ( fn , this as any , args )
6261 saveOutputForFunction ( opts . id , output )
62+ _executionCursor ++
6363 return output
6464 } catch ( error ) {
6565 /**
@@ -94,6 +94,7 @@ export function useFlytrapFunction<
9494 args,
9595 timestamp : Date . now ( )
9696 } )
97+ _executionCursor --
9798
9899 // Replaying code
99100 if ( getMode ( ) === 'replay' ) {
@@ -105,14 +106,12 @@ export function useFlytrapFunction<
105106 `Could not find replay input arguments for function with ID ${ opts . id } , either this is a function you have added, or something has gone wrong during the capture process. If this is the case, contact us.`
106107 )
107108 }
108- // return fn(...args)
109109 // @ts -expect-error for some reason `this as any` still gives error
110110 return executeFunction ( fn , this as any , args )
111111 }
112112
113113 // Merge replay args & real args
114114 const mergedArgs = fillUnserializableFlytrapValues ( replayArgs , args )
115- // const realOutput = fn(...mergedArgs)
116115 // @ts -expect-error for some reason `this as any` still gives error
117116 const realOutput = executeFunction ( fn , this as any , mergedArgs )
118117 return fillUnserializableFlytrapValues ( replayOutput , realOutput )
@@ -123,6 +122,7 @@ export function useFlytrapFunction<
123122 // @ts -expect-error for some reason `this as any` still gives error
124123 const output = executeFunction ( fn , this as any , args )
125124 saveOutputForFunction ( opts . id , output )
125+ _executionCursor ++
126126 return output
127127 } catch ( error ) {
128128 /**
@@ -324,12 +324,17 @@ function getReplayFunctionOutput(functionId: string): any | undefined {
324324}
325325
326326let _executingFunctions : CapturedFunction [ ] = [ ]
327+ let _executionCursor = 1
327328let _functionCalls : CapturedCall [ ] = [ ]
328329let _userId : string | undefined = undefined
329330
330331export const getCapturedFunctions = ( ) => _executingFunctions
331332export const getCapturedCalls = ( ) => _functionCalls
332333
334+ export const getUserId = ( ) => _userId
335+ export const getExecutingFunction = ( ) =>
336+ _executingFunctions . at ( - _executionCursor ) ?? _executingFunctions . at ( - 1 )
337+
333338export const clearCapturedFunctions = ( ) => {
334339 _executingFunctions = [ ]
335340}
@@ -392,8 +397,6 @@ export function identify(userId: string) {
392397 log . info ( 'identify' , `Identified current user as "${ userId } "` )
393398 _userId = userId
394399}
395- export const getUserId = ( ) => _userId
396- export const getExecutingFunction = ( ) => _executingFunctions . at ( - 1 )
397400
398401const addFunctionInvocation = ( functionId : string , invocation : CaptureInvocation ) => {
399402 log . info ( 'function-execution' , `Executing function with ID "${ functionId } "` )
0 commit comments