Skip to content

Fix formatted iterable index argument not accounting for a current-value-holding-iterable correctly #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
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
2 changes: 1 addition & 1 deletion spec/tests/Iterate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ describe('`Iterate` component', () => {
}

expect(renderFn.mock.calls.flat()).toStrictEqual(
['a_current', 'a', 'b_current_formatted_0', 'b_formatted_0'].map(value => ({
['a_current', 'a', 'b_current_formatted_0', 'b_formatted_1'].map(value => ({
value,
pendingFirst: false,
done: false,
Expand Down
2 changes: 1 addition & 1 deletion spec/tests/IterateMulti.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ describe('`IterateMulti` hook', () => {
{ value: 'a', pendingFirst: false, done: false, error: undefined },
],
[
{ value: 'b_formatted_0', pendingFirst: false, done: false, error: undefined },
{ value: 'b_formatted_1', pendingFirst: false, done: false, error: undefined },
{ value: 'a', pendingFirst: false, done: false, error: undefined },
],
]);
Expand Down
70 changes: 47 additions & 23 deletions spec/tests/useAsyncIter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ describe('`useAsyncIter` hook', () => {
gray(
'When given an iterable with a `.value.current` property at any point, uses that as the current value and skips the pending stage'
),
() =>
() => {
([{ initialValue: undefined }, { initialValue: '_' }] as const).forEach(
({ initialValue }) => {
it(
Expand All @@ -454,28 +454,21 @@ describe('`useAsyncIter` hook', () => {

const results: any[] = [];

for (const run of [
for (const next of [
() => renderedHook.rerender({ value: channel1 }),
() => channel1.put('a'),
() =>
act(() =>
renderedHook.rerender({
value: channel1,
})
),
() => act(() => channel1.put('a')),
() =>
act(() =>
renderedHook.rerender({
value: iterateFormatted(channel2, (val, i) => `${val}_formatted_${i}`),
})
),
() => act(() => channel2.put('b')),
renderedHook.rerender({
value: iterateFormatted(channel2, (val, i) => `${val}_formatted_${i}`),
}),
() => channel2.put('b'),
]) {
await run();
await act(next);
results.push(renderedHook.result.current);
}

expect(results).toStrictEqual(
['a_current', 'a', 'b_current_formatted_0', 'b_formatted_0'].map(value => ({
['a_current', 'a', 'b_current_formatted_0', 'b_formatted_1'].map(value => ({
value,
pendingFirst: false,
done: false,
Expand All @@ -485,7 +478,38 @@ describe('`useAsyncIter` hook', () => {
}
);
}
)
);

it(gray('with a formatted iterable'), async () => {
let timesRerendered = 0;
const channel = Object.assign(new IteratorChannelTestHelper<string>(), {
value: { current: 'a_current' },
});

const renderedHook = await act(() =>
renderHook(() => {
timesRerendered++;
return useAsyncIter(iterateFormatted(channel, (val, i) => `${val}_formatted_${i}`));
})
);
expect(timesRerendered).toStrictEqual(1);
expect(renderedHook.result.current).toStrictEqual({
value: 'a_current_formatted_0',
pendingFirst: false,
done: false,
error: undefined,
});

await act(() => channel.put('a_next'));
expect(timesRerendered).toStrictEqual(2);
expect(renderedHook.result.current).toStrictEqual({
value: 'a_next_formatted_1',
pendingFirst: false,
done: false,
error: undefined,
});
});
}
);

it(gray('When unmounted will close the last active iterator it held'), async () => {
Expand Down Expand Up @@ -627,19 +651,19 @@ describe('`useAsyncIter` hook', () => {

const renderedHook = await act(() =>
renderHook(
({ formatInto }) => {
({ formatTo }) => {
timesRerendered++;
return useAsyncIter(iterateFormatted(channel, _ => formatInto));
return useAsyncIter(iterateFormatted(channel, _ => formatTo));
},
{
initialProps: { formatInto: '' as string | null | undefined },
initialProps: { formatTo: '' as string | null | undefined },
}
)
);

await act(() => {
channel.put('a');
renderedHook.rerender({ formatInto: null });
renderedHook.rerender({ formatTo: null });
});
expect(timesRerendered).toStrictEqual(3);
expect(renderedHook.result.current).toStrictEqual({
Expand All @@ -651,7 +675,7 @@ describe('`useAsyncIter` hook', () => {

await act(() => {
channel.put('b');
renderedHook.rerender({ formatInto: undefined });
renderedHook.rerender({ formatTo: undefined });
});
expect(timesRerendered).toStrictEqual(5);
expect(renderedHook.result.current).toStrictEqual({
Expand Down
2 changes: 1 addition & 1 deletion spec/tests/useAsyncIterMulti.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ describe('`useAsyncIterMulti` hook', () => {
{ value: 'a', pendingFirst: false, done: false, error: undefined },
],
[
{ value: 'b_formatted_0', pendingFirst: false, done: false, error: undefined },
{ value: 'b_formatted_1', pendingFirst: false, done: false, error: undefined },
{ value: 'a', pendingFirst: false, done: false, error: undefined },
],
]);
Expand Down
22 changes: 13 additions & 9 deletions src/common/useAsyncItersImperatively/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,20 @@ const useAsyncItersImperatively: {
return existingIterState.currState;
}

const formattedIter: AsyncIterable<unknown> = (() => {
let iterationIdx = 0;
return asyncIterSyncMap(baseIter, value => iterState.formatFn(value, iterationIdx++));
})();

const inputWithMaybeCurrentValue = input as typeof input & {
value?: AsyncIterableSubject<unknown>['value'];
};

let pendingFirst;
let iterationIdx: number;
let pendingFirst: boolean;
let startingValue;

if (inputWithMaybeCurrentValue.value) {
iterationIdx = 1; // If source has a current value, it should have been the "first iteration" already, so in that case the right up next one here is *the second* already (index of 1)
pendingFirst = false;
startingValue = inputWithMaybeCurrentValue.value.current;
} else {
iterationIdx = 0;
pendingFirst = true;
startingValue =
i < ref.current.currResults.length
Expand All @@ -147,11 +145,17 @@ const useAsyncItersImperatively: {
);
}

const formattedIter: AsyncIterable<unknown> = asyncIterSyncMap(baseIter, value =>
iterState.formatFn(value, iterationIdx++)
);

const destroyFn = iterateAsyncIterWithCallbacks(formattedIter, startingValue, next => {
iterState.currState = { pendingFirst: false, ...next };
const newPrevResults = ref.current.currResults.slice(0); // Using `.slice(0)` in attempt to copy the array faster than `[...ref.current.currResults]` would
newPrevResults[i] = iterState.currState;
ref.current.currResults = newPrevResults as typeof ref.current.currResults;
ref.current.currResults = (() => {
const newResults = ref.current.currResults.slice(0); // Using `.slice(0)` in attempt to copy the array faster than `[...ref.current.currResults]` would
newResults[i] = iterState.currState;
return newResults as typeof ref.current.currResults;
})();
onYieldCb(ref.current.currResults);
});

Expand Down
36 changes: 19 additions & 17 deletions src/useAsyncIter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../common/ReactAsyncIterable.js';
import { iterateAsyncIterWithCallbacks } from '../common/iterateAsyncIterWithCallbacks.js';
import { callOrReturn } from '../common/callOrReturn.js';
import { asyncIterSyncMap } from '../common/asyncIterSyncMap.js';
import { type Iterate } from '../Iterate/index.js'; // eslint-disable-line @typescript-eslint/no-unused-vars
import { type iterateFormatted } from '../iterateFormatted/index.js'; // eslint-disable-line @typescript-eslint/no-unused-vars

Expand Down Expand Up @@ -148,19 +149,19 @@ const useAsyncIter: {
const iterSourceRefToUse =
latestInputRef.current[reactAsyncIterSpecialInfoSymbol]?.origSource ?? latestInputRef.current;

useMemo((): void => {
const latestInputRefCurrent = latestInputRef.current!;
const latestInputRefCurrent = latestInputRef.current!;

let value;
useMemo((): void => {
let pendingFirst;
let value;

if (latestInputRefCurrent.value) {
value = latestInputRefCurrent.value.current;
pendingFirst = false;
value = latestInputRefCurrent.value.current;
} else {
const prevSourceLastestVal = stateRef.current.value;
value = prevSourceLastestVal;
pendingFirst = true;
value = prevSourceLastestVal;
}

stateRef.current = {
Expand All @@ -172,22 +173,23 @@ const useAsyncIter: {
}, [iterSourceRefToUse]);

useEffect(() => {
let iterationIdx = 0;
const formattedIter = (() => {
let iterationIdx = latestInputRefCurrent.value ? 1 : 0; // If source has a current value, it should have been the "first iteration" already, so in that case the right up next one here is *the second* already (index of 1)

return iterateAsyncIterWithCallbacks(iterSourceRefToUse, stateRef.current.value, next => {
const possibleGivenFormatFn =
latestInputRef.current?.[reactAsyncIterSpecialInfoSymbol]?.formatFn;
return asyncIterSyncMap(iterSourceRefToUse, value => {
const possibleGivenFormatFn =
latestInputRef.current?.[reactAsyncIterSpecialInfoSymbol]?.formatFn;

const formattedValue = possibleGivenFormatFn
? possibleGivenFormatFn(next.value, iterationIdx++)
: next.value;
const formattedValue = possibleGivenFormatFn
? possibleGivenFormatFn(value, iterationIdx++)
: value;

stateRef.current = {
...next,
pendingFirst: false,
value: formattedValue,
};
return formattedValue;
});
})();

return iterateAsyncIterWithCallbacks(formattedIter, stateRef.current.value, next => {
stateRef.current = { ...next, pendingFirst: false };
rerender();
});
}, [iterSourceRefToUse]);
Expand Down