All files / remote-iterables/src ref-reader.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 5/5
100% Lines 7/7

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                2x         10x 12x 2x 2x 1x   10x    
import { E } from '@endo/eventual-send';
import type { ERef } from '@endo/eventual-send';
/**
 * Make an iterator from a remote reference.
 *
 * @param iteratorRef - The iterator ref to make an iterator from.
 * @returns An iterator that wraps the iterator ref.
 */
export const makeRefIterator = <TValue, TReturn, TNext>(
  iteratorRef: ERef<AsyncIterator<TValue, TReturn, TNext>>,
): AsyncIterator<TValue, TReturn, TNext> & {
  [Symbol.asyncIterator]: () => AsyncIterator<TValue, TReturn, TNext>;
} => {
  const iterator = {
    next: async (...args: [TNext] | []) => E(iteratorRef).next(...args),
    return: async (...args: [TReturn] | []) => E(iteratorRef).return(...args),
    throw: async (error: unknown) => E(iteratorRef).throw(error),
    [Symbol.asyncIterator]: () => iterator,
  };
  return iterator;
};