Skip to content

Commit 20af9b0

Browse files
authored
refactor: various misc refactorings (#26)
1 parent dd06927 commit 20af9b0

File tree

7 files changed

+43
-36
lines changed

7 files changed

+43
-36
lines changed

spec/tests/Iterate.spec.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { it, describe, expect, afterEach, vi } from 'vitest';
22
import { gray } from 'colorette';
33
import { render, cleanup as cleanupMountedReactTrees, act } from '@testing-library/react';
44
import { Iterate, It, type IterationResult } from '../../src/index.js';
5-
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';
5+
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';
66

77
afterEach(() => {
88
cleanupMountedReactTrees();
@@ -18,7 +18,7 @@ describe('`Iterate` component', () => {
1818
'When used in the no-render-function form and given an iterable that yields a value will render correctly'
1919
),
2020
async () => {
21-
const channel = new IterableChannelTestHelper<string>();
21+
const channel = new IteratorChannelTestHelper<string>();
2222

2323
const rendered = render(<Iterate>{channel}</Iterate>);
2424

@@ -47,7 +47,7 @@ describe('`Iterate` component', () => {
4747
'When used in the no-render-function form and given an iterable that yields a value in conjunction with some initial value will render correctly'
4848
),
4949
async () => {
50-
const channel = new IterableChannelTestHelper<string>();
50+
const channel = new IteratorChannelTestHelper<string>();
5151

5252
const rendered = render(<Iterate initialValue="_">{channel}</Iterate>);
5353

@@ -136,7 +136,7 @@ describe('`Iterate` component', () => {
136136
);
137137

138138
it(gray('When given an iterable that yields a value will render correctly'), async () => {
139-
const channel = new IterableChannelTestHelper<string>();
139+
const channel = new IteratorChannelTestHelper<string>();
140140
let timesRerendered = 0;
141141
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
142142

@@ -180,7 +180,7 @@ describe('`Iterate` component', () => {
180180
'When given an iterable that yields a value in conjunction with some initial value will render correctly'
181181
),
182182
async () => {
183-
const channel = new IterableChannelTestHelper<string>();
183+
const channel = new IteratorChannelTestHelper<string>();
184184
let timesRerendered = 0;
185185
let lastRenderFnInput: undefined | IterationResult<string>;
186186

@@ -221,7 +221,7 @@ describe('`Iterate` component', () => {
221221
);
222222

223223
it(gray('When given an iterable that yields multiple values will render correctly'), async () => {
224-
const channel = new IterableChannelTestHelper<string>();
224+
const channel = new IteratorChannelTestHelper<string>();
225225
let timesRerendered = 0;
226226
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
227227

@@ -331,7 +331,7 @@ describe('`Iterate` component', () => {
331331
it(
332332
gray('When given an iterable that yields a value and then completes will render correctly'),
333333
async () => {
334-
const channel = new IterableChannelTestHelper<string>();
334+
const channel = new IteratorChannelTestHelper<string>();
335335
let timesRerendered = 0;
336336
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
337337

@@ -446,7 +446,7 @@ describe('`Iterate` component', () => {
446446
it(
447447
gray('When given an iterable that yields a value and then errors will render correctly'),
448448
async () => {
449-
const channel = new IterableChannelTestHelper<string>();
449+
const channel = new IteratorChannelTestHelper<string>();
450450
let timesRerendered = 0;
451451
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
452452

@@ -498,8 +498,8 @@ describe('`Iterate` component', () => {
498498
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
499499

500500
const [channel1, channel2] = [
501-
new IterableChannelTestHelper<string>(),
502-
new IterableChannelTestHelper<string>(),
501+
new IteratorChannelTestHelper<string>(),
502+
new IteratorChannelTestHelper<string>(),
503503
];
504504

505505
const [channelReturnSpy1, channelReturnSpy2] = [
@@ -587,7 +587,7 @@ describe('`Iterate` component', () => {
587587
it(gray('When unmounted will close the last active iterator it held'), async () => {
588588
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
589589

590-
const channel = new IterableChannelTestHelper<string>();
590+
const channel = new IteratorChannelTestHelper<string>();
591591
const channelReturnSpy = vi.spyOn(channel, 'return');
592592

593593
const buildTestContent = (value: AsyncIterable<string>) => {
@@ -675,7 +675,7 @@ describe('`Iterate` component', () => {
675675
async () => {
676676
let timesRerendered = 0;
677677
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
678-
const channel = new IterableChannelTestHelper<string>();
678+
const channel = new IteratorChannelTestHelper<string>();
679679

680680
const rendered = render(
681681
<Iterate value={channel}>

spec/tests/iterateFormatted.spec.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { render, cleanup as cleanupMountedReactTrees, act } from '@testing-libra
44
import { iterateFormatted, Iterate } from '../../src/index.js';
55
import { pipe } from '../utils/pipe.js';
66
import { asyncIterToArray } from '../utils/asyncIterToArray.js';
7-
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';
7+
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';
88

99
afterEach(() => {
1010
cleanupMountedReactTrees();
@@ -50,7 +50,7 @@ describe('`iterateFormatted` function', () => {
5050
'When the wrapped source is used normally with library tools it is rendered and formatted correctly'
5151
),
5252
async () => {
53-
const channel = new IterableChannelTestHelper<string>();
53+
const channel = new IteratorChannelTestHelper<string>();
5454

5555
const rendered = render(
5656
<Iterate
@@ -81,8 +81,8 @@ describe('`iterateFormatted` function', () => {
8181
),
8282
async () => {
8383
const [channel1, channel2] = [
84-
new IterableChannelTestHelper<string>(),
85-
new IterableChannelTestHelper<string>(),
84+
new IteratorChannelTestHelper<string>(),
85+
new IteratorChannelTestHelper<string>(),
8686
];
8787

8888
const [channelReturnSpy1, channelReturnSpy2] = [
@@ -124,7 +124,7 @@ describe('`iterateFormatted` function', () => {
124124
'Always the latest closure passed in as the format function will be the one to format the next-arriving source value'
125125
),
126126
async () => {
127-
const channel = new IterableChannelTestHelper<string>();
127+
const channel = new IteratorChannelTestHelper<string>();
128128

129129
const Wrapper = (props: { outerValue: string }) => (
130130
<Iterate

spec/tests/useAsyncIter.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { it, describe, expect, afterEach, vi } from 'vitest';
22
import { gray } from 'colorette';
33
import { cleanup as cleanupMountedReactTrees, act, renderHook } from '@testing-library/react';
44
import { useAsyncIter } from '../../src/index.js';
5-
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';
5+
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';
66

77
afterEach(() => {
88
cleanupMountedReactTrees();
@@ -75,7 +75,7 @@ describe('`useAsyncIter` hook', () => {
7575
);
7676

7777
it(gray('When given an iterable that yields a value will return correct results'), async () => {
78-
const channel = new IterableChannelTestHelper<string>();
78+
const channel = new IteratorChannelTestHelper<string>();
7979

8080
const renderedHook = renderHook(() => useAsyncIter(channel));
8181

@@ -101,7 +101,7 @@ describe('`useAsyncIter` hook', () => {
101101
'When given an iterable that yields a value in conjunction with some initial value will return correct results'
102102
),
103103
async () => {
104-
const channel = new IterableChannelTestHelper<string>();
104+
const channel = new IteratorChannelTestHelper<string>();
105105

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

@@ -127,7 +127,7 @@ describe('`useAsyncIter` hook', () => {
127127
gray('When given an iterable that yields multiple values will return correct results'),
128128
async () => {
129129
let timesRerendered = 0;
130-
const channel = new IterableChannelTestHelper<string>();
130+
const channel = new IteratorChannelTestHelper<string>();
131131

132132
const renderedHook = renderHook(() => {
133133
timesRerendered++;
@@ -211,7 +211,7 @@ describe('`useAsyncIter` hook', () => {
211211
),
212212
async () => {
213213
let timesRerendered = 0;
214-
const channel = new IterableChannelTestHelper<string>();
214+
const channel = new IteratorChannelTestHelper<string>();
215215

216216
const renderedHook = renderHook(() => {
217217
timesRerendered++;
@@ -298,7 +298,7 @@ describe('`useAsyncIter` hook', () => {
298298
gray('When given an iterable that yields a value and then errors will return correct results'),
299299
async () => {
300300
let timesRerendered = 0;
301-
const channel = new IterableChannelTestHelper<string>();
301+
const channel = new IteratorChannelTestHelper<string>();
302302
const simulatedErr = new Error('...');
303303

304304
const renderedHook = renderHook(() => {
@@ -334,8 +334,8 @@ describe('`useAsyncIter` hook', () => {
334334
),
335335
async () => {
336336
const [channel1, channel2] = [
337-
new IterableChannelTestHelper<string>(),
338-
new IterableChannelTestHelper<string>(),
337+
new IteratorChannelTestHelper<string>(),
338+
new IteratorChannelTestHelper<string>(),
339339
];
340340

341341
const [channelReturnSpy1, channelReturnSpy2] = [
@@ -409,7 +409,7 @@ describe('`useAsyncIter` hook', () => {
409409
);
410410

411411
it(gray('When unmounted will close the last active iterator it held'), async () => {
412-
const channel = new IterableChannelTestHelper<string>();
412+
const channel = new IteratorChannelTestHelper<string>();
413413
const channelReturnSpy = vi.spyOn(channel, 'return');
414414

415415
const renderedHook = renderHook(({ value }) => useAsyncIter(value), {
@@ -478,7 +478,7 @@ describe('`useAsyncIter` hook', () => {
478478
),
479479
async () => {
480480
let timesRerendered = 0;
481-
const channel = new IterableChannelTestHelper<string>();
481+
const channel = new IteratorChannelTestHelper<string>();
482482

483483
const renderedHook = renderHook(() => {
484484
timesRerendered++;

spec/utils/IterableChannelTestHelper.ts renamed to spec/utils/IteratorChannelTestHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { IterableChannelTestHelper };
1+
export { IteratorChannelTestHelper };
22

3-
class IterableChannelTestHelper<T> implements AsyncIterableIterator<T>, AsyncDisposable {
3+
class IteratorChannelTestHelper<T> implements AsyncIterableIterator<T>, AsyncDisposable {
44
#isChannelClosed = false;
55
#nextIteration = Promise.withResolvers<IteratorResult<T>>();
66

spec/utils/asyncIterTake.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ export { asyncIterTake };
22

33
function asyncIterTake<T>(count: number): (src: AsyncIterable<T>) => AsyncIterable<T> {
44
return sourceIter => {
5+
if (count === 0) {
6+
return {
7+
[Symbol.asyncIterator]: () => ({
8+
next: async () => ({ done: true, value: undefined }),
9+
return: async () => ({ done: true, value: undefined }),
10+
}),
11+
};
12+
}
13+
514
let iterator: AsyncIterator<T>;
615
let remainingCount = count;
716
let closed = false;

spec/utils/pipe.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
export { pipe };
22

3-
const pipe: PipeFunction = (initVal: unknown, ...funcs: ((...args: any[]) => any)[]) => {
4-
return funcs.reduce((currVal, nextFunc) => nextFunc(currVal), initVal);
5-
};
6-
7-
interface PipeFunction {
3+
const pipe: {
84
<const TInitVal>(initVal: TInitVal): TInitVal;
95

106
<const TInitVal, A>(initVal: TInitVal, ...funcs: [(arg: TInitVal) => A]): A;
@@ -95,4 +91,6 @@ interface PipeFunction {
9591
(arg: I) => J,
9692
]
9793
): J;
98-
}
94+
} = (initVal: unknown, ...funcs: ((...args: unknown[]) => unknown)[]) => {
95+
return funcs.reduce((currVal, fn) => fn(currVal), initVal);
96+
};

src/useAsyncIterState/IterableChannel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class IterableChannel<TVal> {
3434

3535
return: async () => {
3636
whenIteratorClosed.resolve({ done: true, value: undefined });
37-
return { done: true as const, value: undefined };
37+
return { done: true, value: undefined };
3838
},
3939
};
4040
},

0 commit comments

Comments
 (0)