All files / remote-iterables/src exo-generator.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                                  2x 3x       3x 3x 1x   2x 1x 3x    
import { E } from '@endo/eventual-send';
import { makePipe } from '@endo/stream';
import type { Writer, Reader } from '@endo/stream';
 
// This type is used in the docs.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { makeEventualIterator } from './eventual-iterator.ts';
import { makeIteratorRef } from './reader-ref.ts';
 
/**
 * Make a remotable generator. Intended to be used in conjunction with
 * {@link makeEventualIterator}. This is the producing end of the pair.
 *
 * @param generator - The generator to make remotable.
 * @returns A remotable reference to the generator.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const makeExoGenerator = <Item>(generator: AsyncGenerator<Item>) => {
  const [writer, reader] = makePipe<Item>() as unknown as [
    Writer<Item>,
    Reader<Item>,
  ];
  (async () => {
    for await (const value of generator) {
      await E(writer).next(value);
    }
    await E(writer).return(undefined);
  })().catch(async (error) => await E(writer).throw(error));
  return makeIteratorRef<Item>(reader);
};