{"version":3,"sources":["../../src/use-raf-state/index.ts"],"sourcesContent":["import { useCallback, useRef } from 'react'\nimport { useRafFn } from '../use-raf-fn'\nimport { useSafeState } from '../use-safe-state'\nimport { isFunction } from '../utils/basic'\n\nimport type { ReactSetState, UseSafeStateOptions } from '../use-safe-state'\nimport type { Gettable } from '../utils/basic'\n\nexport interface UseRafStateOptions extends UseSafeStateOptions {}\n\n/**\n * A React Hook that uses [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) to update the state in the next frame for better performance.\n *\n * This Hook works similarly to [React.useState](https://react.dev/reference/react/useState), except for the timing of updates.\n */\nexport function useRafState<T>(initialState: Gettable<T>, options?: UseRafStateOptions): readonly [T, ReactSetState<T>]\nexport function useRafState<T = undefined>(): readonly [T | undefined, ReactSetState<T | undefined>]\nexport function useRafState<T>(initialState?: Gettable<T>, options?: UseRafStateOptions) {\n  const [state, setState] = useSafeState(initialState, options)\n  const stateRef = useRef(state)\n\n  const scheduleSet = useRafFn(setState, true)\n\n  const set = useCallback<typeof setState>(\n    (value) => {\n      stateRef.current = isFunction(value) ? value(stateRef.current) : value\n      scheduleSet(() => stateRef.current)\n    },\n    [scheduleSet],\n  )\n\n  return [state, set] as const\n}\n"],"names":["useRafState","initialState","options","useSafeState","state","setState","stateRef","useRef","scheduleSet","useRafFn","set","useCallback","value","current","isFunction"],"mappings":";;;;+BAiBgBA;;;eAAAA;;;qBAjBoB;wBACX;4BACI;qBACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcpB,SAASA,YAAeC,YAA0B,EAAEC,OAA4B;IACrF,IAA0BC,iCAAAA,IAAAA,0BAAY,EAACF,cAAcC,cAA9CE,QAAmBD,kBAAZE,WAAYF;IAC1B,IAAMG,WAAWC,IAAAA,aAAM,EAACH;IAExB,IAAMI,cAAcC,IAAAA,kBAAQ,EAACJ,UAAU;IAEvC,IAAMK,MAAMC,IAAAA,kBAAW,EACrB,SAACC;QACCN,SAASO,OAAO,GAAGC,IAAAA,iBAAU,EAACF,SAASA,MAAMN,SAASO,OAAO,IAAID;QACjEJ,YAAY;mBAAMF,SAASO,OAAO;;IACpC,GACA;QAACL;KAAY;IAGf,OAAO;QAACJ;QAAOM;KAAI;AACrB"}