Skip to content

Commit 8100215

Browse files
committed
Refactor inspector into a resource
1 parent df2ee37 commit 8100215

File tree

1 file changed

+69
-62
lines changed

1 file changed

+69
-62
lines changed

watch/test/helpers.ts

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assert } from "@std/assert";
33
import { emptyDir, ensureDir } from "@std/fs";
44
import { dirname, fromFileUrl, join } from "@std/path";
55
import type { Operation, Result, Stream } from "effection";
6-
import { each, Ok, sleep, spawn, until } from "effection";
6+
import { all, each, Ok, resource, sleep, spawn, until } from "effection";
77
import { cp, readFile, writeFile } from "node:fs/promises";
88

99
import type { Start } from "../watch.ts";
@@ -67,72 +67,79 @@ type SuccessfulStart = {
6767

6868
type ProcessStart = Result<SuccessfulStart>;
6969

70-
export function* inspector(stream: Stream<Start, never>) {
71-
let starts: ProcessStart[] = [];
70+
interface Inspector {
71+
starts: ProcessStart[];
72+
expectNext(): Operation<SuccessfulStart>;
73+
expectNoRestart(): Operation<void>;
74+
}
7275

73-
let expected = 0;
76+
export function inspector(stream: Stream<Start, never>): Operation<Inspector> {
77+
return resource(function* (provide) {
78+
let starts: ProcessStart[] = [];
7479

75-
yield* spawn(function* () {
76-
for (let { result } of yield* each(stream)) {
77-
if (result.ok) {
78-
let process = result.value;
79-
let start = {
80-
stdout: "",
81-
stderr: "",
82-
process: result.value,
83-
};
84-
starts.push(Ok(start));
85-
yield* spawn(function* () {
86-
for (let chunk of yield* each(process.stdout)) {
87-
start.stdout += String(chunk);
88-
yield* each.next();
89-
}
90-
});
91-
yield* spawn(function* () {
92-
for (let chunk of yield* each(process.stderr)) {
93-
start.stderr += String(chunk);
94-
yield* each.next();
95-
}
96-
});
97-
} else {
98-
starts.push(result);
99-
}
80+
let expected = 0;
10081

101-
yield* each.next();
102-
}
103-
});
82+
yield* spawn(function* () {
83+
for (let { result } of yield* each(stream)) {
84+
if (result.ok) {
85+
let process = result.value;
86+
let start = {
87+
stdout: "",
88+
stderr: "",
89+
process: result.value,
90+
};
91+
starts.push(Ok(start));
92+
yield* spawn(function* () {
93+
for (let chunk of yield* each(process.stdout)) {
94+
start.stdout += String(chunk);
95+
yield* each.next();
96+
}
97+
});
98+
yield* spawn(function* () {
99+
for (let chunk of yield* each(process.stderr)) {
100+
start.stderr += String(chunk);
101+
yield* each.next();
102+
}
103+
});
104+
} else {
105+
starts.push(result);
106+
}
104107

105-
let inspector = {
106-
starts,
107-
*expectNext(): Operation<SuccessfulStart> {
108-
let initial = expected;
109-
for (let i = 0; i < 500; i++) {
110-
if (initial < starts.length) {
111-
yield* sleep(10);
112-
expected = starts.length;
113-
let result = inspector.starts[inspector.starts.length - 1];
114-
if (result.ok) {
115-
return result.value;
108+
yield* each.next();
109+
}
110+
});
111+
112+
yield* provide({
113+
starts,
114+
*expectNext() {
115+
let initial = expected;
116+
for (let i = 0; i < 500; i++) {
117+
if (initial < starts.length) {
118+
yield* sleep(10);
119+
expected = starts.length;
120+
let result = starts[starts.length - 1];
121+
if (result.ok) {
122+
return result.value;
123+
} else {
124+
throw new Error(
125+
`expected successful start, but failed: ${result.error}`,
126+
);
127+
}
116128
} else {
117-
throw new Error(
118-
`expected successful start, but failed: ${result.error}`,
119-
);
129+
yield* sleep(10);
120130
}
121-
} else {
122-
yield* sleep(10);
123131
}
124-
}
125-
throw new Error(`expecting a sucessful start but it never appeared.`);
126-
},
127-
*expectNoRestart() {
128-
let prexisting = inspector.starts.length;
129-
yield* sleep(200);
130-
let restarts = inspector.starts.length - prexisting;
131-
assert(
132-
restarts === 0,
133-
`expected no process restarts to have happened, but instead there were: ${restarts}`,
134-
);
135-
},
136-
};
137-
return inspector;
132+
throw new Error(`expecting a sucessful start but it never appeared.`);
133+
},
134+
*expectNoRestart() {
135+
let prexisting = starts.length;
136+
yield* sleep(200);
137+
let restarts = starts.length - prexisting;
138+
assert(
139+
restarts === 0,
140+
`expected no process restarts to have happened, but instead there were: ${restarts}`,
141+
);
142+
},
143+
});
144+
});
138145
}

0 commit comments

Comments
 (0)