one
Version:
One is a React framework for web, iOS, and Android apps.
52 lines (49 loc) • 1.51 kB
JavaScript
import { useCallback, useLayoutEffect, useRef, useState } from "react";
import { createLatestComputation } from "./createLatestComputation.mjs";
function useBackgroundComputation(definition, input) {
const [state, setState] = useState({
phase: "idle",
revision: 0
});
const owner = useRef(null);
const submitted = useRef(null);
const active = input !== null;
useLayoutEffect(() => {
if (!active) return;
const computation = createLatestComputation(definition, (changed) => {
setState(changed.phase === "ready" ? {
phase: "ready",
revision: changed.result.revision
} : changed);
});
owner.current = computation;
return () => {
owner.current = null;
submitted.current = null;
computation.dispose();
};
}, [active, definition]);
useLayoutEffect(() => {
if (input === null) return;
submitted.current = {
input,
factory: definition
};
owner.current?.update(input);
}, [
active,
definition,
input
]);
const getCurrent = useCallback(() => owner.current?.getCurrent() ?? null, []);
const matches = active && submitted.current?.input === input && submitted.current.factory === definition;
if (matches && state.phase === "failed") throw state.error;
const current = owner.current?.getCurrent();
const result = matches && state.phase === "ready" && current && state.revision === current.revision ? current : null;
return {
result,
getCurrent
};
}
export { useBackgroundComputation };
//# sourceMappingURL=useBackgroundComputation.mjs.map