You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
5.8 KiB

{"ast":null,"code":"import _slicedToArray from \"@babel/runtime/helpers/slicedToArray\";\nimport * as React from 'react';\nvar UNINTIALIZED_STATE = {};\nexport default function useSyncState(initialState) {\n var stateRef = React.useRef(UNINTIALIZED_STATE);\n var isSchedulingRef = React.useRef(false);\n var isMountedRef = React.useRef(true);\n React.useEffect(function () {\n isMountedRef.current = true;\n return function () {\n isMountedRef.current = false;\n };\n }, []);\n if (stateRef.current === UNINTIALIZED_STATE) {\n stateRef.current = typeof initialState === 'function' ? initialState() : initialState;\n }\n var _React$useState = React.useState(stateRef.current),\n _React$useState2 = _slicedToArray(_React$useState, 2),\n trackingState = _React$useState2[0],\n setTrackingState = _React$useState2[1];\n var getState = React.useCallback(function () {\n return stateRef.current;\n }, []);\n var setState = React.useCallback(function (state) {\n if (state === stateRef.current || !isMountedRef.current) {\n return;\n }\n stateRef.current = state;\n if (!isSchedulingRef.current) {\n setTrackingState(state);\n }\n }, []);\n var scheduleUpdate = React.useCallback(function (callback) {\n isSchedulingRef.current = true;\n try {\n callback();\n } finally {\n isSchedulingRef.current = false;\n }\n }, []);\n var flushUpdates = React.useCallback(function () {\n if (!isMountedRef.current) {\n return;\n }\n setTrackingState(stateRef.current);\n }, []);\n if (trackingState !== stateRef.current) {\n setTrackingState(stateRef.current);\n }\n var state = stateRef.current;\n React.useDebugValue(state);\n return [state, getState, setState, scheduleUpdate, flushUpdates];\n}","map":{"version":3,"sources":["useSyncState.tsx"],"names":["React","UNINTIALIZED_STATE","useSyncState","initialState","stateRef","useRef","isSchedulingRef","isMountedRef","useEffect","current","trackingState","setTrackingState","useState","getState","useCallback","setState","state","scheduleUpdate","callback","flushUpdates","useDebugValue"],"mappings":";AAAA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAE9B,IAAMC,kBAAkB,GAAG,CAAC,CAAC;AAK7B,eAAe,SAASC,YAAY,CAAIC,YAA4B,EAAE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AACG,OAAO,GAAG,KAAK;IACjC;EACF,CAAC,EAAE,EAAE,CAAC;EAEN,IAAMU,YAAY,GAAGnB,KAAK,CAACc,WAAW,CAAC,YAAM;IAC3C,IAAI,CAACP,YAAY,CAACE,OAAO,EAAE;MACzB;IACF;IAIAE,gBAAgB,CAACP,QAAQ,CAACK,OAAO,CAAC;EACpC,CAAC,EAAE,EAAE,CAAC;EAIN,IAAIC,aAAa,KAAKN,QAAQ,CAACK,OAAO,EAAE;IACtCE,gBAAgB,CAACP,QAAQ,CAACK,OAAO,CAAC;EACpC;EAEA,IAAMO,KAAK,GAAGZ,QAAQ,CAACK,OAAO;EAE9BT,KAAK,CAACoB,aAAa,CAACJ,KAAK,CAAC;EAE1B,OAAO,CAACA,KAAK,EAAEH,QAAQ,EAAEE,QAAQ,EAAEE,cAAc,EAAEE,YAAY,CAAC;AAClE","sourceRoot":"../../src","sourcesContent":["import * as React from 'react';\nconst UNINTIALIZED_STATE = {};\n\n/**\n * This is definitely not compatible with concurrent mode, but we don't have a solution for sync state yet.\n */\nexport default function useSyncState(initialState) {\n const stateRef = React.useRef(UNINTIALIZED_STATE);\n const isSchedulingRef = React.useRef(false);\n const isMountedRef = React.useRef(true);\n React.useEffect(() => {\n isMountedRef.current = true;\n return () => {\n isMountedRef.current = false;\n };\n }, []);\n if (stateRef.current === UNINTIALIZED_STATE) {\n stateRef.current =\n // @ts-expect-error: initialState is a function, but TypeScript doesn't think so\n typeof initialState === 'function' ? initialState() : initialState;\n }\n const [trackingState, setTrackingState] = React.useState(stateRef.current);\n const getState = React.useCallback(() => stateRef.current, []);\n const setState = React.useCallback(state => {\n if (state === stateRef.current || !isMountedRef.current) {\n return;\n }\n stateRef.current = state;\n if (!isSchedulingRef.current) {\n setTrackingState(state);\n }\n }, []);\n const scheduleUpdate = React.useCallback(callback => {\n isSchedulingRef.current = true;\n try {\n callback();\n } finally {\n isSchedulingRef.current = false;\n }\n }, []);\n const flushUpdates = React.useCallback(() => {\n if (!isMountedRef.current) {\n return;\n }\n\n // Make sure that the tracking state is up-to-date.\n // We call it unconditionally, but React should skip the update if state is unchanged.\n setTrackingState(stateRef.current);\n }, []);\n\n // If we're rendering and the tracking state is out of date, update it immediately\n // This will make sure that our updates are applied as early as possible.\n if (trackingState !== stateRef.current) {\n setTrackingState(stateRef.current);\n }\n const state = stateRef.current;\n React.useDebugValue(state);\n return [state, getState, setState, scheduleUpdate, flushUpdates];\n}\n//# sourceMappingURL=useSyncState.js.map"]},"metadata":{},"sourceType":"module"}