{"version":3,"sources":["../../src/use-re-focus/index.ts"],"sourcesContent":["import { useEffectOnce } from '../use-effect-once'\nimport { useLatest } from '../use-latest'\nimport { useThrottledFn } from '../use-throttled-fn'\n\nimport type { UseThrottledFnOptions } from '../use-throttled-fn'\nimport type { AnyFunc } from '../utils/basic'\n\nexport interface UseReFocusOptions extends UseThrottledFnOptions {\n  /**\n   * Register focus event listener.\n   */\n  registerReFocus?: (callback: AnyFunc) => void\n}\n\nexport function registerWebReFocus(callback: AnyFunc): () => void {\n  function handleFocus() {\n    callback()\n  }\n\n  function handleVisibilityChange() {\n    if (!document.hidden) {\n      handleFocus()\n    }\n  }\n\n  window.addEventListener('visibilitychange', handleVisibilityChange, { passive: true })\n  window.addEventListener('focus', handleFocus, { passive: true })\n\n  return () => {\n    window.removeEventListener('visibilitychange', handleVisibilityChange)\n    window.removeEventListener('focus', handleFocus)\n  }\n}\n\n/**\n * A hook to call the callback when the window is focused.\n *\n * @since 1.4.0\n */\nexport function useReFocus(callback: AnyFunc, options: UseReFocusOptions = {}): () => void {\n  const { registerReFocus = registerWebReFocus, ...throttleOptions } = options\n\n  const throttledFn = useThrottledFn(callback, {\n    leading: true,\n    trailing: false,\n    ...throttleOptions,\n  })\n\n  const latest = useLatest({ throttledFn })\n\n  useEffectOnce(() => {\n    return registerReFocus(() => latest.current.throttledFn())\n  })\n\n  return () => throttledFn.cancel()\n}\n\nexport function createReactNativeReFocusRegister(appState: any): (callback: AnyFunc) => () => void {\n  return function registerReactNativeReFocus(callback: AnyFunc) {\n    let state = appState.currentState\n\n    const onAppStateChange = (nextAppState: string) => {\n      if (state.match(/inactive|background/) && nextAppState === 'active') {\n        callback()\n      }\n\n      state = nextAppState\n    }\n\n    const subscription = appState.addEventListener('change', onAppStateChange)\n\n    return () => subscription.remove()\n  }\n}\n"],"names":["createReactNativeReFocusRegister","registerWebReFocus","useReFocus","callback","handleFocus","handleVisibilityChange","document","hidden","window","addEventListener","passive","removeEventListener","options","registerReFocus","throttleOptions","throttledFn","useThrottledFn","leading","trailing","latest","useLatest","useEffectOnce","current","cancel","appState","registerReactNativeReFocus","state","currentState","onAppStateChange","nextAppState","match","subscription","remove"],"mappings":";;;;;;;;;;;QAyDgBA;eAAAA;;QA3CAC;eAAAA;;QAyBAC;eAAAA;;;6BAvCc;yBACJ;8BACK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYxB,SAASD,mBAAmBE,QAAiB;IAClD,SAASC;QACPD;IACF;IAEA,SAASE;QACP,IAAI,CAACC,SAASC,MAAM,EAAE;YACpBH;QACF;IACF;IAEAI,OAAOC,gBAAgB,CAAC,oBAAoBJ,wBAAwB;QAAEK,SAAS;IAAK;IACpFF,OAAOC,gBAAgB,CAAC,SAASL,aAAa;QAAEM,SAAS;IAAK;IAE9D,OAAO;QACLF,OAAOG,mBAAmB,CAAC,oBAAoBN;QAC/CG,OAAOG,mBAAmB,CAAC,SAASP;IACtC;AACF;AAOO,SAASF,WAAWC,QAAiB;QAAES,UAAAA,iEAA6B,CAAC;IAC1E,+BAAqEA,QAA7DC,iBAAAA,wDAAkBZ,+CAAuBa,6CAAoBF;;;IAErE,IAAMG,cAAcC,IAAAA,8BAAc,EAACb,UAAU;QAC3Cc,SAAS;QACTC,UAAU;OACPJ;IAGL,IAAMK,SAASC,IAAAA,oBAAS,EAAC;QAAEL,aAAAA;IAAY;IAEvCM,IAAAA,4BAAa,EAAC;QACZ,OAAOR,gBAAgB;mBAAMM,OAAOG,OAAO,CAACP,WAAW;;IACzD;IAEA,OAAO;eAAMA,YAAYQ,MAAM;;AACjC;AAEO,SAASvB,iCAAiCwB,QAAa;IAC5D,OAAO,SAASC,2BAA2BtB,QAAiB;QAC1D,IAAIuB,QAAQF,SAASG,YAAY;QAEjC,IAAMC,mBAAmB,SAACC;YACxB,IAAIH,MAAMI,KAAK,CAAC,0BAA0BD,iBAAiB,UAAU;gBACnE1B;YACF;YAEAuB,QAAQG;QACV;QAEA,IAAME,eAAeP,SAASf,gBAAgB,CAAC,UAAUmB;QAEzD,OAAO;mBAAMG,aAAaC,MAAM;;IAClC;AACF"}