shelving
Version:
Toolkit for using data in JavaScript.
21 lines (20 loc) • 722 B
JavaScript
import { useEffect, useState } from "react";
import { runSequence } from "../util/sequence.js";
/**
* Subscribe to an async iterable for the lifetime of the component.
*
* @param sequence An object implementing the `AsyncIterable` interface.
* - Subscription is recreated every time this value changes.
* - Memoise this value to persist the subscription for the lifetime of the component.
*/
export function useSequence(sequence) {
const [value, setValue] = useState(undefined);
const [error, setError] = useState(undefined);
useEffect(() => {
if (sequence)
return runSequence(sequence, setValue, setError);
}, [sequence]);
if (error)
throw error;
return value;
}