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,WAAW,CACZ,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CACnB,IAA6B;IAE7B,IAAM,MAAM,GAAG;QACb,IAAI,EAAE,IAAI,CAAC,MAAO;QAClB,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ;KACE,CAAC;IAE9B,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,MAAM,CAAC,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,OAAO,EAAb,CAAa,CAAC,CAAC,CAAC;IAC9E,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport { mergeDeepArray } from \"../../utilities/index.js\";\nimport type {\n Cache,\n Reference,\n StoreObject,\n MissingTree,\n} from \"../../cache/index.js\";\n\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\nimport type { ApolloClient, OperationVariables } from \"../../core/index.js\";\nimport type { NoInfer } from \"../types/types.js\";\nimport { useDeepMemo, useLazyRef, wrapHook } from \"./internal/index.js\";\nimport equal from \"@wry/equality\";\n\nexport interface UseFragmentOptions<TData, TVars>\n extends Omit<\n Cache.DiffOptions<NoInfer<TData>, NoInfer<TVars>>,\n \"id\" | \"query\" | \"optimistic\" | \"previousResult\" | \"returnPartialData\"\n >,\n Omit<\n Cache.ReadFragmentOptions<TData, TVars>,\n \"id\" | \"variables\" | \"returnPartialData\"\n > {\n from: StoreObject | Reference | string;\n // Override this field to make it optional (default: true).\n optimistic?: boolean;\n /**\n * The instance of `ApolloClient` to use to look up the fragment.\n *\n * By default, the instance that's passed down via context is used, but you\n * can provide a different instance here.\n *\n * @docGroup 1. Operation options\n */\n client?: ApolloClient<any>;\n}\n\nexport type UseFragmentResult<TData> =\n | {\n data: TData;\n complete: true;\n missing?: never;\n }\n | {\n data: DeepPartial<TData>;\n complete: false;\n missing?: MissingTree;\n };\n\nexport function useFragment<TData = any, TVars = OperationVariables>(\n options: UseFragmentOptions<TData, TVars>\n): UseFragmentResult<TData> {\n return wrapHook(\n \"useFragment\",\n _useFragment,\n useApolloClient(options.client)\n )(options);\n}\n\nfunction _useFragment<TData = any, TVars = OperationVariables>(\n options: UseFragmentOptions<TData, TVars>\n): UseFragmentResult<TData> {\n const { cache } = useApolloClient(options.client);\n\n const diffOptions = useDeepMemo<Cache.DiffOptions<TData, TVars>>(() => {\n const {\n fragment,\n fragmentName,\n from,\n optimistic = true,\n ...rest\n } = options;\n\n return {\n ...rest,\n returnPartialData: true,\n id: typeof from === \"string\" ? from : cache.identify(from),\n query: cache[\"getFragmentDoc\"](fragment, fragmentName),\n optimistic,\n };\n }, [options]);\n\n const resultRef = useLazyRef<UseFragmentResult<TData>>(() =>\n diffToResult(cache.diff<TData>(diffOptions))\n );\n\n const stableOptions = useDeepMemo(() => options, [options]);\n\n // Since .next is async, we need to make sure that we\n // get the correct diff on the next render given new diffOptions\n React.useMemo(() => {\n resultRef.current = diffToResult(cache.diff<TData>(diffOptions));\n }, [diffOptions, cache]);\n\n // Used for both getSnapshot and getServerSnapshot\n const getSnapshot = React.useCallback(() => resultRef.current, []);\n\n return useSyncExternalStore(\n React.useCallback(\n (forceUpdate) => {\n let lastTimeout = 0;\n const subscription = cache.watchFragment(stableOptions).subscribe({\n next: (result) => {\n if (equal(result, resultRef.current)) return;\n resultRef.current = result;\n // If we get another update before we've re-rendered, bail out of\n // the update and try again. This ensures that the relative timing\n // between useQuery and useFragment stays roughly the same as\n // fixed in https://github.com/apollographql/apollo-client/pull/11083\n clearTimeout(lastTimeout);\n lastTimeout = setTimeout(forceUpdate) as any;\n },\n });\n return () => {\n subscription.unsubscribe();\n clearTimeout(lastTimeout);\n };\n },\n [cache, stableOptions]\n ),\n getSnapshot,\n getSnapshot\n );\n}\n\nfunction diffToResult<TData>(\n diff: Cache.DiffResult<TData>\n): UseFragmentResult<TData> {\n const result = {\n data: diff.result!,\n complete: !!diff.complete,\n } as UseFragmentResult<TData>;\n\n if (diff.missing) {\n result.missing = mergeDeepArray(diff.missing.map((error) => error.missing));\n }\n\n return result;\n}\n"]} |