UNPKG

3.28 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useSyncState;
7
8var React = _interopRequireWildcard(require("react"));
9
10function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
12function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
14const UNINTIALIZED_STATE = {};
15/**
16 * This is definitely not compatible with concurrent mode, but we don't have a solution for sync state yet.
17 */
18
19function useSyncState(initialState) {
20 const stateRef = React.useRef(UNINTIALIZED_STATE);
21 const isSchedulingRef = React.useRef(false);
22 const isMountedRef = React.useRef(true);
23 React.useEffect(() => {
24 isMountedRef.current = true;
25 return () => {
26 isMountedRef.current = false;
27 };
28 }, []);
29
30 if (stateRef.current === UNINTIALIZED_STATE) {
31 stateRef.current = // @ts-expect-error: initialState is a function, but TypeScript doesn't think so
32 typeof initialState === 'function' ? initialState() : initialState;
33 }
34
35 const [trackingState, setTrackingState] = React.useState(stateRef.current);
36 const getState = React.useCallback(() => stateRef.current, []);
37 const setState = React.useCallback(state => {
38 if (state === stateRef.current || !isMountedRef.current) {
39 return;
40 }
41
42 stateRef.current = state;
43
44 if (!isSchedulingRef.current) {
45 setTrackingState(state);
46 }
47 }, []);
48 const scheduleUpdate = React.useCallback(callback => {
49 isSchedulingRef.current = true;
50
51 try {
52 callback();
53 } finally {
54 isSchedulingRef.current = false;
55 }
56 }, []);
57 const flushUpdates = React.useCallback(() => {
58 if (!isMountedRef.current) {
59 return;
60 } // Make sure that the tracking state is up-to-date.
61 // We call it unconditionally, but React should skip the update if state is unchanged.
62
63
64 setTrackingState(stateRef.current);
65 }, []); // If we're rendering and the tracking state is out of date, update it immediately
66 // This will make sure that our updates are applied as early as possible.
67
68 if (trackingState !== stateRef.current) {
69 setTrackingState(stateRef.current);
70 }
71
72 const state = stateRef.current;
73 React.useDebugValue(state);
74 return [state, getState, setState, scheduleUpdate, flushUpdates];
75}
76//# sourceMappingURL=useSyncState.js.map
\No newline at end of file