react-async-iterators
Version:
The magic of JavaScript async iterators in React ⛓️ 🧬 🔃
21 lines • 860 B
JavaScript
import { callOrReturn } from '../common/callOrReturn.js';
import { useRefWithInitialValue } from '../common/hooks/useRefWithInitialValue.js';
import { useEffectStrictModeSafe } from '../common/hooks/useEffectStrictModeSafe.js';
import { AsyncIterableChannel, } from '../common/AsyncIterableChannel.js';
export { useAsyncIterState };
function useAsyncIterState(initialValue) {
const ref = useRefWithInitialValue(() => {
const initialValueCalced = callOrReturn(initialValue);
const channel = new AsyncIterableChannel(initialValueCalced);
return {
channel,
result: [channel.out, newVal => channel.put(newVal)],
};
});
const { channel, result } = ref.current;
useEffectStrictModeSafe(() => {
return () => channel.close();
});
return result;
}
//# sourceMappingURL=index.js.map