Skip to content

Commit ce550ca

Browse files
authored
refactor: casual cleanup (#73)
1 parent 79d0834 commit ce550ca

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

spec/tests/useAsyncIter.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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, iterateFormatted } from '../../src/index.js';
5-
import { pipe } from '../utils/pipe.js';
65
import { asyncIterOf } from '../utils/asyncIterOf.js';
76
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';
87

src/useAsyncIter/index.ts

+43-43
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export { useAsyncIter, type IterationResult };
4747
* across component updates as long as `input` keeps getting passed the same object reference every
4848
* time (similar to the behavior of a `useEffect(() => {...}, [input])`), therefore care should be taken
4949
* to avoid constantly recreating the iterable on every render, by e.g; declaring it outside the component
50-
* body, control __when__ it should be recreated with React's
50+
* body, controlling __when__ it should be recreated with React's
5151
* [`useMemo`](https://react.dev/reference/react/useMemo) or alternatively use the library's
5252
* {@link iterateFormatted `iterateFormatted`} util for a formatted version of an iterable which
5353
* preserves its identity.
@@ -123,7 +123,7 @@ const useAsyncIter: {
123123
const rerender = useSimpleRerender();
124124

125125
const stateRef = useRefWithInitialValue<IterationResult<any, any>>(() => ({
126-
value: callOrReturn(initialVal) /*as any*/,
126+
value: callOrReturn(initialVal),
127127
pendingFirst: true,
128128
done: false,
129129
error: undefined,
@@ -136,63 +136,63 @@ const useAsyncIter: {
136136
useEffect(() => {}, [undefined]);
137137

138138
stateRef.current = {
139-
value: latestInputRef.current /*as unknown*/,
139+
value: latestInputRef.current,
140140
pendingFirst: false,
141141
done: false,
142142
error: undefined,
143143
};
144144

145145
return stateRef.current;
146-
} else {
147-
const iterSourceRefToUse =
148-
latestInputRef.current[reactAsyncIterSpecialInfoSymbol]?.origSource ?? latestInputRef.current;
146+
}
149147

150-
useMemo((): void => {
151-
const latestInputRefCurrent = latestInputRef.current!;
148+
const iterSourceRefToUse =
149+
latestInputRef.current[reactAsyncIterSpecialInfoSymbol]?.origSource ?? latestInputRef.current;
152150

153-
let value;
154-
let pendingFirst;
151+
useMemo((): void => {
152+
const latestInputRefCurrent = latestInputRef.current!;
155153

156-
if (latestInputRefCurrent.value) {
157-
value = latestInputRefCurrent.value.current;
158-
pendingFirst = false;
159-
} else {
160-
const prevSourceLastestVal = stateRef.current.value;
161-
value = prevSourceLastestVal;
162-
pendingFirst = true;
163-
}
154+
let value;
155+
let pendingFirst;
164156

165-
stateRef.current = {
166-
value,
167-
pendingFirst,
168-
done: false,
169-
error: undefined,
170-
};
171-
}, [iterSourceRefToUse]);
157+
if (latestInputRefCurrent.value) {
158+
value = latestInputRefCurrent.value.current;
159+
pendingFirst = false;
160+
} else {
161+
const prevSourceLastestVal = stateRef.current.value;
162+
value = prevSourceLastestVal;
163+
pendingFirst = true;
164+
}
165+
166+
stateRef.current = {
167+
value,
168+
pendingFirst,
169+
done: false,
170+
error: undefined,
171+
};
172+
}, [iterSourceRefToUse]);
172173

173-
useEffect(() => {
174-
let iterationIdx = 0;
174+
useEffect(() => {
175+
let iterationIdx = 0;
175176

176-
return iterateAsyncIterWithCallbacks(iterSourceRefToUse, stateRef.current.value, next => {
177-
const possibleGivenFormatFn =
178-
latestInputRef.current?.[reactAsyncIterSpecialInfoSymbol]?.formatFn;
177+
return iterateAsyncIterWithCallbacks(iterSourceRefToUse, stateRef.current.value, next => {
178+
const possibleGivenFormatFn =
179+
latestInputRef.current?.[reactAsyncIterSpecialInfoSymbol]?.formatFn;
179180

180-
const formattedValue = possibleGivenFormatFn
181-
? possibleGivenFormatFn(next.value, iterationIdx++)
182-
: next.value; /*as unknown*/
181+
const formattedValue = possibleGivenFormatFn
182+
? possibleGivenFormatFn(next.value, iterationIdx++)
183+
: next.value;
183184

184-
stateRef.current = {
185-
...next,
186-
pendingFirst: false,
187-
value: formattedValue,
188-
};
185+
stateRef.current = {
186+
...next,
187+
pendingFirst: false,
188+
value: formattedValue,
189+
};
189190

190-
rerender();
191-
});
192-
}, [iterSourceRefToUse]);
191+
rerender();
192+
});
193+
}, [iterSourceRefToUse]);
193194

194-
return stateRef.current;
195-
}
195+
return stateRef.current;
196196
};
197197

198198
/**

0 commit comments

Comments
 (0)