Skip to content

Commit c4174ee

Browse files
authored
fix(iterateFormatted): compute formatted current value on demand instead of eagerly as the underlying value might change at any later time (#63)
1 parent 9ad4aeb commit c4174ee

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/iterateFormatted/index.ts

+19-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
reactAsyncIterSpecialInfoSymbol,
3+
parseReactAsyncIterable,
34
type ReactAsyncIterable,
45
type ReactAsyncIterSpecialInfo,
56
} from '../common/ReactAsyncIterable.js';
@@ -104,41 +105,28 @@ function iterateFormatted(
104105
return formatFn(source, 0);
105106
}
106107

107-
const sourcePrevSpecialInfo = source[reactAsyncIterSpecialInfoSymbol];
108+
const { baseIter, formatFn: precedingFormatFn } = parseReactAsyncIterable(source);
108109

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

112-
...(!sourcePrevSpecialInfo
113-
? {
114-
value: !source.value
115-
? undefined
116-
: {
117-
current: formatFn(source.value.current, 0),
118-
},
119-
120-
[reactAsyncIterSpecialInfoSymbol]: {
121-
origSource: source,
122-
formatFn,
123-
},
124-
}
125-
: {
126-
value: !source.value
127-
? undefined
128-
: {
129-
current: (() => {
130-
const prevMapResult = sourcePrevSpecialInfo.formatFn(source.value.current, 0);
131-
return formatFn(prevMapResult, 0);
132-
})(),
133-
},
134-
135-
[reactAsyncIterSpecialInfoSymbol]: {
136-
origSource: sourcePrevSpecialInfo.origSource,
137-
formatFn: (value: unknown, i: number) => {
138-
const prevMapResult = sourcePrevSpecialInfo.formatFn(value, i);
139-
return formatFn(prevMapResult, i);
113+
get value() {
114+
return !source.value
115+
? undefined
116+
: {
117+
get current() {
118+
const result = precedingFormatFn(source.value!.current, 0);
119+
return formatFn(result, 0);
140120
},
141-
},
142-
}),
121+
};
122+
},
123+
124+
[reactAsyncIterSpecialInfoSymbol]: {
125+
origSource: baseIter,
126+
formatFn: (value: unknown, i: number) => {
127+
const result = precedingFormatFn(value, i);
128+
return formatFn(result, i);
129+
},
130+
},
143131
};
144132
}

0 commit comments

Comments
 (0)