Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/AsyncIterableSubject/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export { type AsyncIterableSubject };

/**
* Represents a concept of an async iterable object with a notion of a current value, which is
* publicly readable via a property.
*/
type AsyncIterableSubject<T, TCurrVal = T> = AsyncIterable<T> & {
/**
* A React Ref-like object whose inner `current` property shows the most up to date state value.
*/

value: {
readonly current?: T | TCurrVal;
};
};
13 changes: 4 additions & 9 deletions src/common/AsyncIterableChannel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type MutableRefObject } from 'react';
import { type MaybeFunction } from './MaybeFunction.js';
import { type AsyncIterableSubject } from '../AsyncIterableSubject/index.js';
import { promiseWithResolvers } from './promiseWithResolvers.js';
import { callWithArgsOrReturn } from './callWithArgsOrReturn.js';

export { AsyncIterableChannel, type AsyncIterableSubject };
export { AsyncIterableChannel, type AsyncIterableChannelSubject };

class AsyncIterableChannel<T, TInit = T> {
#isClosed = false;
Expand Down Expand Up @@ -31,7 +31,7 @@ class AsyncIterableChannel<T, TInit = T> {
this.#nextIteration.resolve({ done: true, value: undefined });
}

out: AsyncIterableSubject<T, TInit> = {
out: AsyncIterableChannelSubject<T, TInit> = {
value: (() => {
const self = this;
return {
Expand Down Expand Up @@ -64,12 +64,7 @@ class AsyncIterableChannel<T, TInit = T> {
* 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 AsyncIterableSubject<T, TInit> = {
/**
* A React Ref-like object whose inner `current` property shows the most up to date state value.
*/
value: Readonly<MutableRefObject<T | TInit>>;

type AsyncIterableChannelSubject<T, TCurrVal = T> = AsyncIterableSubject<T, TCurrVal> & {
/**
* 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
Expand Down
2 changes: 1 addition & 1 deletion src/common/MaybeFunction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { MaybeFunction };
export { type MaybeFunction };

type MaybeFunction<T, TPossibleArgs extends unknown[] = []> = T | ((...args: TPossibleArgs) => T);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { iterateFormatted } from './iterateFormatted/index.js';
import { useAsyncIterState, type AsyncIterStateResult } from './useAsyncIterState/index.js';
import { type MaybeAsyncIterable } from './MaybeAsyncIterable/index.js';
import { type ReactAsyncIterable } from './common/ReactAsyncIterable.js';
import { type AsyncIterableSubject } from './AsyncIterableSubject/index.js';

export {
useAsyncIter,
Expand All @@ -23,6 +24,7 @@ export {
type AsyncIterStateResult,
type MaybeAsyncIterable,
type ReactAsyncIterable,
type AsyncIterableSubject,

/**
* @deprecated use {@link ReactAsyncIterable `ReactAsyncIterable`} instead.
Expand Down
11 changes: 7 additions & 4 deletions src/useAsyncIterState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { callOrReturn } from '../common/callOrReturn.js';
import { useRefWithInitialValue } from '../common/hooks/useRefWithInitialValue.js';
import { type MaybeFunction } from '../common/MaybeFunction.js';
import { type Iterate } from '../Iterate/index.js'; // eslint-disable-line @typescript-eslint/no-unused-vars
import { AsyncIterableChannel, type AsyncIterableSubject } from '../common/AsyncIterableChannel.js';
import {
AsyncIterableChannel,
type AsyncIterableChannelSubject,
} from '../common/AsyncIterableChannel.js';

export { useAsyncIterState, type AsyncIterStateResult, type AsyncIterableSubject };
export { useAsyncIterState, type AsyncIterStateResult, type AsyncIterableChannelSubject };

/**
* Basically like {@link https://react.dev/reference/react/useState `React.useState`}, only that the value
Expand Down Expand Up @@ -148,11 +151,11 @@ type AsyncIterStateResult<TVal, TInitVal> = [
* meaning multiple iterators can be consumed (iterated) simultaneously, each one picking up the
* same values as others the moment they were generated through state updates.
*/
values: AsyncIterableSubject<TVal, TInitVal>,
values: AsyncIterableChannelSubject<TVal, TInitVal>,

/**
* A function which updates the state, causing the paired async iterable to yield the updated state
* value and immediately sets its `.current.value` property to the latest state.
*/
setValue: (update: TVal | ((prevState: TVal | TInitVal) => TVal)) => void,
setValue: (update: MaybeFunction<TVal, [prevState: TVal | TInitVal]>) => void,
];
Loading