{"version":3,"sources":["../../src/react/use-snapshot.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react'\nimport { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector.js'\nimport { isFunction, shallowEqual } from '../utils/index.js'\nimport { snapshot, subscribe } from '../vanilla/index.js'\n\nimport type { SnapshotSelector, SubscribeListener } from '../vanilla/index.js'\n\nexport interface SnapshotOptions<StateSlice> {\n  /**\n   * Whether to notify updates synchronously, default is false, which means updates are batched asynchronously to improve performance.\n   *\n   * In some scenarios (e.g., using Chinese input method in a text box), it is necessary to immediately obtain the latest state, in which case this can be set to true.\n   *\n   * @defaultValue false\n   */\n  sync?: boolean\n  /**\n   * Custom equality function to compare the previous and next state slices.\n   *\n   * @defaultValue Object.is\n   */\n  isEqual?: (a: StateSlice, b: StateSlice) => boolean\n}\n\n/**\n * Returns a snapshot of the store state.\n *\n * @since 0.2.0\n */\nexport function useSnapshot<State extends object>(state: State): State\nexport function useSnapshot<State extends object>(\n  state: State,\n  options: SnapshotOptions<State>\n): State\nexport function useSnapshot<State extends object, StateSlice>(\n  state: State,\n  selector: SnapshotSelector<State, StateSlice>\n): StateSlice\nexport function useSnapshot<State extends object, StateSlice>(\n  state: State,\n  selector: undefined,\n  options: SnapshotOptions<StateSlice>\n): State\nexport function useSnapshot<State extends object, StateSlice>(\n  state: State,\n  selector?: SnapshotSelector<State, StateSlice>,\n  options?: SnapshotOptions<StateSlice>\n): StateSlice\nexport function useSnapshot<State extends object, StateSlice>(\n  proxyState: State,\n  selectorOrOption?: SnapshotOptions<StateSlice> | SnapshotSelector<State, StateSlice>,\n  maybeOptions?: SnapshotOptions<StateSlice>\n) {\n  const isUnmountedRef = useRef(false)\n\n  useEffect(() => {\n    isUnmountedRef.current = false\n    return () => {\n      isUnmountedRef.current = true\n    }\n  }, [])\n\n  let options: SnapshotOptions<StateSlice> | undefined\n  let selector: SnapshotSelector<State, StateSlice> | undefined\n\n  if (selectorOrOption && !isFunction(selectorOrOption)) {\n    options = selectorOrOption\n    selector = undefined\n  } else {\n    options = maybeOptions\n    selector = selectorOrOption\n  }\n\n  const { sync: updateInSync, isEqual = shallowEqual } = options ?? {}\n\n  const _subscribe = useCallback(\n    (callback: SubscribeListener<State>) => subscribe(proxyState, callback, updateInSync),\n    [proxyState, updateInSync]\n  )\n\n  const _getSnapshot = useCallback(() => snapshot(proxyState), [proxyState])\n\n  const _selector = (selector || ((s) => s)) as (state: State) => StateSlice\n\n  const _snapshot = useSyncExternalStoreWithSelector<State, StateSlice>(\n    _subscribe,\n    _getSnapshot,\n    _getSnapshot,\n    _selector,\n    (a, b) => {\n      if (isUnmountedRef.current) return true\n      return isEqual(a, b)\n    }\n  )\n\n  return _snapshot as StateSlice\n}\n"],"names":["useSnapshot","proxyState","selectorOrOption","maybeOptions","isUnmountedRef","useRef","useEffect","current","options","selector","isFunction","undefined","sync","updateInSync","isEqual","shallowEqual","_subscribe","useCallback","callback","subscribe","_getSnapshot","snapshot","_selector","s","_snapshot","useSyncExternalStoreWithSelector","a","b"],"mappings":";;;;+BAgDgBA;;;eAAAA;;;qBAhD+B;4BACE;;;AA+C1C,SAASA,YACdC,UAAiB,EACjBC,gBAAoF,EACpFC,YAA0C;IAE1C,IAAMC,iBAAiBC,IAAAA,aAAM,EAAC;IAE9BC,IAAAA,gBAAS,EAAC;QACRF,eAAeG,OAAO,GAAG;QACzB,OAAO;YACLH,eAAeG,OAAO,GAAG;QAC3B;IACF,GAAG,EAAE;IAEL,IAAIC;IACJ,IAAIC;IAEJ,IAAIP,oBAAoB,CAACQ,IAAAA,oBAAU,EAACR,mBAAmB;QACrDM,UAAUN;QACVO,WAAWE;IACb,OAAO;QACLH,UAAUL;QACVM,WAAWP;IACb;IAEA,IAAuDM,OAAAA,oBAAAA,qBAAAA,UAAW,CAAC,GAA3DI,AAAMC,eAAyCL,KAA/CI,qBAA+CJ,KAA3BM,SAAAA,oCAAUC,sBAAY;IAElD,IAAMC,aAAaC,IAAAA,kBAAW,EAC5B,SAACC;eAAuCC,IAAAA,oBAAS,EAAClB,YAAYiB,UAAUL;OACxE;QAACZ;QAAYY;KAAa;IAG5B,IAAMO,eAAeH,IAAAA,kBAAW,EAAC;eAAMI,IAAAA,mBAAQ,EAACpB;OAAa;QAACA;KAAW;IAEzE,IAAMqB,YAAab,YAAa,SAACc;eAAMA;;IAEvC,IAAMC,YAAYC,IAAAA,8CAAgC,EAChDT,YACAI,cACAA,cACAE,WACA,SAACI,GAAGC;QACF,IAAIvB,eAAeG,OAAO,EAAE,OAAO;QACnC,OAAOO,QAAQY,GAAGC;IACpB;IAGF,OAAOH;AACT"}