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AC1B,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC;aAC9B,IAAI,CAAC,UAAA,WAAW;YACf,mBAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAE/C,OAAO,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;QAEL,OAAO,CAAC,KAAK,CAAC;YACZ,mBAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport { useCallback, useEffect, useMemo, useRef } from 'react';\n\nimport { OperationVariables } from '../../core';\nimport { mergeOptions } from '../../utilities';\nimport {\n LazyQueryHookOptions,\n LazyQueryResultTuple,\n QueryResult,\n} from '../types/types';\nimport { useInternalState } from './useQuery';\nimport { useApolloClient } from './useApolloClient';\n\n// The following methods, when called will execute the query, regardless of\n// whether the useLazyQuery execute function was called before.\nconst EAGER_METHODS = [\n 'refetch',\n 'reobserve',\n 'fetchMore',\n 'updateQuery',\n 'startPolling',\n 'subscribeToMore',\n] as const;\n\nexport function useLazyQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LazyQueryHookOptions<TData, TVariables>\n): LazyQueryResultTuple<TData, TVariables> {\n const abortControllersRef = useRef(new Set<AbortController>());\n\n const execOptionsRef = useRef<Partial<LazyQueryHookOptions<TData, TVariables>>>();\n const merged = execOptionsRef.current ? mergeOptions(options, execOptionsRef.current) : options;\n\n const internalState = useInternalState<TData, TVariables>(\n useApolloClient(options && options.client),\n merged?.query ?? query\n );\n\n const useQueryResult = internalState.useQuery({\n ...merged,\n skip: !execOptionsRef.current,\n });\n\n const initialFetchPolicy =\n useQueryResult.observable.options.initialFetchPolicy ||\n internalState.getDefaultFetchPolicy();\n\n const result: QueryResult<TData, TVariables> =\n Object.assign(useQueryResult, {\n called: !!execOptionsRef.current,\n });\n\n // We use useMemo here to make sure the eager methods have a stable identity.\n const eagerMethods = useMemo(() => {\n const eagerMethods: Record<string, any> = {};\n for (const key of EAGER_METHODS) {\n const method = result[key];\n eagerMethods[key] = function () {\n if (!execOptionsRef.current) {\n execOptionsRef.current = Object.create(null);\n // Only the first time populating execOptionsRef.current matters here.\n internalState.forceUpdate();\n }\n return method.apply(this, arguments);\n };\n }\n\n return eagerMethods;\n }, []);\n\n Object.assign(result, eagerMethods);\n\n useEffect(() => {\n return () => {\n abortControllersRef.current.forEach((controller) => {\n controller.abort();\n });\n }\n }, [])\n\n const execute = useCallback<\n LazyQueryResultTuple<TData, TVariables>[0]\n >(executeOptions => {\n const controller = new AbortController();\n abortControllersRef.current.add(controller);\n\n execOptionsRef.current = executeOptions ? {\n ...executeOptions,\n fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy,\n } : {\n fetchPolicy: initialFetchPolicy,\n };\n\n const promise = internalState\n .asyncUpdate(controller.signal) // Like internalState.forceUpdate, but returns a Promise.\n .then(queryResult => {\n abortControllersRef.current.delete(controller);\n\n return Object.assign(queryResult, eagerMethods);\n });\n\n promise.catch(() => {\n abortControllersRef.current.delete(controller);\n });\n\n return promise;\n }, []);\n\n return [execute, result];\n}\n"]} |