Skip to content

Commit

Permalink
fix(iterateFormatted): compute formatted current value on demand inst…
Browse files Browse the repository at this point in the history
…ead of eagerly as the underlying value might change at any later time (#63)
  • Loading branch information
shtaif authored Jan 23, 2025
1 parent 9ad4aeb commit c4174ee
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions src/iterateFormatted/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
reactAsyncIterSpecialInfoSymbol,
parseReactAsyncIterable,
type ReactAsyncIterable,
type ReactAsyncIterSpecialInfo,
} from '../common/ReactAsyncIterable.js';
Expand Down Expand Up @@ -104,41 +105,28 @@ function iterateFormatted(
return formatFn(source, 0);
}

const sourcePrevSpecialInfo = source[reactAsyncIterSpecialInfoSymbol];
const { baseIter, formatFn: precedingFormatFn } = parseReactAsyncIterable(source);

return {
[Symbol.asyncIterator]: () => asyncIterSyncMap(source, formatFn)[Symbol.asyncIterator](),

...(!sourcePrevSpecialInfo
? {
value: !source.value
? undefined
: {
current: formatFn(source.value.current, 0),
},

[reactAsyncIterSpecialInfoSymbol]: {
origSource: source,
formatFn,
},
}
: {
value: !source.value
? undefined
: {
current: (() => {
const prevMapResult = sourcePrevSpecialInfo.formatFn(source.value.current, 0);
return formatFn(prevMapResult, 0);
})(),
},

[reactAsyncIterSpecialInfoSymbol]: {
origSource: sourcePrevSpecialInfo.origSource,
formatFn: (value: unknown, i: number) => {
const prevMapResult = sourcePrevSpecialInfo.formatFn(value, i);
return formatFn(prevMapResult, i);
get value() {
return !source.value
? undefined
: {
get current() {
const result = precedingFormatFn(source.value!.current, 0);
return formatFn(result, 0);
},
},
}),
};
},

[reactAsyncIterSpecialInfoSymbol]: {
origSource: baseIter,
formatFn: (value: unknown, i: number) => {
const result = precedingFormatFn(value, i);
return formatFn(result, i);
},
},
};
}

0 comments on commit c4174ee

Please sign in to comment.