{"version":3,"sources":["../src/private/useMemoWithPrevious.ts","../src/useReduceMemo.ts"],"sourcesContent":["import { useEffect, useMemo, useRef, type DependencyList } from 'react';\n\nexport default function useMemoWithPrevious<T>(factory: (prevValue: T | undefined) => T, deps: DependencyList): T {\n  const prevValueRef = useRef<T | undefined>(undefined);\n  // We are building a `useMemo`-like hook, `deps` is passed as-is and `factory` is not one fo the dependencies.\n  // eslint-disable-next-line react-hooks/exhaustive-deps\n  const value = useMemo<T>(() => factory(prevValueRef.current), deps);\n\n  useEffect(() => {\n    prevValueRef.current = value;\n  });\n\n  return value;\n}\n","import useMemoWithPrevious from './private/useMemoWithPrevious.ts';\n\ninterface Reducer<T, U, V extends U | undefined = U | undefined> {\n  (previousValue: V, currentValue: T, currentIndex: number, array: readonly T[] | Iterable<T>): U;\n}\n\ntype Entry<T, U> = {\n  readonly callbackfn: Reducer<T, U | undefined, U>;\n  readonly input: T;\n  readonly output: U | undefined;\n};\n\nfunction useReduceMemo<T>(\n  array: readonly T[] | Iterable<T>,\n  callbackfn: (\n    previousValue: T | undefined,\n    currentValue: T,\n    currentIndex: number,\n    array: readonly T[] | Iterable<T>\n  ) => T\n): T | undefined;\n\nfunction useReduceMemo<T, U>(\n  array: readonly T[] | Iterable<T>,\n  callbackfn: (\n    previousValue: U | undefined,\n    currentValue: T,\n    currentIndex: number,\n    array: readonly T[] | Iterable<T>\n  ) => U\n): U | undefined;\n\nfunction useReduceMemo<T, U>(\n  array: readonly T[] | Iterable<T>,\n  callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: readonly T[] | Iterable<T>) => U,\n  initialValue: U\n): U;\n\n/**\n * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.\n *\n * @template T The type of elements in the input array.\n * @template U The type of the reduced output value.\n *\n * @param array The input array to be reduced. Must be a readonly array.\n * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.\n *\n * @returns The single value that results from the reduction, or `undefined` if the array is empty and no initial value is provided.\n *\n * @example\n * ```ts\n * const array = [1, 2, 3];\n * const reducer = useCallback((prev, curr) => prev + curr, []);\n *\n * const sum = useReduceMemo(array, reducer, 0);\n *\n * console.log(sum); // Output: 6\n * ```\n */\n// TODO: Should we have a `useBuildReducer()` hook which, `useBuildReducer(reducer, 0)([1, 2, 3])`?\nfunction useReduceMemo<T, U>(\n  array: readonly T[] | Iterable<T>,\n  callbackfn: (\n    previousValue: U | undefined,\n    currentValue: T,\n    currentIndex: number,\n    array: readonly T[] | Iterable<T>\n  ) => U,\n  initialValue?: U | undefined\n): U | undefined {\n  const state = useMemoWithPrevious<readonly Entry<T, U>[]>(\n    (state = Object.freeze([])) => {\n      const nextState: Entry<T, U>[] = [];\n      let changed = false;\n      let index = 0;\n      let prevValue: U | undefined = initialValue;\n      let shouldRecompute = false;\n\n      for (const value of array) {\n        const entry = state[+index];\n\n        if (!shouldRecompute && entry && Object.is(entry?.input, value) && Object.is(entry?.callbackfn, callbackfn)) {\n          prevValue = entry?.output;\n\n          // Skips the activity if it has been reduced in the past render loop.\n          nextState.push(entry);\n        } else {\n          changed = true;\n          shouldRecompute = true;\n\n          nextState.push(\n            Object.freeze({\n              callbackfn,\n              input: value,\n              output: (prevValue = callbackfn(prevValue, value, index, array))\n            })\n          );\n        }\n\n        index++;\n      }\n\n      if (nextState.length !== state.length) {\n        changed = true;\n      }\n\n      // Returns the original array if nothing changed.\n      return changed ? Object.freeze(nextState) : state;\n    },\n    [array, callbackfn]\n  );\n\n  // eslint-disable-next-line no-magic-numbers\n  return state.length ? state.at(-1)?.output : initialValue;\n}\n\nexport default useReduceMemo;\n"],"mappings":";AAAA,SAAS,WAAW,SAAS,cAAmC;AAEjD,SAAR,oBAAwC,SAA0C,MAAyB;AAChH,QAAM,eAAe,OAAsB,MAAS;AAGpD,QAAM,QAAQ,QAAW,MAAM,QAAQ,aAAa,OAAO,GAAG,IAAI;AAElE,YAAU,MAAM;AACd,iBAAa,UAAU;AAAA,EACzB,CAAC;AAED,SAAO;AACT;;;AC+CA,SAAS,cACP,OACA,YAMA,cACe;AACf,QAAM,QAAQ;AAAA,IACZ,CAACA,SAAQ,OAAO,OAAO,CAAC,CAAC,MAAM;AAC7B,YAAM,YAA2B,CAAC;AAClC,UAAI,UAAU;AACd,UAAI,QAAQ;AACZ,UAAI,YAA2B;AAC/B,UAAI,kBAAkB;AAEtB,iBAAW,SAAS,OAAO;AACzB,cAAM,QAAQA,OAAM,CAAC,KAAK;AAE1B,YAAI,CAAC,mBAAmB,SAAS,OAAO,GAAG,OAAO,OAAO,KAAK,KAAK,OAAO,GAAG,OAAO,YAAY,UAAU,GAAG;AAC3G,sBAAY,OAAO;AAGnB,oBAAU,KAAK,KAAK;AAAA,QACtB,OAAO;AACL,oBAAU;AACV,4BAAkB;AAElB,oBAAU;AAAA,YACR,OAAO,OAAO;AAAA,cACZ;AAAA,cACA,OAAO;AAAA,cACP,QAAS,YAAY,WAAW,WAAW,OAAO,OAAO,KAAK;AAAA,YAChE,CAAC;AAAA,UACH;AAAA,QACF;AAEA;AAAA,MACF;AAEA,UAAI,UAAU,WAAWA,OAAM,QAAQ;AACrC,kBAAU;AAAA,MACZ;AAGA,aAAO,UAAU,OAAO,OAAO,SAAS,IAAIA;AAAA,IAC9C;AAAA,IACA,CAAC,OAAO,UAAU;AAAA,EACpB;AAGA,SAAO,MAAM,SAAS,MAAM,GAAG,EAAE,GAAG,SAAS;AAC/C;AAEA,IAAO,wBAAQ;","names":["state"]}