Skip to content

Commit b3f5826

Browse files
authored
refactor: drop createSignal, and name what jsx-ast and html hold
createSignal had the channel as its only caller, so the channel keeps its own promise to wake its reader, and misc.mjs is as on main. The channel queues items, not batches: merge pushes single items through it. jsx-ast's describe pushes into the channel itself, named described as it holds descriptors, and readPages goes. html's alongside becomes writeWhileCompiling, which says what it decides. The test of the manifest not waiting for the pages goes, as the one of worker.stream holding back the next batch checks the same and more. html's test of the bundles' timing no longer reads all.html back, which its sidebar-order test checks, and the channel's tests use Array.fromAsync. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fbce898 commit b3f5826

7 files changed

Lines changed: 71 additions & 176 deletions

File tree

‎packages/core/src/utils/__tests__/channel.test.mjs‎

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,44 @@ import { describe, it } from 'node:test';
33

44
import { createChannel } from '../channel.mjs';
55

6-
/**
7-
* Reads every batch of an async iterable.
8-
*
9-
* @template T
10-
* @param {AsyncIterable<T[]>} iterable
11-
* @returns {Promise<T[][]>}
12-
*/
13-
async function readBatches(iterable) {
14-
const batches = [];
15-
16-
for await (const batch of iterable) {
17-
batches.push(batch);
18-
}
19-
20-
return batches;
21-
}
22-
236
describe('createChannel', () => {
24-
it('gives the batches as they are added, until it ends', async () => {
7+
it('gives the items as they are added, until it ends', async () => {
258
const channel = createChannel();
26-
const reading = readBatches(channel);
9+
const reading = Array.fromAsync(channel);
2710

2811
channel.push([1]);
2912
await new Promise(resolve => setImmediate(resolve));
3013
channel.push([2]);
3114
channel.end();
3215

33-
const batches = await reading;
16+
const items = await reading;
3417

35-
deepStrictEqual(batches, [[1], [2]]);
18+
deepStrictEqual(items, [[1], [2]]);
3619
});
3720

38-
it('gives a prepended batch before those not read yet', async () => {
21+
it('gives a prepended item before those not read yet', async () => {
3922
const channel = createChannel();
4023

4124
channel.push([1]);
4225
channel.push([2]);
4326
channel.prepend([0]);
4427
channel.end();
4528

46-
const batches = await readBatches(channel);
29+
const items = await Array.fromAsync(channel);
4730

48-
deepStrictEqual(batches, [[0], [1], [2]]);
31+
deepStrictEqual(items, [[0], [1], [2]]);
4932
});
5033

51-
it('drops the batches not read yet when cleared', async () => {
34+
it('drops the items not read yet when cleared', async () => {
5235
const channel = createChannel();
5336

5437
channel.push([1]);
5538
channel.clear();
5639
channel.push([2]);
5740
channel.end();
5841

59-
const batches = await readBatches(channel);
42+
const items = await Array.fromAsync(channel);
6043

61-
deepStrictEqual(batches, [[2]]);
44+
deepStrictEqual(items, [[2]]);
6245
});
6346
});

‎packages/core/src/utils/channel.mjs‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,80 @@
11
'use strict';
22

3-
import { createSignal } from './misc.mjs';
4-
53
/**
6-
* Creates a queue of batches that can be read as an async iterable while it is
4+
* Creates a queue of items that can be read as an async iterable while it is
75
* still being filled.
86
*
97
* @template T
108
*/
119
export const createChannel = () => {
12-
/** @type {Array<T[]>} */
13-
const batches = [];
10+
/** @type {Array<T>} */
11+
const items = [];
12+
13+
// Resolved as items are added, and at the end
14+
let added = Promise.withResolvers();
1415

15-
// Notified as batches are added, and at the end
16-
const added = createSignal();
16+
/**
17+
* Wakes the reader waiting for an item, or for the end.
18+
*/
19+
const notify = () => {
20+
added.resolve();
21+
added = Promise.withResolvers();
22+
};
1723

1824
let ended = false;
1925

2026
return {
2127
/**
22-
* Adds a batch, after those not read yet.
28+
* Adds an item, after those not read yet.
2329
*
24-
* @param {T[]} batch
30+
* @param {T} item
2531
*/
26-
push(batch) {
27-
batches.push(batch);
28-
added.notify();
32+
push(item) {
33+
items.push(item);
34+
notify();
2935
},
3036

3137
/**
32-
* Adds a batch, before those not read yet.
38+
* Adds an item, before those not read yet.
3339
*
34-
* @param {T[]} batch
40+
* @param {T} item
3541
*/
36-
prepend(batch) {
37-
batches.unshift(batch);
38-
added.notify();
42+
prepend(item) {
43+
items.unshift(item);
44+
notify();
3945
},
4046

4147
/**
42-
* Drops the batches not read yet.
48+
* Drops the items not read yet.
4349
*/
4450
clear() {
45-
batches.length = 0;
51+
items.length = 0;
4652
},
4753

4854
/**
49-
* Ends the channel: the reader stops once it has read every batch.
55+
* Ends the channel: the reader stops once it has read every item.
5056
*/
5157
end() {
5258
ended = true;
53-
added.notify();
59+
notify();
5460
},
5561

5662
/**
57-
* Reads the batches as they are added, until the channel ends.
63+
* Reads the items as they are added, until the channel ends.
5864
*
59-
* @returns {AsyncGenerator<T[]>}
65+
* @returns {AsyncGenerator<T>}
6066
*/
6167
async *[Symbol.asyncIterator]() {
6268
for (;;) {
63-
while (batches.length === 0) {
69+
while (items.length === 0) {
6470
if (ended) {
6571
return;
6672
}
6773

68-
await added.wait();
74+
await added.promise;
6975
}
7076

71-
yield batches.shift();
77+
yield items.shift();
7278
}
7379
},
7480
};

‎packages/core/src/utils/misc.mjs‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,3 @@ export const deepMerge = (...objects) => {
7070
return result;
7171
}, {});
7272
};
73-
74-
/**
75-
* Creates a signal: waiting on it resolves the next time it is notified.
76-
*
77-
* @returns {{ wait: () => Promise<void>, notify: () => void }}
78-
*/
79-
export const createSignal = () => {
80-
let next = Promise.withResolvers();
81-
82-
return {
83-
/**
84-
* Waits for the next notification.
85-
*
86-
* @returns {Promise<void>}
87-
*/
88-
wait: () => next.promise,
89-
90-
/**
91-
* Wakes what waits.
92-
*/
93-
notify: () => {
94-
next.resolve();
95-
next = Promise.withResolvers();
96-
},
97-
};
98-
};

‎packages/react/src/html/__tests__/generate.test.mjs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ describe('web generate', () => {
514514
// input would never be built
515515
{ timeout: 10_000 },
516516
async context => {
517-
const { config, output } = await createTestConfiguration(context);
517+
const { config } = await createTestConfiguration(context);
518518
const calls = [];
519519
const bundled = Promise.withResolvers();
520520

@@ -544,12 +544,6 @@ describe('web generate', () => {
544544
'all.jsx',
545545
'index.jsx',
546546
]);
547-
548-
const html = await readFile(join(output, 'all.html'), 'utf8');
549-
550-
// In sidebar order, the index left out
551-
assert.match(html, /File system body[\s\S]*Zlib body/);
552-
assert.doesNotMatch(html, /Index body/);
553547
}
554548
);
555549

‎packages/react/src/html/generate.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const buildSite = async (input, options) => {
212212
// so the manifest is kept until then
213213
let manifest;
214214

215-
const alongside = getConfig().threads > 1;
215+
const writeWhileCompiling = getConfig().threads > 1;
216216

217217
/**
218218
* Compiles each page as it arrives, and hands it over. A function of its
@@ -226,7 +226,7 @@ const buildSite = async (input, options) => {
226226

227227
for (const item of items) {
228228
if ('manifest' in item) {
229-
if (alongside) {
229+
if (writeWhileCompiling) {
230230
startWriting(item.manifest);
231231
} else {
232232
manifest = item.manifest;

‎packages/react/src/jsx-ast/__tests__/generate.test.mjs‎

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -207,63 +207,11 @@ describe('jsx-ast generate', () => {
207207
assert.equal(whole.manifests.length, 1);
208208
});
209209

210-
it(
211-
'emits the manifest without waiting for the pages still being built',
212-
// The first page is built only once the manifest has arrived, so a
213-
// manifest that waited for it would never come
214-
{ timeout: 10_000 },
215-
async () => {
216-
await setConfig({ target: ['jsx-ast'] });
217-
218-
const gate = Promise.withResolvers();
219-
const worker = {
220-
async *stream(batches) {
221-
const all = [];
222-
223-
// Every batch is taken as it arrives
224-
for await (const items of batches) {
225-
all.push(items);
226-
}
227-
228-
for (const items of all) {
229-
// The first page takes until the test lets it finish
230-
if (items.some(({ head }) => head.api === 'fs')) {
231-
await gate.promise;
232-
}
233-
234-
yield items.map(({ head }) => ({ data: head }));
235-
}
236-
},
237-
};
238-
239-
const output = generate(
240-
streamOf([[createEntry('fs', 'File system')]]),
241-
worker
242-
);
243-
244-
const { value } = await output.next();
245-
246-
assert.deepEqual(
247-
value[0].manifest.map(({ api }) => api),
248-
['fs', '404']
249-
);
250-
251-
gate.resolve();
252-
253-
const { pages } = await collect(output);
254-
255-
assert.deepEqual(
256-
pages.map(({ data }) => data.api),
257-
['fs', '404']
258-
);
259-
}
260-
);
261-
262210
it(
263211
'emits the manifest while worker.stream holds back the next batch',
264212
// The pool only takes the next batch once there's room, and the first
265213
// page holds it until the manifest has arrived, so a manifest that waited
266-
// for the pool to take every batch would never come
214+
// for the pages, or for the pool to take every batch, would never come
267215
{ timeout: 10_000 },
268216
async () => {
269217
await setConfig({ target: ['jsx-ast'] });

0 commit comments

Comments
 (0)