react-async-iterators
Version:
The magic of JavaScript async iterators in React ⛓️ 🧬 🔃
33 lines • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAsyncIterMemo = void 0;
const react_1 = require("react");
const ReactAsyncIterable_js_1 = require("../common/ReactAsyncIterable.js");
const useLatest_js_1 = require("../common/hooks/useLatest.js");
const asyncIterSyncMap_js_1 = require("../common/asyncIterSyncMap.js");
const useAsyncIterMemo = (factory, deps) => {
const latestDepsRef = (0, useLatest_js_1.useLatest)(deps);
const depsWithFormattedItersAccountedFor = latestDepsRef.current.map(dep => isReactAsyncIterable(dep) ? dep[ReactAsyncIterable_js_1.reactAsyncIterSpecialInfoSymbol].origSource : dep);
const result = (0, react_1.useMemo)(() => {
const depsWithWrappedFormattedIters = latestDepsRef.current.map((dep, i) => {
const specialInfo = isReactAsyncIterable(dep)
? dep[ReactAsyncIterable_js_1.reactAsyncIterSpecialInfoSymbol]
: undefined;
return !specialInfo
? dep
: (() => {
let iterationIdx = 0;
return (0, asyncIterSyncMap_js_1.asyncIterSyncMap)(specialInfo.origSource, value => latestDepsRef.current[i][ReactAsyncIterable_js_1.reactAsyncIterSpecialInfoSymbol].formatFn(value, iterationIdx++) // TODO: Any chance there won't be a `.formatFn` here if its possible that this might be called somehow at the moment the deps were changed completely?
);
})();
});
return factory(...depsWithWrappedFormattedIters);
}, depsWithFormattedItersAccountedFor);
return result;
};
exports.useAsyncIterMemo = useAsyncIterMemo;
function isReactAsyncIterable(input) {
const inputAsAny = input;
return !!inputAsAny?.[ReactAsyncIterable_js_1.reactAsyncIterSpecialInfoSymbol];
}
//# sourceMappingURL=index.js.map