@@ -11,6 +11,7 @@ let running = false
1111let errFn
1212let queueId = 0
1313let sessionId = null
14+ let sessionStack = [ ] // Stack to support nested sessions
1415let asyncErr = null
1516let ignoredErrs = [ ]
1617
@@ -89,6 +90,7 @@ module.exports = {
8990 if ( promise && running ) this . catch ( )
9091 queueId ++
9192 sessionId = null
93+ sessionStack = [ ] // Clear the session stack
9294 asyncErr = null
9395 log ( `${ currentQueue ( ) } Starting recording promises` )
9496 promise = Promise . resolve ( )
@@ -123,8 +125,13 @@ module.exports = {
123125 */
124126 start ( name ) {
125127 if ( sessionId ) {
126- debug ( `${ currentQueue ( ) } Session already started as ${ sessionId } ` )
127- this . restore ( sessionId )
128+ debug ( `${ currentQueue ( ) } Session already started as ${ sessionId } , nesting session ${ name } ` )
129+ // Push current session to stack instead of restoring it
130+ sessionStack . push ( {
131+ id : sessionId ,
132+ promise : promise ,
133+ running : this . running ,
134+ } )
128135 }
129136 debug ( `${ currentQueue ( ) } Starting <${ name } > session` )
130137 tasks . push ( '--->' )
@@ -142,9 +149,18 @@ module.exports = {
142149 tasks . push ( '<---' )
143150 debug ( `${ currentQueue ( ) } Finalize <${ name } > session` )
144151 this . running = false
145- sessionId = null
146152 this . catch ( errFn )
147153 promise = promise . then ( ( ) => oldPromises . pop ( ) )
154+
155+ // Restore parent session from stack if available
156+ if ( sessionStack . length > 0 ) {
157+ const parentSession = sessionStack . pop ( )
158+ sessionId = parentSession . id
159+ this . running = parentSession . running
160+ debug ( `${ currentQueue ( ) } Restored parent session <${ sessionId } >` )
161+ } else {
162+ sessionId = null
163+ }
148164 } ,
149165
150166 /**
0 commit comments