@@ -162,8 +162,13 @@ export interface Shell<
162162
163163// Internal storages
164164const storage = new AsyncLocalStorage < Options > ( )
165- const snapshots : Snapshot [ ] = [ ]
166- const delimiters : Options [ 'delimiter' ] [ ] = [ ]
165+ const box = ( < B extends Snapshot | Snapshot [ 'delimiter' ] > ( box : B [ ] = [ ] ) => ( {
166+ push ( item : B ) : void {
167+ if ( box . length > 0 ) throw new Fail ( `Box is busy` )
168+ box . push ( item )
169+ } ,
170+ loot : box . pop . bind ( box ) as < T extends B > ( ) => T | undefined ,
171+ } ) ) ( )
167172
168173const getStore = ( ) = > storage . getStore ( ) || defaults
169174
@@ -202,7 +207,7 @@ export const $: $ = new Proxy<$>(
202207 pieces as TemplateStringsArray ,
203208 args
204209 ) as string
205- snapshots . push ( getSnapshot ( opts , from , cmd ) )
210+ box . push ( getSnapshot ( opts , from , cmd ) )
206211 const pp = new ProcessPromise ( noop )
207212
208213 if ( ! pp . isHalted ( ) ) pp . run ( )
@@ -260,14 +265,15 @@ export class ProcessPromise extends Promise<ProcessOutput> {
260265 executor ( ...args )
261266 } )
262267
263- if ( snapshots . length ) {
264- this . _snapshot = snapshots . pop ( ) !
268+ const snapshot = box . loot < Snapshot > ( )
269+ if ( snapshot ) {
270+ this . _snapshot = snapshot
265271 this . _resolve = resolve !
266272 this . _reject = ( v : ProcessOutput ) => {
267273 reject ! ( v )
268274 if ( this . sync ) throw v
269275 }
270- if ( this . _snapshot . halt ) this . _stage = 'halted '
276+ if ( snapshot . halt ) this . _stage = 'halted '
271277 } else ProcessPromise . disarm ( this )
272278 }
273279
@@ -800,7 +806,7 @@ export class ProcessOutput extends Error {
800806 }
801807
802808 lines ( delimiter ?: string | RegExp ) : string [ ] {
803- delimiters . push ( delimiter )
809+ box . push ( delimiter )
804810 return [ ...this ]
805811 }
806812
@@ -818,8 +824,8 @@ export class ProcessOutput extends Error {
818824
819825 * [ Symbol . iterator ] ( ) : Iterator < string > {
820826 const memo : ( string | undefined ) [ ] = [ ]
821- const dlmtr =
822- delimiters . pop ( ) || this . _dto . delimiter || $ . delimiter || DLMTR
827+ // prettier-ignore
828+ const dlmtr = box . loot < Options [ 'delimiter' ] > ( ) || this . _dto . delimiter || $ . delimiter || DLMTR
823829
824830 for ( const chunk of this . _dto . store . stdall ) {
825831 yield * getLines ( chunk , memo , dlmtr )
0 commit comments