Skip to content

Rename useAsyncIterable hook into useAsyncIter #4

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 17, 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
7 changes: 3 additions & 4 deletions .github/actions/ci-common-setup/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ runs:

- name: Get Node.js version
id: node_version
run: |
echo "version=$(node -v)" >>$GITHUB_OUTPUT
shell: bash
run: echo "version=$(node -v)" >>$GITHUB_OUTPUT

- name: Get package manager type and version from `package.json`
id: pkg_manager_value
Expand All @@ -29,8 +28,8 @@ runs:
prop_path: 'packageManager'

- name: Install pnpm
run: npm install -g ${{ steps.pkg_manager_value.outputs.value }}
shell: bash
run: npm install -g ${{ steps.pkg_manager_value.outputs.value }}

- name: Restore possibly cached dependencies
id: cache-node-modules
Expand All @@ -41,5 +40,5 @@ runs:

- name: Install dependencies if weren't cached
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: pnpm install --frozen-lockfile
shell: bash
run: pnpm install --frozen-lockfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { it, describe, expect, afterEach, vi } from 'vitest';
import { cleanup as cleanupMountedReactTrees, act, renderHook } from '@testing-library/react';
import { useAsyncIterable } from '../../src/index.js';
import { useAsyncIter } from '../../src/index.js';
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';

afterEach(() => {
cleanupMountedReactTrees();
});

describe('`useAsyncIterable` hook', () => {
describe('`useAsyncIter` hook', () => {
it('When updated with non-iterable values consecutively will render correctly', async () => {
let timesRerendered = 0;

const renderedHook = renderHook(
({ value }) => {
timesRerendered++;
return useAsyncIterable(value);
return useAsyncIter(value);
},
{ initialProps: { value: '' } }
);
Expand All @@ -36,7 +36,7 @@ describe('`useAsyncIterable` hook', () => {

const renderedHook = renderHook(() => {
timesRerendered++;
return useAsyncIterable('a');
return useAsyncIter('a');
});

expect(timesRerendered).toStrictEqual(1);
Expand All @@ -53,7 +53,7 @@ describe('`useAsyncIterable` hook', () => {

const renderedHook = renderHook(() => {
timesRerendered++;
return useAsyncIterable('a', '_');
return useAsyncIter('a', '_');
});

expect(timesRerendered).toStrictEqual(1);
Expand All @@ -68,7 +68,7 @@ describe('`useAsyncIterable` hook', () => {
it('When given an iterable that yields a value will return correct results', async () => {
const channel = new IterableChannelTestHelper<string>();

const renderedHook = renderHook(() => useAsyncIterable(channel));
const renderedHook = renderHook(() => useAsyncIter(channel));

expect(renderedHook.result.current).toStrictEqual({
value: undefined,
Expand All @@ -90,7 +90,7 @@ describe('`useAsyncIterable` hook', () => {
it('When given an iterable that yields a value in conjunction with some initial value will return correct results', async () => {
const channel = new IterableChannelTestHelper<string>();

const renderedHook = renderHook(() => useAsyncIterable(channel, '_'));
const renderedHook = renderHook(() => useAsyncIter(channel, '_'));

expect(renderedHook.result.current).toStrictEqual({
value: '_',
Expand All @@ -115,7 +115,7 @@ describe('`useAsyncIterable` hook', () => {

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

expect(timesRerendered).toStrictEqual(1);
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('`useAsyncIterable` hook', () => {

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

await act(() => {}); // To take us past the initial render and right after the first iteration
Expand All @@ -164,7 +164,7 @@ describe('`useAsyncIterable` hook', () => {

const renderedHook = renderHook(() => {
timesRerendered++;
return useAsyncIterable(emptyIter, '_');
return useAsyncIter(emptyIter, '_');
});

await act(() => {}); // To take us past the initial render and right after the first iteration
Expand All @@ -184,7 +184,7 @@ describe('`useAsyncIterable` hook', () => {

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

await act(() => channel.put('a')); // To take us past the initial render and right after the first iteration
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('`useAsyncIterable` hook', () => {

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

await act(() => {}); // To take us past the initial render and right after the first iteration
Expand All @@ -240,7 +240,7 @@ describe('`useAsyncIterable` hook', () => {

const renderedHook = renderHook(() => {
timesRerendered++;
return useAsyncIterable(erroringIter, '_');
return useAsyncIter(erroringIter, '_');
});

await act(() => {}); // To take us past the initial render and right after the first iteration
Expand All @@ -261,7 +261,7 @@ describe('`useAsyncIterable` hook', () => {

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

await act(() => channel.put('a'));
Expand Down Expand Up @@ -296,7 +296,7 @@ describe('`useAsyncIterable` hook', () => {
vi.spyOn(channel2, 'return'),
];

const renderedHook = renderHook(({ value }) => useAsyncIterable(value), {
const renderedHook = renderHook(({ value }) => useAsyncIter(value), {
initialProps: {
value: (async function* () {})() as AsyncIterable<string>,
},
Expand Down Expand Up @@ -364,7 +364,7 @@ describe('`useAsyncIterable` hook', () => {
const channel = new IterableChannelTestHelper<string>();
const channelIterCloseSpy = vi.spyOn(channel, 'return');

const renderedHook = renderHook(({ value }) => useAsyncIterable(value), {
const renderedHook = renderHook(({ value }) => useAsyncIter(value), {
initialProps: {
value: (async function* () {})() as AsyncIterable<string>,
},
Expand Down
6 changes: 3 additions & 3 deletions src/Iterate/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ReactNode } from 'react';
import { useAsyncIterable, type IterationResult } from '../useAsyncIterable/index.js';
import { useAsyncIter, type IterationResult } from '../useAsyncIter/index.js';

export { Iterate, type IterateProps };

Expand All @@ -8,12 +8,12 @@ function Iterate<TVal, TInitialVal = undefined>(props: IterateProps<TVal, TIniti
typeof props.children === 'function'
? (() => {
const propsBetterTyped = props as IteratePropsWithRenderFunction<TVal, TInitialVal>;
const next = useAsyncIterable(propsBetterTyped.value, propsBetterTyped.initialValue);
const next = useAsyncIter(propsBetterTyped.value, propsBetterTyped.initialValue);
return propsBetterTyped.children(next);
})()
: (() => {
const propsBetterTyped = props as IteratePropsWithIterableAsChildren;
const next = useAsyncIterable(propsBetterTyped.children, propsBetterTyped.initialValue);
const next = useAsyncIter(propsBetterTyped.children, propsBetterTyped.initialValue);
return next.value;
})();

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAsyncIterable, type IterationResult } from './useAsyncIterable/index.js';
import { useAsyncIter, type IterationResult } from './useAsyncIter/index.js';
import { Iterate, type IterateProps } from './Iterate/index.js';

export { useAsyncIterable, type IterationResult, Iterate, type IterateProps };
export { useAsyncIter, type IterationResult, Iterate, type IterateProps };
26 changes: 13 additions & 13 deletions src/useAsyncIterable/index.ts → src/useAsyncIter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import { isAsyncIter } from '../common/isAsyncIter.js';
import { useSimpleRerender } from '../common/hooks/useSimpleRerender.js';
import { type ExtractAsyncIterValue } from '../common/ExtractAsyncIterValue.js';

export { useAsyncIterable, type IterationResult };
export { useAsyncIter, type IterationResult };

// TODO: The initial value can be given as a function, which the internal `useState` would invoke as it's defined to do. So the typings should take into account it possibly being a function and if that's the case then to extract its return type instead of using the function type itself

function useAsyncIterable<TValue>(
input: AsyncIterable<TValue>,
initialValue?: undefined
): IterationResult<TValue, undefined>;
const useAsyncIter: {
<TValue>(
input: AsyncIterable<TValue>,
initialValue?: undefined
): IterationResult<TValue, undefined>;

function useAsyncIterable<TValue, TInitValue = undefined>(
input: TValue,
initialValue?: TInitValue
): IterationResult<TValue, TInitValue>;

function useAsyncIterable<TValue, TInitValue = undefined>(
<TValue, TInitValue = undefined>(
input: TValue,
initialValue?: TInitValue
): IterationResult<TValue, TInitValue>;
} = <TValue, TInitValue = undefined>(
input: TValue,
initialValue: TInitValue
): IterationResult<TValue, TInitValue> {
): IterationResult<TValue, TInitValue> => {
const rerender = useSimpleRerender();

const stateRef = useRef<IterationResult<TValue, TInitValue>>({
Expand Down Expand Up @@ -102,7 +102,7 @@ function useAsyncIterable<TValue, TInitValue = undefined>(

return stateRef.current;
}
}
};

type IterationResult<TValue, TInitValue = undefined> = {
/** The most recent value received */
Expand Down
Loading