Skip to content

Commit

Permalink
refactor: casual cleanup (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
shtaif authored Jan 28, 2025
1 parent 79d0834 commit ce550ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
1 change: 0 additions & 1 deletion spec/tests/useAsyncIter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { it, describe, expect, afterEach, vi } from 'vitest';
import { gray } from 'colorette';
import { cleanup as cleanupMountedReactTrees, act, renderHook } from '@testing-library/react';
import { useAsyncIter, iterateFormatted } from '../../src/index.js';
import { pipe } from '../utils/pipe.js';
import { asyncIterOf } from '../utils/asyncIterOf.js';
import { IteratorChannelTestHelper } from '../utils/IteratorChannelTestHelper.js';

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

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

stateRef.current = {
value: latestInputRef.current /*as unknown*/,
value: latestInputRef.current,
pendingFirst: false,
done: false,
error: undefined,
};

return stateRef.current;
} else {
const iterSourceRefToUse =
latestInputRef.current[reactAsyncIterSpecialInfoSymbol]?.origSource ?? latestInputRef.current;
}

useMemo((): void => {
const latestInputRefCurrent = latestInputRef.current!;
const iterSourceRefToUse =
latestInputRef.current[reactAsyncIterSpecialInfoSymbol]?.origSource ?? latestInputRef.current;

let value;
let pendingFirst;
useMemo((): void => {
const latestInputRefCurrent = latestInputRef.current!;

if (latestInputRefCurrent.value) {
value = latestInputRefCurrent.value.current;
pendingFirst = false;
} else {
const prevSourceLastestVal = stateRef.current.value;
value = prevSourceLastestVal;
pendingFirst = true;
}
let value;
let pendingFirst;

stateRef.current = {
value,
pendingFirst,
done: false,
error: undefined,
};
}, [iterSourceRefToUse]);
if (latestInputRefCurrent.value) {
value = latestInputRefCurrent.value.current;
pendingFirst = false;
} else {
const prevSourceLastestVal = stateRef.current.value;
value = prevSourceLastestVal;
pendingFirst = true;
}

stateRef.current = {
value,
pendingFirst,
done: false,
error: undefined,
};
}, [iterSourceRefToUse]);

useEffect(() => {
let iterationIdx = 0;
useEffect(() => {
let iterationIdx = 0;

return iterateAsyncIterWithCallbacks(iterSourceRefToUse, stateRef.current.value, next => {
const possibleGivenFormatFn =
latestInputRef.current?.[reactAsyncIterSpecialInfoSymbol]?.formatFn;
return iterateAsyncIterWithCallbacks(iterSourceRefToUse, stateRef.current.value, next => {
const possibleGivenFormatFn =
latestInputRef.current?.[reactAsyncIterSpecialInfoSymbol]?.formatFn;

const formattedValue = possibleGivenFormatFn
? possibleGivenFormatFn(next.value, iterationIdx++)
: next.value; /*as unknown*/
const formattedValue = possibleGivenFormatFn
? possibleGivenFormatFn(next.value, iterationIdx++)
: next.value;

stateRef.current = {
...next,
pendingFirst: false,
value: formattedValue,
};
stateRef.current = {
...next,
pendingFirst: false,
value: formattedValue,
};

rerender();
});
}, [iterSourceRefToUse]);
rerender();
});
}, [iterSourceRefToUse]);

return stateRef.current;
}
return stateRef.current;
};

/**
Expand Down

0 comments on commit ce550ca

Please sign in to comment.