one
Version:
One is a React framework for web, iOS, and Android apps.
30 lines (27 loc) • 837 B
JavaScript
import { useRef, useState } from "react";
function useControlled(onChange, revision = 0) {
if (!Number.isInteger(revision) || revision < 0 || revision > 2147483647) {
throw new Error("Control revision must be a nonnegative Int32");
}
const received = useRef({
eventCount: 0,
revision
});
const [acknowledged, setAcknowledged] = useState(received.current);
return {
revision,
acknowledgedEvent: acknowledged.revision === revision ? acknowledged.eventCount : 0,
onNativeChange(event) {
if (event.revision !== revision) return;
if (received.current.revision === revision && event.eventCount <= received.current.eventCount) return;
received.current = event;
try {
onChange(event);
} finally {
setAcknowledged(event);
}
}
};
}
export { useControlled };
//# sourceMappingURL=controlled.mjs.map