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,CAAC,CAAC;QAC3C,OAAO,cAAM,OAAA,GAAG,CAAC,WAAW,EAAE,EAAjB,CAAiB,CAAC;IACjC,CAAC,CAAC,EACF;QACE;;WAEG;QACH,EAAE,IAAA;KACH,CACF,CAAC;AACJ,CAAC","sourcesContent":["import { invariant } from \"../../utilities/globals/index.js\";\nimport * as React from \"rehackt\";\nimport type { DocumentNode } from \"graphql\";\nimport type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport { equal } from \"@wry/equality\";\n\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport type {\n NoInfer,\n SubscriptionHookOptions,\n SubscriptionResult,\n} from \"../types/types.js\";\nimport type {\n ApolloClient,\n DefaultContext,\n ErrorPolicy,\n FetchPolicy,\n FetchResult,\n OperationVariables,\n} from \"../../core/index.js\";\nimport { ApolloError, Observable } from \"../../core/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useDeepMemo } from \"./internal/useDeepMemo.js\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\nimport { toApolloError } from \"./useQuery.js\";\nimport { useIsomorphicLayoutEffect } from \"./internal/useIsomorphicLayoutEffect.js\";\n\n/**\n * > Refer to the [Subscriptions](https://www.apollographql.com/docs/react/data/subscriptions/) section for a more in-depth overview of `useSubscription`.\n *\n * @example\n * ```jsx\n * const COMMENTS_SUBSCRIPTION = gql`\n * subscription OnCommentAdded($repoFullName: String!) {\n * commentAdded(repoFullName: $repoFullName) {\n * id\n * content\n * }\n * }\n * `;\n *\n * function DontReadTheComments({ repoFullName }) {\n * const {\n * data: { commentAdded },\n * loading,\n * } = useSubscription(COMMENTS_SUBSCRIPTION, { variables: { repoFullName } });\n * return <h4>New comment: {!loading && commentAdded.content}</h4>;\n * }\n * ```\n * @remarks\n * #### Consider using `onData` instead of `useEffect`\n *\n * If you want to react to incoming data, please use the `onData` option instead of `useEffect`.\n * State updates you make inside a `useEffect` hook might cause additional rerenders, and `useEffect` is mostly meant for side effects of rendering, not as an event handler.\n * State updates made in an event handler like `onData` might - depending on the React version - be batched and cause only a single rerender.\n *\n * Consider the following component:\n *\n * ```jsx\n * export function Subscriptions() {\n * const { data, error, loading } = useSubscription(query);\n * const [accumulatedData, setAccumulatedData] = useState([]);\n *\n * useEffect(() => {\n * setAccumulatedData((prev) => [...prev, data]);\n * }, [data]);\n *\n * return (\n * <>\n * {loading && <p>Loading...</p>}\n * {JSON.stringify(accumulatedData, undefined, 2)}\n * </>\n * );\n * }\n * ```\n *\n * Instead of using `useEffect` here, we can re-write this component to use the `onData` callback function accepted in `useSubscription`'s `options` object:\n *\n * ```jsx\n * export function Subscriptions() {\n * const [accumulatedData, setAccumulatedData] = useState([]);\n * const { data, error, loading } = useSubscription(\n * query,\n * {\n * onData({ data }) {\n * setAccumulatedData((prev) => [...prev, data])\n * }\n * }\n * );\n *\n * return (\n * <>\n * {loading && <p>Loading...</p>}\n * {JSON.stringify(accumulatedData, undefined, 2)}\n * </>\n * );\n * }\n * ```\n *\n * > ⚠️ **Note:** The `useSubscription` option `onData` is available in Apollo Client >= 3.7. In previous versions, the equivalent option is named `onSubscriptionData`.\n *\n * Now, the first message will be added to the `accumulatedData` array since `onData` is called _before_ the component re-renders. React 18 automatic batching is still in effect and results in a single re-render, but with `onData` we can guarantee each message received after the component mounts is added to `accumulatedData`.\n *\n * @since 3.0.0\n * @param subscription - A GraphQL subscription document parsed into an AST by `gql`.\n * @param options - Options to control how the subscription is executed.\n * @returns Query result object\n */\nexport function useSubscription<\n TData = any,\n TVariables extends OperationVariables = OperationVariables,\n>(\n subscription: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: SubscriptionHookOptions<\n NoInfer<TData>,\n NoInfer<TVariables>\n > = Object.create(null)\n) {\n const hasIssuedDeprecationWarningRef = React.useRef(false);\n const client = useApolloClient(options.client);\n verifyDocumentType(subscription, DocumentType.Subscription);\n\n if (!hasIssuedDeprecationWarningRef.current) {\n hasIssuedDeprecationWarningRef.current = true;\n\n if (options.onSubscriptionData) {\n invariant.warn(\n options.onData ?\n \"'useSubscription' supports only the 'onSubscriptionData' or 'onData' option, but not both. Only the 'onData' option will be used.\"\n : \"'onSubscriptionData' is deprecated and will be removed in a future major version. Please use the 'onData' option instead.\"\n );\n }\n\n if (options.onSubscriptionComplete) {\n invariant.warn(\n options.onComplete ?\n \"'useSubscription' supports only the 'onSubscriptionComplete' or 'onComplete' option, but not both. Only the 'onComplete' option will be used.\"\n : \"'onSubscriptionComplete' is deprecated and will be removed in a future major version. Please use the 'onComplete' option instead.\"\n );\n }\n }\n\n const {\n skip,\n fetchPolicy,\n errorPolicy,\n shouldResubscribe,\n context,\n extensions,\n ignoreResults,\n } = options;\n const variables = useDeepMemo(() => options.variables, [options.variables]);\n\n const recreate = () =>\n createSubscription(\n client,\n subscription,\n variables,\n fetchPolicy,\n errorPolicy,\n context,\n extensions\n );\n\n let [observable, setObservable] = React.useState(\n options.skip ? null : recreate\n );\n\n const recreateRef = React.useRef(recreate);\n useIsomorphicLayoutEffect(() => {\n recreateRef.current = recreate;\n });\n\n if (skip) {\n if (observable) {\n setObservable((observable = null));\n }\n } else if (\n !observable ||\n ((client !== observable.__.client ||\n subscription !== observable.__.query ||\n fetchPolicy !== observable.__.fetchPolicy ||\n errorPolicy !== observable.__.errorPolicy ||\n !equal(variables, observable.__.variables)) &&\n (typeof shouldResubscribe === \"function\" ?\n !!shouldResubscribe(options!)\n : shouldResubscribe) !== false)\n ) {\n setObservable((observable = recreate()));\n }\n\n const optionsRef = React.useRef(options);\n React.useEffect(() => {\n optionsRef.current = options;\n });\n\n const fallbackLoading = !skip && !ignoreResults;\n const fallbackResult = React.useMemo<SubscriptionResult<TData, TVariables>>(\n () => ({\n loading: fallbackLoading,\n error: void 0,\n data: void 0,\n variables,\n }),\n [fallbackLoading, variables]\n );\n\n const ignoreResultsRef = React.useRef(ignoreResults);\n useIsomorphicLayoutEffect(() => {\n // We cannot reference `ignoreResults` directly in the effect below\n // it would add a dependency to the `useEffect` deps array, which means the\n // subscription would be recreated if `ignoreResults` changes\n // As a result, on resubscription, the last result would be re-delivered,\n // rendering the component one additional time, and re-triggering `onData`.\n // The same applies to `fetchPolicy`, which results in a new `observable`\n // being created. We cannot really avoid it in that case, but we can at least\n // avoid it for `ignoreResults`.\n ignoreResultsRef.current = ignoreResults;\n });\n\n const ret = useSyncExternalStore<SubscriptionResult<TData, TVariables>>(\n React.useCallback(\n (update) => {\n if (!observable) {\n return () => {};\n }\n\n let subscriptionStopped = false;\n const variables = observable.__.variables;\n const client = observable.__.client;\n const subscription = observable.subscribe({\n next(fetchResult) {\n if (subscriptionStopped) {\n return;\n }\n\n const result = {\n loading: false,\n // TODO: fetchResult.data can be null but SubscriptionResult.data\n // expects TData | undefined only\n data: fetchResult.data!,\n error: toApolloError(fetchResult),\n variables,\n };\n observable.__.setResult(result);\n if (!ignoreResultsRef.current) update();\n\n if (result.error) {\n optionsRef.current.onError?.(result.error);\n } else if (optionsRef.current.onData) {\n optionsRef.current.onData({\n client,\n data: result,\n });\n } else if (optionsRef.current.onSubscriptionData) {\n optionsRef.current.onSubscriptionData({\n client,\n subscriptionData: result,\n });\n }\n },\n error(error) {\n error =\n error instanceof ApolloError ? error : (\n new ApolloError({ protocolErrors: [error] })\n );\n if (!subscriptionStopped) {\n observable.__.setResult({\n loading: false,\n data: void 0,\n error,\n variables,\n });\n if (!ignoreResultsRef.current) update();\n optionsRef.current.onError?.(error);\n }\n },\n complete() {\n if (!subscriptionStopped) {\n if (optionsRef.current.onComplete) {\n optionsRef.current.onComplete();\n } else if (optionsRef.current.onSubscriptionComplete) {\n optionsRef.current.onSubscriptionComplete();\n }\n }\n },\n });\n\n return () => {\n // immediately stop receiving subscription values, but do not unsubscribe\n // until after a short delay in case another useSubscription hook is\n // reusing the same underlying observable and is about to subscribe\n subscriptionStopped = true;\n setTimeout(() => {\n subscription.unsubscribe();\n });\n };\n },\n [observable]\n ),\n () =>\n observable && !skip && !ignoreResults ?\n observable.__.result\n : fallbackResult,\n () => fallbackResult\n );\n return React.useMemo(\n () => ({\n ...ret,\n restart() {\n invariant(\n !optionsRef.current.skip,\n \"A subscription that is skipped cannot be restarted.\"\n );\n setObservable(recreateRef.current());\n },\n }),\n [ret]\n );\n}\n\nfunction createSubscription<\n TData = any,\n TVariables extends OperationVariables = OperationVariables,\n>(\n client: ApolloClient<any>,\n query: TypedDocumentNode<TData, TVariables>,\n variables: TVariables | undefined,\n fetchPolicy: FetchPolicy | undefined,\n errorPolicy: ErrorPolicy | undefined,\n context: DefaultContext | undefined,\n extensions: Record<string, any> | undefined\n) {\n const options = {\n query,\n variables,\n fetchPolicy,\n errorPolicy,\n context,\n extensions,\n };\n const __ = {\n ...options,\n client,\n result: {\n loading: true,\n data: void 0,\n error: void 0,\n variables,\n } as SubscriptionResult<TData, TVariables>,\n setResult(result: SubscriptionResult<TData, TVariables>) {\n __.result = result;\n },\n };\n\n let observable: Observable<FetchResult<TData>> | null = null;\n return Object.assign(\n new Observable<FetchResult<TData>>((observer) => {\n // lazily start the subscription when the first observer subscribes\n // to get around strict mode\n if (!observable) {\n observable = client.subscribe(options);\n }\n const sub = observable.subscribe(observer);\n return () => sub.unsubscribe();\n }),\n {\n /**\n * A tracking object to store details about the observable and the latest result of the subscription.\n */\n __,\n }\n );\n}\n"]}
\No newline at end of file