Skip to content

Commit 03bd5b0

Browse files
authored
refactor: rename useAsyncIterable hook into useAsyncIter (#4)
1 parent 29729f8 commit 03bd5b0

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

.github/actions/ci-common-setup/action.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ runs:
1717

1818
- name: Get Node.js version
1919
id: node_version
20-
run: |
21-
echo "version=$(node -v)" >>$GITHUB_OUTPUT
2220
shell: bash
21+
run: echo "version=$(node -v)" >>$GITHUB_OUTPUT
2322

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

3130
- name: Install pnpm
32-
run: npm install -g ${{ steps.pkg_manager_value.outputs.value }}
3331
shell: bash
32+
run: npm install -g ${{ steps.pkg_manager_value.outputs.value }}
3433

3534
- name: Restore possibly cached dependencies
3635
id: cache-node-modules
@@ -41,5 +40,5 @@ runs:
4140

4241
- name: Install dependencies if weren't cached
4342
if: steps.cache-node-modules.outputs.cache-hit != 'true'
44-
run: pnpm install --frozen-lockfile
4543
shell: bash
44+
run: pnpm install --frozen-lockfile

spec/tests/useAsyncIterable.spec.ts renamed to spec/tests/useAsyncIter.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { it, describe, expect, afterEach, vi } from 'vitest';
22
import { cleanup as cleanupMountedReactTrees, act, renderHook } from '@testing-library/react';
3-
import { useAsyncIterable } from '../../src/index.js';
3+
import { useAsyncIter } from '../../src/index.js';
44
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';
55

66
afterEach(() => {
77
cleanupMountedReactTrees();
88
});
99

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

1414
const renderedHook = renderHook(
1515
({ value }) => {
1616
timesRerendered++;
17-
return useAsyncIterable(value);
17+
return useAsyncIter(value);
1818
},
1919
{ initialProps: { value: '' } }
2020
);
@@ -36,7 +36,7 @@ describe('`useAsyncIterable` hook', () => {
3636

3737
const renderedHook = renderHook(() => {
3838
timesRerendered++;
39-
return useAsyncIterable('a');
39+
return useAsyncIter('a');
4040
});
4141

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

5454
const renderedHook = renderHook(() => {
5555
timesRerendered++;
56-
return useAsyncIterable('a', '_');
56+
return useAsyncIter('a', '_');
5757
});
5858

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

71-
const renderedHook = renderHook(() => useAsyncIterable(channel));
71+
const renderedHook = renderHook(() => useAsyncIter(channel));
7272

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

93-
const renderedHook = renderHook(() => useAsyncIterable(channel, '_'));
93+
const renderedHook = renderHook(() => useAsyncIter(channel, '_'));
9494

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

116116
const renderedHook = renderHook(() => {
117117
timesRerendered++;
118-
return useAsyncIterable(channel);
118+
return useAsyncIter(channel);
119119
});
120120

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

145145
const renderedHook = renderHook(() => {
146146
timesRerendered++;
147-
return useAsyncIterable(emptyIter);
147+
return useAsyncIter(emptyIter);
148148
});
149149

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

165165
const renderedHook = renderHook(() => {
166166
timesRerendered++;
167-
return useAsyncIterable(emptyIter, '_');
167+
return useAsyncIter(emptyIter, '_');
168168
});
169169

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

185185
const renderedHook = renderHook(() => {
186186
timesRerendered++;
187-
return useAsyncIterable(channel);
187+
return useAsyncIter(channel);
188188
});
189189

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

218218
const renderedHook = renderHook(() => {
219219
timesRerendered++;
220-
return useAsyncIterable(erroringIter);
220+
return useAsyncIter(erroringIter);
221221
});
222222

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

241241
const renderedHook = renderHook(() => {
242242
timesRerendered++;
243-
return useAsyncIterable(erroringIter, '_');
243+
return useAsyncIter(erroringIter, '_');
244244
});
245245

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

262262
const renderedHook = renderHook(() => {
263263
timesRerendered++;
264-
return useAsyncIterable(channel);
264+
return useAsyncIter(channel);
265265
});
266266

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

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

367-
const renderedHook = renderHook(({ value }) => useAsyncIterable(value), {
367+
const renderedHook = renderHook(({ value }) => useAsyncIter(value), {
368368
initialProps: {
369369
value: (async function* () {})() as AsyncIterable<string>,
370370
},

src/Iterate/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ReactNode } from 'react';
2-
import { useAsyncIterable, type IterationResult } from '../useAsyncIterable/index.js';
2+
import { useAsyncIter, type IterationResult } from '../useAsyncIter/index.js';
33

44
export { Iterate, type IterateProps };
55

@@ -8,12 +8,12 @@ function Iterate<TVal, TInitialVal = undefined>(props: IterateProps<TVal, TIniti
88
typeof props.children === 'function'
99
? (() => {
1010
const propsBetterTyped = props as IteratePropsWithRenderFunction<TVal, TInitialVal>;
11-
const next = useAsyncIterable(propsBetterTyped.value, propsBetterTyped.initialValue);
11+
const next = useAsyncIter(propsBetterTyped.value, propsBetterTyped.initialValue);
1212
return propsBetterTyped.children(next);
1313
})()
1414
: (() => {
1515
const propsBetterTyped = props as IteratePropsWithIterableAsChildren;
16-
const next = useAsyncIterable(propsBetterTyped.children, propsBetterTyped.initialValue);
16+
const next = useAsyncIter(propsBetterTyped.children, propsBetterTyped.initialValue);
1717
return next.value;
1818
})();
1919

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useAsyncIterable, type IterationResult } from './useAsyncIterable/index.js';
1+
import { useAsyncIter, type IterationResult } from './useAsyncIter/index.js';
22
import { Iterate, type IterateProps } from './Iterate/index.js';
33

4-
export { useAsyncIterable, type IterationResult, Iterate, type IterateProps };
4+
export { useAsyncIter, type IterationResult, Iterate, type IterateProps };

src/useAsyncIterable/index.ts renamed to src/useAsyncIter/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import { isAsyncIter } from '../common/isAsyncIter.js';
44
import { useSimpleRerender } from '../common/hooks/useSimpleRerender.js';
55
import { type ExtractAsyncIterValue } from '../common/ExtractAsyncIterValue.js';
66

7-
export { useAsyncIterable, type IterationResult };
7+
export { useAsyncIter, type IterationResult };
88

99
// 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
1010

11-
function useAsyncIterable<TValue>(
12-
input: AsyncIterable<TValue>,
13-
initialValue?: undefined
14-
): IterationResult<TValue, undefined>;
11+
const useAsyncIter: {
12+
<TValue>(
13+
input: AsyncIterable<TValue>,
14+
initialValue?: undefined
15+
): IterationResult<TValue, undefined>;
1516

16-
function useAsyncIterable<TValue, TInitValue = undefined>(
17-
input: TValue,
18-
initialValue?: TInitValue
19-
): IterationResult<TValue, TInitValue>;
20-
21-
function useAsyncIterable<TValue, TInitValue = undefined>(
17+
<TValue, TInitValue = undefined>(
18+
input: TValue,
19+
initialValue?: TInitValue
20+
): IterationResult<TValue, TInitValue>;
21+
} = <TValue, TInitValue = undefined>(
2222
input: TValue,
2323
initialValue: TInitValue
24-
): IterationResult<TValue, TInitValue> {
24+
): IterationResult<TValue, TInitValue> => {
2525
const rerender = useSimpleRerender();
2626

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

103103
return stateRef.current;
104104
}
105-
}
105+
};
106106

107107
type IterationResult<TValue, TInitValue = undefined> = {
108108
/** The most recent value received */

0 commit comments

Comments
 (0)