{"version":3,"file":"useStateStore.mjs","names":[],"sources":["../../../../src/store/hooks/useStateStore.ts"],"sourcesContent":["import { useCallback, useMemo } from 'react';\nimport { useSyncExternalStore } from 'use-sync-external-store/shim';\n\nimport type { StateStore } from '@stream-io/state-store';\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nconst noop = () => {};\n\nconst areSelectionsEqual = <\n  O extends Readonly<Record<string, unknown> | Readonly<unknown[]>>,\n>(\n  cached: O,\n  next: O,\n) => {\n  let cachedKeyCount = 0;\n\n  for (const key in cached) {\n    if (cached[key] !== next[key]) return false;\n    cachedKeyCount += 1;\n  }\n\n  let nextKeyCount = 0;\n  for (const key in next) {\n    if (!(key in cached)) return false;\n    nextKeyCount += 1;\n  }\n\n  return cachedKeyCount === nextKeyCount;\n};\n\nexport function useStateStore<\n  T extends Record<string, unknown>,\n  O extends Readonly<Record<string, unknown> | Readonly<unknown[]>>,\n>(store: StateStore<T>, selector: (v: T) => O): O;\nexport function useStateStore<\n  T extends Record<string, unknown>,\n  O extends Readonly<Record<string, unknown> | Readonly<unknown[]>>,\n>(store: StateStore<T> | undefined, selector: (v: T) => O): O | undefined;\nexport function useStateStore<\n  T extends Record<string, unknown>,\n  O extends Readonly<Record<string, unknown> | Readonly<unknown[]>>,\n>(store: StateStore<T> | undefined, selector: (v: T) => O) {\n  const wrappedSubscription = useCallback(\n    (onStoreChange: () => void) => {\n      const unsubscribe = store?.subscribeWithSelector(selector, onStoreChange);\n      return unsubscribe ?? noop;\n    },\n    [store, selector],\n  );\n\n  const wrappedSnapshot = useMemo(() => {\n    let cachedTuple: [T, O];\n\n    return () => {\n      const currentValue = store?.getLatestValue();\n\n      if (!currentValue) return undefined;\n\n      // The store value is unchanged, so reuse the cached selection.\n      if (cachedTuple && cachedTuple[0] === currentValue) {\n        return cachedTuple[1];\n      }\n\n      const newlySelected = selector(currentValue);\n\n      // The store value changed but the selection may not have: keep the cached one when both\n      // have the same keys and every key holds the same value.\n      if (cachedTuple && areSelectionsEqual(cachedTuple[1], newlySelected)) {\n        return cachedTuple[1];\n      }\n\n      cachedTuple = [currentValue, newlySelected];\n      return cachedTuple[1];\n    };\n  }, [store, selector]);\n\n  const state = useSyncExternalStore(\n    wrappedSubscription,\n    wrappedSnapshot,\n    wrappedSnapshot,\n  );\n\n  return state;\n}\n"],"mappings":";;;AAMA,IAAM,aAAa,CAAC;AAEpB,IAAM,sBAGJ,QACA,SACG;CACH,IAAI,iBAAiB;CAErB,KAAK,MAAM,OAAO,QAAQ;EACxB,IAAI,OAAO,SAAS,KAAK,MAAM,OAAO;EACtC,kBAAkB;CACpB;CAEA,IAAI,eAAe;CACnB,KAAK,MAAM,OAAO,MAAM;EACtB,IAAI,EAAE,OAAO,SAAS,OAAO;EAC7B,gBAAgB;CAClB;CAEA,OAAO,mBAAmB;AAC5B;AAUA,SAAgB,cAGd,OAAkC,UAAuB;CACzD,MAAM,sBAAsB,aACzB,kBAA8B;EAE7B,OADoB,OAAO,sBAAsB,UAAU,aAAa,KAClD;CACxB,GACA,CAAC,OAAO,QAAQ,CAClB;CAEA,MAAM,kBAAkB,cAAc;EACpC,IAAI;EAEJ,aAAa;GACX,MAAM,eAAe,OAAO,eAAe;GAE3C,IAAI,CAAC,cAAc,OAAO,KAAA;GAG1B,IAAI,eAAe,YAAY,OAAO,cACpC,OAAO,YAAY;GAGrB,MAAM,gBAAgB,SAAS,YAAY;GAI3C,IAAI,eAAe,mBAAmB,YAAY,IAAI,aAAa,GACjE,OAAO,YAAY;GAGrB,cAAc,CAAC,cAAc,aAAa;GAC1C,OAAO,YAAY;EACrB;CACF,GAAG,CAAC,OAAO,QAAQ,CAAC;CAQpB,OANc,qBACZ,qBACA,iBACA,eAGK;AACT"}