Skip to content

Commit 7a3ad03

Browse files
committed
test AsyncIterator
1 parent e52e390 commit 7a3ad03

File tree

2 files changed

+124
-28
lines changed

2 files changed

+124
-28
lines changed

tests/docstrings_examples/DocTest.res

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -327,23 +327,90 @@ let main = async () => {
327327

328328
let batchSize = OS.cpus()->Array.length
329329

330-
let compilationResults =
331-
(await modules
332-
->chunkArray(batchSize)
333-
->Array.map(async arrExample => {
334-
await arrExample
335-
->Array.map(async example => {
336-
let id = example.id->String.replaceAll(".", "__")
337-
let rescriptCode = example->getCodeBlocks
338-
let jsCode = await compileTest(~id, ~code=rescriptCode)
339-
(example, (rescriptCode, jsCode))
340-
})
341-
->Promise.all
342-
})
343-
->Promise.all)
344-
->Array.flat
330+
let chuncks = modules->chunkArray(batchSize)
331+
332+
let context = ref(0)
333+
334+
let asyncIterator = AsyncIterator.make(async () => {
335+
let currentValue = context.contents
336+
// Increment current value
337+
context := currentValue + 1
338+
339+
{
340+
AsyncIterator.value: Some(currentValue),
341+
done: currentValue == Array.length(chuncks) - 1,
342+
}
343+
})
344+
345+
let result = []
346+
// await asyncIterator->AsyncIterator.forEach(async value =>
347+
// switch value {
348+
// | Some(value) =>
349+
// let c = Array.getUnsafe(chuncks, value)
350+
//
351+
// let a = await c->Array.map(async example => {
352+
// let id = example.id->String.replaceAll(".", "__")
353+
// let rescriptCode = example->getCodeBlocks
354+
// let jsCode = await compileTest(~id, ~code=rescriptCode)
355+
// (example, (rescriptCode, jsCode))
356+
// })->Promise.all
357+
//
358+
// Array.push(result, a)->ignore
359+
//
360+
// | None => ()
361+
// }
362+
// )
363+
let processMyAsyncIterator = async () => {
364+
// ReScript doesn't have `for ... of` loops, but it's easy to mimic using a while loop.
365+
let break = ref(false)
366+
367+
while !break.contents {
368+
// Await the next iterator value
369+
let {value, done} = await asyncIterator->AsyncIterator.next
370+
371+
// Exit the while loop if the iterator says it's done
372+
break := done
373+
374+
// This will log the (int) value of the current async iteration, if a value was returned.
375+
switch value {
376+
| Some(index) =>
377+
let c = Array.getUnsafe(chuncks, index)
378+
379+
let a =
380+
await c
381+
->Array.map(async example => {
382+
let id = example.id->String.replaceAll(".", "__")
383+
let rescriptCode = example->getCodeBlocks
384+
let jsCode = await compileTest(~id, ~code=rescriptCode)
385+
(example, (rescriptCode, jsCode))
386+
})
387+
->Promise.all
388+
389+
Array.push(result, a)
390+
391+
| None => ()
392+
}
393+
}
394+
}
395+
396+
processMyAsyncIterator()->ignore
345397

346-
// let _ = c > 1
398+
let compilationResults = result->Array.flat
399+
400+
// let compilationResults =
401+
// (await chuncks
402+
// ->Array.map(async arrExample => {
403+
// await arrExample
404+
// ->Array.map(async example => {
405+
// let id = example.id->String.replaceAll(".", "__")
406+
// let rescriptCode = example->getCodeBlocks
407+
// let jsCode = await compileTest(~id, ~code=rescriptCode)
408+
// (example, (rescriptCode, jsCode))
409+
// })
410+
// ->Promise.all
411+
// })
412+
// ->Promise.all)
413+
// ->Array.flat
347414

348415
// let compilationResults =
349416
// await modules

tests/docstrings_examples/DocTest.res.mjs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Belt_List from "rescript/lib/es6/Belt_List.js";
1111
import * as Nodeutil from "node:util";
1212
import * as Belt_Array from "rescript/lib/es6/Belt_Array.js";
1313
import * as Pervasives from "rescript/lib/es6/Pervasives.js";
14+
import * as $$AsyncIterator from "rescript/lib/es6/AsyncIterator.js";
1415
import * as Child_process from "child_process";
1516
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
1617
import * as Primitive_exceptions from "rescript/lib/es6/Primitive_exceptions.js";
@@ -362,18 +363,46 @@ async function main() {
362363
}
363364
}).map(f => getExamples(extractDocFromFile(Path.join("runtime", f)))).flat();
364365
let batchSize = Os.cpus().length;
365-
let compilationResults = (await Promise.all(chunkArray(modules, batchSize).map(async arrExample => await Promise.all(arrExample.map(async example => {
366-
let id = example.id.replaceAll(".", "__");
367-
let rescriptCode = getCodeBlocks(example);
368-
let jsCode = await compileTest(id, rescriptCode);
369-
return [
370-
example,
371-
[
372-
rescriptCode,
373-
jsCode
374-
]
375-
];
376-
}))))).flat();
366+
let chuncks = chunkArray(modules, batchSize);
367+
let context = {
368+
contents: 0
369+
};
370+
let asyncIterator = $$AsyncIterator.make(async () => {
371+
let currentValue = context.contents;
372+
context.contents = currentValue + 1 | 0;
373+
return {
374+
done: currentValue === (chuncks.length - 1 | 0),
375+
value: currentValue
376+
};
377+
});
378+
let result = [];
379+
let processMyAsyncIterator = async () => {
380+
let $$break = false;
381+
while (!$$break) {
382+
let match = await asyncIterator.next();
383+
let value = match.value;
384+
$$break = match.done;
385+
if (value !== undefined) {
386+
let c = chuncks[value];
387+
let a = await Promise.all(c.map(async example => {
388+
let id = example.id.replaceAll(".", "__");
389+
let rescriptCode = getCodeBlocks(example);
390+
let jsCode = await compileTest(id, rescriptCode);
391+
return [
392+
example,
393+
[
394+
rescriptCode,
395+
jsCode
396+
]
397+
];
398+
}));
399+
result.push(a);
400+
}
401+
402+
};
403+
};
404+
processMyAsyncIterator();
405+
let compilationResults = result.flat();
377406
let match = $$Array.reduce(compilationResults, [
378407
[],
379408
[]

0 commit comments

Comments
 (0)