one
Version:
One is a React framework for web, iOS, and Android apps.
25 lines (22 loc) • 901 B
JavaScript
import { useCallback, useRef, useSyncExternalStore } from "react";
import { createSyncState, isSyncState } from "./syncStore.mjs";
function useNativeState(initial) {
const ref = useRef(null);
if (ref.current === null) {
ref.current = createSyncState(initial);
}
const state = ref.current;
useSyncExternalStore(state.subscribe, state.getSnapshot, state.getSnapshot);
return state;
}
function syncHandleOf(value) {
return isSyncState(value) ? value : null;
}
function useSyncValue(value) {
const handle = syncHandleOf(value);
const subscribe = useCallback((notify) => handle ? handle.subscribe(notify) : () => {}, [handle]);
const getSnapshot = useCallback(() => handle ? handle.getSnapshot() : value, [handle, value]);
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
}
export { syncHandleOf, useNativeState, useSyncValue };
//# sourceMappingURL=syncNativeState.mjs.map