Skip to content

Commit 8c7b56f

Browse files
authored
Merge branch 'master' into docs-edit
2 parents 5151c74 + 5dd5ba6 commit 8c7b56f

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/useAsyncIterState/IterableChannel.ts renamed to src/common/AsyncIterableChannel.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { type MutableRefObject } from 'react';
2-
import { promiseWithResolvers } from '../common/promiseWithResolvers.js';
3-
import { callWithArgsOrReturn } from '../common/callWithArgsOrReturn.js';
2+
import { type MaybeFunction } from './MaybeFunction.js';
3+
import { promiseWithResolvers } from './promiseWithResolvers.js';
4+
import { callWithArgsOrReturn } from './callWithArgsOrReturn.js';
45

5-
export { IterableChannel, type AsyncIterableSubject };
6+
export { AsyncIterableChannel, type AsyncIterableSubject };
67

7-
class IterableChannel<T, TInit = T> {
8+
class AsyncIterableChannel<T, TInit = T> {
89
#isClosed = false;
910
#nextIteration = promiseWithResolvers<IteratorResult<T, void>>();
1011
#currentValue: T | TInit;
@@ -13,23 +14,24 @@ class IterableChannel<T, TInit = T> {
1314
this.#currentValue = initialValue;
1415
}
1516

16-
put(update: T | ((prevState: T | TInit) => T)): void {
17-
if (!this.#isClosed) {
18-
(async () => {
19-
this.#currentValue = callWithArgsOrReturn(update, this.#currentValue);
20-
await undefined; // Deferring to the next microtick so that an attempt to pull the a value before making multiple rapid synchronous calls to `put()` will make that pull ultimately yield only the last value that was put - instead of the first one as were if this otherwise wasn't deferred.
21-
this.#nextIteration.resolve({ done: false, value: this.#currentValue });
22-
this.#nextIteration = promiseWithResolvers();
23-
})();
17+
put(update: MaybeFunction<T, [prevState: T | TInit]>): void {
18+
if (this.#isClosed) {
19+
return;
2420
}
21+
(async () => {
22+
this.#currentValue = callWithArgsOrReturn(update, this.#currentValue);
23+
await undefined; // Deferring to the next microtick so that an attempt to pull the a value before making multiple rapid synchronous calls to `put()` will make that pull ultimately yield only the last value that was put - instead of the first one as were if this otherwise wasn't deferred.
24+
this.#nextIteration.resolve({ done: false, value: this.#currentValue });
25+
this.#nextIteration = promiseWithResolvers();
26+
})();
2527
}
2628

2729
close(): void {
2830
this.#isClosed = true;
2931
this.#nextIteration.resolve({ done: true, value: undefined });
3032
}
3133

32-
values: AsyncIterableSubject<T, TInit> = {
34+
out: AsyncIterableSubject<T, TInit> = {
3335
value: (() => {
3436
const self = this;
3537
return {

src/useAsyncIterState/index.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useEffect } from 'react';
22
import { callOrReturn } from '../common/callOrReturn.js';
33
import { useRefWithInitialValue } from '../common/hooks/useRefWithInitialValue.js';
4+
import { type MaybeFunction } from '../common/MaybeFunction.js';
45
import { type Iterate } from '../Iterate/index.js'; // eslint-disable-line @typescript-eslint/no-unused-vars
5-
import { IterableChannel, type AsyncIterableSubject } from './IterableChannel.js';
6+
import { AsyncIterableChannel, type AsyncIterableSubject } from '../common/AsyncIterableChannel.js';
67

78
export { useAsyncIterState, type AsyncIterStateResult, type AsyncIterableSubject };
89

@@ -99,25 +100,25 @@ export { useAsyncIterState, type AsyncIterStateResult, type AsyncIterableSubject
99100
function useAsyncIterState<TVal>(): AsyncIterStateResult<TVal, undefined>;
100101

101102
function useAsyncIterState<TVal>(
102-
initialValue: TVal | (() => TVal)
103+
initialValue: MaybeFunction<TVal>
103104
): AsyncIterStateResult<TVal, TVal>;
104105

105106
function useAsyncIterState<TVal, TInitVal = undefined>(
106-
initialValue: TInitVal | (() => TInitVal)
107+
initialValue: MaybeFunction<TInitVal>
107108
): AsyncIterStateResult<TVal, TInitVal>;
108109

109110
function useAsyncIterState<TVal, TInitVal>(
110-
initialValue?: TInitVal | (() => TInitVal)
111+
initialValue?: MaybeFunction<TInitVal>
111112
): AsyncIterStateResult<TVal, TInitVal> {
112113
const ref = useRefWithInitialValue<{
113-
channel: IterableChannel<TVal, TInitVal>;
114+
channel: AsyncIterableChannel<TVal, TInitVal>;
114115
result: AsyncIterStateResult<TVal, TInitVal>;
115116
}>(() => {
116117
const initialValueCalced = callOrReturn(initialValue) as TInitVal;
117-
const channel = new IterableChannel<TVal, TInitVal>(initialValueCalced);
118+
const channel = new AsyncIterableChannel<TVal, TInitVal>(initialValueCalced);
118119
return {
119120
channel,
120-
result: [channel.values, newVal => channel.put(newVal)],
121+
result: [channel.out, newVal => channel.put(newVal)],
121122
};
122123
});
123124

0 commit comments

Comments
 (0)