{"version":3,"file":"index.modern.js","sources":["../src/FetchProvider.ts","../src/useFetch.ts"],"sourcesContent":["import {\n  ComponentType,\n  Dispatch,\n  ReactNode,\n  SetStateAction,\n  createElement,\n  useCallback,\n  useState,\n} from 'react';\n\nimport { createContext, useContextSelector, useContextUpdate } from 'use-context-selector';\n\nexport type FetchFunc<Input, Result> = (input: Input) => Promise<Result>;\n\nexport type FetchState<Input, Result> = {\n  input: Input;\n  result?: Result;\n  error?: unknown;\n  promise?: Promise<void>;\n}\n\nexport const createFetchState = <Input, Result>(\n  fn: FetchFunc<Input, Result>,\n  input: Input,\n) => {\n  const state: FetchState<Input, Result> = {\n    input,\n  };\n  state.promise = new Promise<void>((resolve, reject) => {\n    fn(input).then((r) => {\n      state.result = r;\n      resolve();\n    }).catch((e) => {\n      state.error = e;\n      reject(e);\n    }).finally(() => {\n      delete state.promise;\n    });\n  });\n  return state;\n};\n\nconst NO_PROVIDER = 'NO_PROVIDER';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype FetchMap = Map<FetchFunc<any, any>, FetchState<any, any>>;\n\ntype MapState = readonly [\n  FetchMap,\n  Dispatch<SetStateAction<FetchMap>>\n]\n\nconst FetchContext = createContext<MapState | null>(null);\n\ntype FetchProviderProps = {\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any, react/require-default-props\n  initialInputs?: Iterable<readonly [FetchFunc<any, any>, any]>\n  children: ReactNode;\n};\n\n/**\n * FetchProvider component\n *\n * Put this component higher in the component tree.\n *\n * @example\n * import { FetchProvider } from 'react-hooks-fetch';\n *\n * const App = () => (\n *   <FetchProvider initialInputs={[[fn, input]]}>\n *     ...\n *   </FetchProvider>\n * );\n */\nexport const FetchProvider = ({ initialInputs, children }: FetchProviderProps) => {\n  const createMap = () => {\n    const map = new Map();\n    if (initialInputs) {\n      for (const [fn, input] of initialInputs) {\n        map.set(fn, createFetchState(fn, input));\n      }\n    }\n    return map;\n  };\n  return createElement(\n    FetchContext.Provider as ComponentType<{ value: MapState }>,\n    { value: useState(createMap) },\n    children,\n  );\n};\n\nexport const useFetchState = <Input, Result>(\n  fn: FetchFunc<Input, Result>,\n) => {\n  const state = useContextSelector(\n    FetchContext,\n    (mapState) => {\n      if (!mapState) {\n        return NO_PROVIDER;\n      }\n      return mapState[0].get(fn) as FetchState<Input, Result> | undefined;\n    },\n  );\n  if (state === NO_PROVIDER) {\n    throw new Error('missing Provider');\n  }\n  return state;\n};\n\nexport const useSetFetchState = <Input, Result>(\n  fn: FetchFunc<Input, Result>,\n) => {\n  const update = useContextUpdate(FetchContext);\n  const setMapState = useContextSelector(\n    FetchContext,\n    (mapState) => {\n      if (!mapState) {\n        return NO_PROVIDER;\n      }\n      return mapState[1];\n    },\n  );\n  if (setMapState === NO_PROVIDER) {\n    throw new Error('missing Provider');\n  }\n  const setFetchState = useCallback((\n    nextState: FetchState<Input, Result>,\n  ) => {\n    update(() => {\n      setMapState((prev) => new Map(prev).set(fn, nextState));\n    }, { suspense: true });\n  }, [update, setMapState, fn]);\n  return setFetchState;\n};\n","import { useCallback } from 'react';\n\nimport {\n  FetchFunc,\n  createFetchState,\n  useFetchState,\n  useSetFetchState,\n} from './FetchProvider';\n\n/**\n * useRefetch hook\n *\n * This returns only `refetch` part of what `useFetch` returns.\n *\n * @example\n * import { useFetch } from 'react-hooks-fetch';\n *\n * const refetch = useRefetch(desc);\n * refetch('1');\n */\nexport function useRefetch<Input, Result>(\n  fn: FetchFunc<Input, Result>,\n) {\n  const setFetchState = useSetFetchState(fn);\n  const refetch = useCallback((input: Input) => {\n    setFetchState(createFetchState(fn, input));\n  }, [setFetchState, fn]);\n  return refetch;\n}\n\nexport function useFetch<Input, Result>(\n  fn: FetchFunc<Input, Result>,\n  options: { allowUndefined: true },\n): {\n  input: Input | undefined;\n  result: Result | undefined;\n  refetch: (input: Input) => void;\n};\n\nexport function useFetch<Input, Result>(\n  fn: FetchFunc<Input, Result>,\n): {\n  input: Input;\n  result: Result;\n  refetch: (input: Input) => void;\n};\n\n/**\n * useFetch hook\n *\n * This is the main hook to be used.\n *\n * @example\n * import { useFetch } from 'react-hooks-fetch';\n *\n * const { input, result, refetch } = useFetch(desc);\n */\nexport function useFetch<Input, Result>(\n  fn: FetchFunc<Input, Result>,\n  options?: { allowUndefined: boolean },\n) {\n  let state = useFetchState(fn);\n  if (!state && options?.allowUndefined) {\n    state = { input: undefined as unknown as Input };\n  }\n  if (!state) {\n    throw new Error('missing initial input');\n  }\n  if ('promise' in state) {\n    throw state.promise;\n  }\n  if ('error' in state) {\n    throw state.error;\n  }\n  return {\n    input: state.input,\n    result: state.result as Result,\n    refetch: useRefetch(fn),\n  };\n}\n"],"names":["createFetchState","fn","input","state","promise","Promise","resolve","reject","then","r","result","catch","e","error","finally","FetchContext","createContext","FetchProvider","initialInputs","children","createElement","Provider","value","useState","map","Map","set","useRefetch","setFetchState","update","useContextUpdate","setMapState","useContextSelector","mapState","Error","useCallback","nextState","prev","suspense","useSetFetchState","useFetch","options","get","useFetchState","allowUndefined","undefined","refetch"],"mappings":"8KAqBaA,EAAmB,CAC9BC,EACAC,KAEA,MAAMC,EAAmC,CACvCD,MAAAA,GAaF,OAXAC,EAAMC,QAAU,IAAIC,QAAc,CAACC,EAASC,KAC1CN,EAAGC,GAAOM,KAAMC,IACdN,EAAMO,OAASD,EACfH,MACCK,MAAOC,IACRT,EAAMU,MAAQD,EACdL,EAAOK,KACNE,QAAQ,YACFX,EAAMC,YAGVD,GAaHY,EAAeC,EAA+B,MAsBvCC,EAAgB,EAAGC,cAAAA,EAAeC,SAAAA,KAUtCC,EACLL,EAAaM,SACb,CAAEC,MAAOC,EAXO,KAChB,MAAMC,EAAM,IAAIC,IAChB,GAAIP,EACF,IAAK,MAAOjB,EAAIC,KAAUgB,EACxBM,EAAIE,IAAIzB,EAAID,EAAiBC,EAAIC,IAGrC,OAAOsB,KAKPL,GCnEE,SAAUQ,EACd1B,GAEA,MAAM2B,EDuFN3B,CAAAA,IAEA,MAAM4B,EAASC,EAAiBf,G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