Skip to content

Various misc refactorings #26

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
Jan 2, 2025
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
24 changes: 12 additions & 12 deletions spec/tests/Iterate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { it, describe, expect, afterEach, vi } from 'vitest';
import { gray } from 'colorette';
import { render, cleanup as cleanupMountedReactTrees, act } from '@testing-library/react';
import { Iterate, It, type IterationResult } from '../../src/index.js';
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';

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

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

Expand Down Expand Up @@ -47,7 +47,7 @@ describe('`Iterate` component', () => {
'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'
),
async () => {
const channel = new IterableChannelTestHelper<string>();
const channel = new IteratorChannelTestHelper<string>();

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

Expand Down Expand Up @@ -136,7 +136,7 @@ describe('`Iterate` component', () => {
);

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

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

Expand Down Expand Up @@ -221,7 +221,7 @@ describe('`Iterate` component', () => {
);

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

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

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

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

const [channel1, channel2] = [
new IterableChannelTestHelper<string>(),
new IterableChannelTestHelper<string>(),
new IteratorChannelTestHelper<string>(),
new IteratorChannelTestHelper<string>(),
];

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

const channel = new IterableChannelTestHelper<string>();
const channel = new IteratorChannelTestHelper<string>();
const channelReturnSpy = vi.spyOn(channel, 'return');

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

const rendered = render(
<Iterate value={channel}>
Expand Down
10 changes: 5 additions & 5 deletions spec/tests/iterateFormatted.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { render, cleanup as cleanupMountedReactTrees, act } from '@testing-libra
import { iterateFormatted, Iterate } from '../../src/index.js';
import { pipe } from '../utils/pipe.js';
import { asyncIterToArray } from '../utils/asyncIterToArray.js';
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';

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

const rendered = render(
<Iterate
Expand Down Expand Up @@ -81,8 +81,8 @@ describe('`iterateFormatted` function', () => {
),
async () => {
const [channel1, channel2] = [
new IterableChannelTestHelper<string>(),
new IterableChannelTestHelper<string>(),
new IteratorChannelTestHelper<string>(),
new IteratorChannelTestHelper<string>(),
];

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

const Wrapper = (props: { outerValue: string }) => (
<Iterate
Expand Down
20 changes: 10 additions & 10 deletions spec/tests/useAsyncIter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { it, describe, expect, afterEach, vi } from 'vitest';
import { gray } from 'colorette';
import { cleanup as cleanupMountedReactTrees, act, renderHook } from '@testing-library/react';
import { useAsyncIter } from '../../src/index.js';
import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js';
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';

afterEach(() => {
cleanupMountedReactTrees();
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('`useAsyncIter` hook', () => {
);

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

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

Expand All @@ -101,7 +101,7 @@ describe('`useAsyncIter` hook', () => {
'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 channel = new IteratorChannelTestHelper<string>();

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

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

const renderedHook = renderHook(() => {
timesRerendered++;
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('`useAsyncIter` hook', () => {
),
async () => {
let timesRerendered = 0;
const channel = new IterableChannelTestHelper<string>();
const channel = new IteratorChannelTestHelper<string>();

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

const renderedHook = renderHook(() => {
Expand Down Expand Up @@ -334,8 +334,8 @@ describe('`useAsyncIter` hook', () => {
),
async () => {
const [channel1, channel2] = [
new IterableChannelTestHelper<string>(),
new IterableChannelTestHelper<string>(),
new IteratorChannelTestHelper<string>(),
new IteratorChannelTestHelper<string>(),
];

const [channelReturnSpy1, channelReturnSpy2] = [
Expand Down Expand Up @@ -409,7 +409,7 @@ describe('`useAsyncIter` hook', () => {
);

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

const renderedHook = renderHook(({ value }) => useAsyncIter(value), {
Expand Down Expand Up @@ -478,7 +478,7 @@ describe('`useAsyncIter` hook', () => {
),
async () => {
let timesRerendered = 0;
const channel = new IterableChannelTestHelper<string>();
const channel = new IteratorChannelTestHelper<string>();

const renderedHook = renderHook(() => {
timesRerendered++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { IterableChannelTestHelper };
export { IteratorChannelTestHelper };

class IterableChannelTestHelper<T> implements AsyncIterableIterator<T>, AsyncDisposable {
class IteratorChannelTestHelper<T> implements AsyncIterableIterator<T>, AsyncDisposable {
#isChannelClosed = false;
#nextIteration = Promise.withResolvers<IteratorResult<T>>();

Expand Down
9 changes: 9 additions & 0 deletions spec/utils/asyncIterTake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ export { asyncIterTake };

function asyncIterTake<T>(count: number): (src: AsyncIterable<T>) => AsyncIterable<T> {
return sourceIter => {
if (count === 0) {
return {
[Symbol.asyncIterator]: () => ({
next: async () => ({ done: true, value: undefined }),
return: async () => ({ done: true, value: undefined }),
}),
};
}

let iterator: AsyncIterator<T>;
let remainingCount = count;
let closed = false;
Expand Down
10 changes: 4 additions & 6 deletions spec/utils/pipe.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export { pipe };

const pipe: PipeFunction = (initVal: unknown, ...funcs: ((...args: any[]) => any)[]) => {
return funcs.reduce((currVal, nextFunc) => nextFunc(currVal), initVal);
};

interface PipeFunction {
const pipe: {
<const TInitVal>(initVal: TInitVal): TInitVal;

<const TInitVal, A>(initVal: TInitVal, ...funcs: [(arg: TInitVal) => A]): A;
Expand Down Expand Up @@ -95,4 +91,6 @@ interface PipeFunction {
(arg: I) => J,
]
): J;
}
} = (initVal: unknown, ...funcs: ((...args: unknown[]) => unknown)[]) => {
return funcs.reduce((currVal, fn) => fn(currVal), initVal);
};
2 changes: 1 addition & 1 deletion src/useAsyncIterState/IterableChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class IterableChannel<TVal> {

return: async () => {
whenIteratorClosed.resolve({ done: true, value: undefined });
return { done: true as const, value: undefined };
return { done: true, value: undefined };
},
};
},
Expand Down
Loading