{"version":3,"sources":["../../src/use-safe-state/index.ts"],"sourcesContent":["import { useState } from 'react'\nimport { useLatest } from '../use-latest'\nimport { useStableFn } from '../use-stable-fn'\nimport { useUnmounted } from '../use-unmounted'\nimport { isFunction, isReact18OrLater } from '../utils/basic'\nimport { deepEqual } from '../utils/equal'\n\nimport type { Dispatch, SetStateAction } from 'react'\nimport type { Gettable } from '../utils/basic'\n\nexport type ReactSetState<T> = Dispatch<SetStateAction<T>>\n\nexport type UseSafeStateOptions = {\n  /**\n   * Deeply compare the new state with the old state before updating.\n   *\n   * If true, only update the state when the new state is different from the old state.\n   *\n   * @defaultValue false\n   */\n  deep?: boolean\n}\n\n/**\n * A React Hook similar to [React.useState](https://react.dev/reference/react/useState), but suppresses false warning reports in `React <= 17`\n *\n * And includes an optional deep comparison feature (`deep`, default is `false`). It is designed as a safe and efficient alternative to `React.useState`.\n */\nexport function useSafeState<T>(\n  initialState: Gettable<T>,\n  options?: UseSafeStateOptions,\n): readonly [T, ReactSetState<T>]\nexport function useSafeState<T = undefined>(): readonly [T | undefined, ReactSetState<T | undefined>]\nexport function useSafeState<T>(initialState?: Gettable<T>, options?: UseSafeStateOptions) {\n  const { deep = false } = options ?? {}\n  const isUnmounted = useUnmounted()\n  const [state, setState] = useState<T | undefined>(initialState)\n  const latest = useLatest({ deep })\n\n  const setSafeState: ReactSetState<T | undefined> = useStableFn((action) => {\n    /**\n     * @see https://github.com/reactwg/react-18/discussions/82\n     */\n    // React < 18, and the component is unmounted, return as noop\n    if (!isReact18OrLater() && isUnmounted()) return\n\n    const { deep } = latest.current\n\n    if (!deep) {\n      setState(action)\n    } else {\n      setState((pre) => {\n        const newState = isFunction(action) ? action(pre) : action\n        return !deepEqual(pre, newState) ? newState : pre\n      })\n    }\n  })\n\n  return [state, setSafeState] as const\n}\n"],"names":["useSafeState","initialState","options","deep","isUnmounted","useUnmounted","useState","state","setState","latest","useLatest","setSafeState","useStableFn","action","isReact18OrLater","current","pre","newState","isFunction","deepEqual"],"mappings":";;;;+BAiCgBA;;;eAAAA;;;qBAjCS;yBACC;2BACE;4BACC;qBACgB;qBACnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BnB,SAASA,aAAgBC,YAA0B,EAAEC,OAA6B;IACvF,IAAyBA,OAAAA,oBAAAA,qBAAAA,UAAW,CAAC,eAAZA,KAAjBC,MAAAA,8BAAO;IACf,IAAMC,cAAcC,IAAAA,0BAAY;IAChC,IAA0BC,6BAAAA,IAAAA,eAAQ,EAAgBL,mBAA3CM,QAAmBD,cAAZE,WAAYF;IAC1B,IAAMG,SAASC,IAAAA,oBAAS,EAAC;QAAEP,MAAAA;IAAK;IAEhC,IAAMQ,eAA6CC,IAAAA,wBAAW,EAAC,SAACC;QAC9D;;KAEC,GACD,6DAA6D;QAC7D,IAAI,CAACC,IAAAA,uBAAgB,OAAMV,eAAe;QAE1C,IAAM,AAAED,OAASM,OAAOM,OAAO,CAAvBZ;QAER,IAAI,CAACA,MAAM;YACTK,SAASK;QACX,OAAO;YACLL,SAAS,SAACQ;gBACR,IAAMC,WAAWC,IAAAA,iBAAU,EAACL,UAAUA,OAAOG,OAAOH;gBACpD,OAAO,CAACM,IAAAA,gBAAS,EAACH,KAAKC,YAAYA,WAAWD;YAChD;QACF;IACF;IAEA,OAAO;QAACT;QAAOI;KAAa;AAC9B"}