UNPKG

react-async-iterators

Version:

The magic of JavaScript async iterators in React ⛓️ 🧬 🔃

31 lines (30 loc) 1.45 kB
import { type MaybeFunction } from './MaybeFunction.js'; import { type AsyncIterableSubject } from '../AsyncIterableSubject/index.js'; export { AsyncIterableChannel, type AsyncIterableChannelSubject }; declare class AsyncIterableChannel<T, TInit = T> { #private; constructor(initialValue: TInit); put(update: MaybeFunction<T, [prevState: T | TInit]>): void; close(): void; out: AsyncIterableChannelSubject<T, TInit>; } /** * A stateful async iterable which will yield every updated value following an update. Includes a * `.value.current` property which shows the current up to date state value. * * This is a shared async iterable - all iterators obtained from it share the same source values, * meaning that multiple iterators can be consumed (iterated) simultaneously and each one would pick up * the same values as others the moment they were generated through state updates. */ type AsyncIterableChannelSubject<T, TCurrVal = T> = { value: AsyncIterableSubject<T, TCurrVal>['value']; /** * Returns an async iterator to iterate over. All iterators returned by this share the same source * values - they can be iterated by multiple consumers simultaneously and each would pick up the * same values as others the moment they were generated. */ [Symbol.asyncIterator](): { next(): Promise<IteratorResult<T, void>>; return(): Promise<IteratorReturnResult<void>>; }; };