diff --git a/spec/tests/Iterate.spec.tsx b/spec/tests/Iterate.spec.tsx index cc4f67c..583edac 100644 --- a/spec/tests/Iterate.spec.tsx +++ b/spec/tests/Iterate.spec.tsx @@ -138,7 +138,7 @@ describe('`Iterate` component', () => { it(gray('When given an iterable that yields a value will render correctly'), async () => { const channel = new IterableChannelTestHelper(); let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const rendered = render( @@ -223,7 +223,7 @@ describe('`Iterate` component', () => { it(gray('When given an iterable that yields multiple values will render correctly'), async () => { const channel = new IterableChannelTestHelper(); let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const rendered = render( @@ -267,7 +267,7 @@ describe('`Iterate` component', () => { async () => { const emptyIter = (async function* () {})(); let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const rendered = render( @@ -333,7 +333,7 @@ describe('`Iterate` component', () => { async () => { const channel = new IterableChannelTestHelper(); let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const rendered = render( @@ -380,7 +380,7 @@ describe('`Iterate` component', () => { throw simulatedError; })(); let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const rendered = render( @@ -448,7 +448,7 @@ describe('`Iterate` component', () => { async () => { const channel = new IterableChannelTestHelper(); let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const simulatedErr = new Error('...'); @@ -495,7 +495,7 @@ describe('`Iterate` component', () => { "When consecutively updated with new iterables will close the previous one's iterator every time and render accordingly" ), async () => { - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const [channel1, channel2] = [ new IterableChannelTestHelper(), @@ -585,7 +585,7 @@ describe('`Iterate` component', () => { ); it(gray('When unmounted will close the last active iterator it held'), async () => { - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const channel = new IterableChannelTestHelper(); const channelReturnSpy = vi.spyOn(channel, 'return'); @@ -641,7 +641,7 @@ describe('`Iterate` component', () => { yield* ['a', 'b', 'c']; })(); let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const rendered = render( @@ -674,7 +674,7 @@ describe('`Iterate` component', () => { ), async () => { let timesRerendered = 0; - let lastRenderFnInput: undefined | IterationResult; + let lastRenderFnInput: undefined | IterationResult; const channel = new IterableChannelTestHelper(); const rendered = render( diff --git a/src/Iterate/index.tsx b/src/Iterate/index.tsx index 0ef1d15..92c79e5 100644 --- a/src/Iterate/index.tsx +++ b/src/Iterate/index.tsx @@ -107,7 +107,10 @@ function Iterate(props: IterateProps { const propsBetterTyped = props as IteratePropsWithRenderFunction; - const next = useAsyncIter(propsBetterTyped.value, propsBetterTyped.initialValue); + const next = useAsyncIter( + propsBetterTyped.value, + propsBetterTyped.initialValue as TInitialVal + ); return propsBetterTyped.children(next); })() : (() => { diff --git a/src/useAsyncIter/index.ts b/src/useAsyncIter/index.ts index 7fba0cf..bca5eb9 100644 --- a/src/useAsyncIter/index.ts +++ b/src/useAsyncIter/index.ts @@ -99,8 +99,8 @@ export { useAsyncIter, type IterationResult }; * ``` */ const useAsyncIter: { - (input: TVal, initialVal?: undefined): IterationResult; - (input: TVal, initialVal?: TInitVal): IterationResult; + (input: TVal, initialVal?: undefined): IterationResult; + (input: TVal, initialVal: TInitVal): IterationResult; } = < TVal extends | undefined @@ -120,7 +120,7 @@ const useAsyncIter: { const rerender = useSimpleRerender(); const stateRef = useRef>({ - value: initialVal, + value: initialVal as any, pendingFirst: true, done: false, error: undefined, @@ -225,12 +225,12 @@ const useAsyncIter: { type IterationResult = { /** * The most recent value received from the async iterable iteration, starting as {@link TInitVal}. - * If the source was instead a plain value, it will simply be it. + * If the source was a plain value instead, it will simply be it, ignoring any {@link TInitVal}. * - * Starting to iterate a new async iterable at any future point on itself doesn't reset this; - * only some newly resolved next value will. + * When the source iterable changes and an iteration restarts with a new iterable, the same last + * `value` is carried over and reflected until the new iterable resolves its first value. * */ - value: ExtractAsyncIterValue | TInitVal; + value: TVal extends AsyncIterable ? J | TInitVal : TVal; /** * Indicates whether the iterated async iterable is still pending its own first value to be