UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

29 lines (26 loc) • 948 B
import { useCallback, useRef, useSyncExternalStore } from "react"; import { createSyncState, isSyncState } from "./syncStore.native.js"; function useNativeState(initial) { var ref = useRef(null); if (ref.current === null) { ref.current = createSyncState(initial); } var state = ref.current; useSyncExternalStore(state.subscribe, state.getSnapshot, state.getSnapshot); return state; } function syncHandleOf(value) { return isSyncState(value) ? value : null; } function useSyncValue(value) { var handle = syncHandleOf(value); var subscribe = useCallback(function(notify) { return handle ? handle.subscribe(notify) : function() {}; }, [handle]); var getSnapshot = useCallback(function() { return handle ? handle.getSnapshot() : value; }, [handle, value]); return useSyncExternalStore(subscribe, getSnapshot, getSnapshot); } export { syncHandleOf, useNativeState, useSyncValue }; //# sourceMappingURL=syncNativeState.native.js.map