Skip to content

Fix yielding consecutive identical values causes unnecessary re-rendering #21

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
Dec 27, 2024
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
36 changes: 36 additions & 0 deletions spec/tests/Iterate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,42 @@ describe('`Iterate` component', () => {
);
}
);

it(
gray(
'When given iterable yields consecutive identical values the hook will not consequently re-render'
),
async () => {
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
const channel = new IterableChannelTestHelper<string>();

const rendered = render(
<Iterate value={channel}>
{next => {
timesRerendered++;
lastRenderFnInput = next;
return <div id="test-created-elem">Render count: {timesRerendered}</div>;
}}
</Iterate>
);

for (let i = 0; i < 3; ++i) {
await act(() => channel.put('a'));
}

expect(timesRerendered).toStrictEqual(2);
expect(lastRenderFnInput).toStrictEqual({
value: 'a',
pendingFirst: false,
done: false,
error: undefined,
});
expect(rendered.container.innerHTML).toStrictEqual(
'<div id="test-created-elem">Render count: 2</div>'
);
}
);
});

const simulatedError = new Error('🚨 Simulated Error 🚨');
27 changes: 27 additions & 0 deletions spec/tests/useAsyncIter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,33 @@ describe('`useAsyncIter` hook', () => {
});
}
);

it(
gray(
'When given iterable yields consecutive identical values the hook will not consequently re-render'
),
async () => {
let timesRerendered = 0;
const channel = new IterableChannelTestHelper<string>();

const renderedHook = renderHook(() => {
timesRerendered++;
return useAsyncIter(channel);
});

for (let i = 0; i < 3; ++i) {
await act(() => channel.put('a'));
}

expect(timesRerendered).toStrictEqual(2);
expect(renderedHook.result.current).toStrictEqual({
value: 'a',
pendingFirst: false,
done: false,
error: undefined,
});
}
);
});

const simulatedError = new Error('🚨 Simulated Error 🚨');
16 changes: 9 additions & 7 deletions src/useAsyncIter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,15 @@ const useAsyncIter: {
iterationIdx++
) ?? (value as ExtractAsyncIterValue<TVal>);

stateRef.current = {
value: formattedValue,
pendingFirst: false,
done: false,
error: undefined,
};
rerender();
if (!Object.is(formattedValue, stateRef.current.value)) {
stateRef.current = {
value: formattedValue,
pendingFirst: false,
done: false,
error: undefined,
};
rerender();
}
}
}
if (!iteratorClosedByConsumer) {
Expand Down
Loading