@@ -12,20 +12,16 @@ import {
1212 TerminalProviderInstanceNotFoundError ,
1313} from "@t3tools/contracts" ;
1414import * as HostProcess from "@t3tools/shared/HostProcess" ;
15- import * as Data from "effect/Data" ;
1615import * as Deferred from "effect/Deferred" ;
17- import * as Duration from "effect/Duration" ;
1816import * as Effect from "effect/Effect" ;
1917import * as Base64Url from "effect/encoding/Base64Url" ;
2018import * as Exit from "effect/Exit" ;
2119import * as Fiber from "effect/Fiber" ;
2220import * as FileSystem from "effect/FileSystem" ;
2321import * as Layer from "effect/Layer" ;
24- import * as Option from "effect/Option" ;
2522import * as PlatformError from "effect/PlatformError" ;
2623import * as Path from "effect/Path" ;
2724import * as Ref from "effect/Ref" ;
28- import * as Schedule from "effect/Schedule" ;
2925import * as Schema from "effect/Schema" ;
3026import * as Scope from "effect/Scope" ;
3127import * as Stream from "effect/Stream" ;
@@ -50,10 +46,6 @@ vi.mock("effect/SynchronizedRef", async (importOriginal) => {
5046
5147const encodeUnknownJson = Schema . encodeUnknownSync ( Schema . fromJsonString ( Schema . Unknown ) ) ;
5248
53- class WaitForConditionError extends Data . TaggedError ( "WaitForConditionError" ) < {
54- readonly message : string ;
55- } > { }
56-
5749class FakePtyProcess implements PtyAdapter . PtyProcess {
5850 readonly writes : string [ ] = [ ] ;
5951 readonly resizeCalls : Array < { cols : number ; rows : number } > = [ ] ;
@@ -205,26 +197,6 @@ class GatedPtyAdapter extends FakePtyAdapter {
205197 }
206198}
207199
208- const waitFor = < E , R > (
209- predicate : Effect . Effect < boolean , E , R > ,
210- timeout : Duration . Input = 800 ,
211- ) : Effect . Effect < void , WaitForConditionError | E , R > =>
212- predicate . pipe (
213- Effect . filterOrFail (
214- ( done ) => done ,
215- ( ) => new WaitForConditionError ( { message : "Condition not met" } ) ,
216- ) ,
217- Effect . retry ( Schedule . spaced ( "15 millis" ) ) ,
218- Effect . timeoutOption ( timeout ) ,
219- Effect . flatMap ( ( result ) =>
220- Option . match ( result , {
221- onNone : ( ) =>
222- Effect . fail ( new WaitForConditionError ( { message : "Timed out waiting for condition" } ) ) ,
223- onSome : ( ) => Effect . void ,
224- } ) ,
225- ) ,
226- ) ;
227-
228200function openInput ( overrides : Partial < TerminalOpenInput > = { } ) : TerminalOpenInput {
229201 return {
230202 threadId : "thread-1" ,
@@ -773,14 +745,21 @@ it.layer(
773745 Effect . gen ( function * ( ) {
774746 const ptyAdapter = new FakePtyAdapter ( ) ;
775747 let duringCheck : ( pid : number ) => Effect . Effect < void > = ( ) => Effect . void ;
776- const { manager, getEvents } = yield * createManager ( 5 , {
748+ const { manager } = yield * createManager ( 5 , {
777749 ptyAdapter,
778750 subprocessPollIntervalMs : 60_000 ,
779751 subprocessInspector : ( pid ) =>
780752 duringCheck ( pid ) . pipe (
781753 Effect . as ( { hasRunningSubprocess : false , childCommand : null , processIds : [ ] } ) ,
782754 ) ,
783755 } ) ;
756+ const outputSeen = yield * Deferred . make < void > ( ) ;
757+ const unsubscribeOutput = yield * manager . subscribe ( ( event ) =>
758+ event . type === "output"
759+ ? Deferred . succeed ( outputSeen , undefined ) . pipe ( Effect . asVoid )
760+ : Effect . void ,
761+ ) ;
762+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( unsubscribeOutput ) ) ;
784763 yield * manager . open ( openInput ( { terminalId : "typed" } ) ) ;
785764 yield * manager . open ( openInput ( { terminalId : "echoed" } ) ) ;
786765 const [ typed , echoed ] = ptyAdapter . processes ;
@@ -791,9 +770,7 @@ it.layer(
791770 . pipe ( Effect . orDie )
792771 : Effect . gen ( function * ( ) {
793772 echoed ! . emitData ( "make build\r\n" ) ;
794- yield * waitFor (
795- Effect . map ( getEvents , ( events ) => events . some ( ( event ) => event . type === "output" ) ) ,
796- ) ;
773+ yield * Deferred . await ( outputSeen ) ;
797774 } ) . pipe ( Effect . orDie ) ;
798775
799776 yield * manager . closeIdle ( { threadId : "thread-1" } ) ;
0 commit comments