1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = useSyncState;
|
7 |
|
8 | var React = _interopRequireWildcard(require("react"));
|
9 |
|
10 | function _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 |
|
12 | function _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 |
|
14 | const UNINTIALIZED_STATE = {};
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function 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 =
|
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 | }
|
61 |
|
62 |
|
63 |
|
64 | setTrackingState(stateRef.current);
|
65 | }, []);
|
66 |
|
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 |
|
\ | No newline at end of file |