{
  "version": 3,
  "sources": ["../../../src/storeObservation/compiledHooks/useCompiledRefetchableFragment.ts"],
  "sourcesContent": ["import { useState, useCallback, useRef, useEffect } from \"react\";\nimport { unstable_batchedUpdates } from \"react-dom\";\nimport invariant from \"invariant\";\nimport { useCompiledFragment } from \"./useCompiledFragment\";\nimport { isEqual } from \"lodash\";\nimport { convertFetchPolicy } from \"../../convertFetchPolicy\";\nimport { useOverridenOrDefaultApolloClient } from \"../../useOverridenOrDefaultApolloClient\";\n\nimport type { CompiledArtefactModule } from \"@graphitation/apollo-react-relay-duct-tape-compiler\";\nimport type { FragmentReference } from \"./types\";\nimport type { FetchPolicy } from \"../../types\";\n\nexport interface Disposable {\n  dispose(): void;\n}\n\nexport type RefetchFn<Variables extends object = object> = (\n  variables: Partial<Variables>,\n  options?: RefetchOptions,\n) => Disposable;\n\nexport interface RefetchOptions {\n  onComplete?: (error: Error | null) => void;\n  /**\n   * @deprecated Use `onComplete` to match the Relay API.\n   */\n  onCompleted?: (error: Error | null) => void;\n  fetchPolicy?: FetchPolicy;\n}\n\nfunction getOnCompleteCallback(\n  options?: Pick<RefetchOptions, \"onComplete\" | \"onCompleted\">,\n): ((error: Error | null) => void) | undefined {\n  return options?.onComplete ?? options?.onCompleted;\n}\n\n/**\n * These do not exist in the Relay API and should not be exported from the package.\n */\nexport interface PrivateRefetchOptions\n  extends Omit<RefetchOptions, \"fetchPolicy\"> {\n  /**\n   * Returns the fetched data.\n   */\n  UNSTABLE_onCompletedWithData?: (\n    error: Error | null,\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    data: Record<string, any> | null,\n  ) => void;\n\n  fetchPolicy?: FetchPolicy | \"no-cache\";\n}\n\nexport function useCompiledRefetchableFragment(\n  documents: CompiledArtefactModule,\n  fragmentReference: FragmentReference,\n): [data: object, refetch: RefetchFn] {\n  const { executionQueryDocument, metadata } = documents;\n  invariant(\n    metadata && metadata.mainFragment,\n    \"useRefetchableFragment(): Expected metadata to have been extracted from \" +\n      \"the fragment. Did you forget to invoke the compiler?\",\n  );\n  invariant(\n    executionQueryDocument,\n    \"useRefetchableFragment(): Expected fragment `%s` to be refetchable when \" +\n      \"using `useRefetchableFragment`. Did you forget to add a @refetchable \" +\n      \"directive to the fragment?\",\n    metadata.mainFragment.name,\n  );\n\n  const client = useOverridenOrDefaultApolloClient();\n\n  // We use state for this, so that...\n  const [\n    fragmentReferenceWithOwnVariables,\n    setFragmentReferenceWithOwnVariables,\n  ] = useState(fragmentReference);\n  // ...this gets invoked again with updated variables.\n  const data = useCompiledFragment(\n    documents,\n    fragmentReferenceWithOwnVariables,\n  );\n\n  const disposable = useRef<Disposable>();\n  useEffect(\n    () => () => {\n      if (disposable.current) {\n        disposable.current.dispose();\n        disposable.current = undefined;\n      }\n    },\n    [], // On unmount\n  );\n\n  const refetch = useCallback<RefetchFn>(\n    (variablesSubset, options?: PrivateRefetchOptions) => {\n      const variables = {\n        ...fragmentReference.__fragments,\n        ...variablesSubset,\n        id: fragmentReference.id,\n      };\n      const observable = client.watchQuery({\n        fetchPolicy: convertFetchPolicy(options?.fetchPolicy) ?? \"network-only\",\n        query: executionQueryDocument,\n        variables,\n      });\n      let subscription: ZenObservable.Subscription | undefined =\n        observable.subscribe(\n          ({ data, error, loading }) => {\n            if (!loading) {\n              // Be sure not to keep a retain cycle, so cleanup the reference first thing.\n              subscription?.unsubscribe();\n              subscription = undefined;\n              disposable.current = undefined;\n            }\n            unstable_batchedUpdates(() => {\n              if (!loading) {\n                if (options?.UNSTABLE_onCompletedWithData) {\n                  options.UNSTABLE_onCompletedWithData(error || null, data);\n                } else {\n                  // Public callback fires only when finished loading.\n                  getOnCompleteCallback(options)?.(error || null);\n                }\n              }\n              if (!error) {\n                const { id: _, ...variablesToPropagate } = variables;\n                const nextVariables = {\n                  ...fragmentReference.__fragments,\n                  ...variablesToPropagate,\n                };\n                // No need to trigger an update to propagate new variables if they don't actually change.\n                if (\n                  !isEqual(\n                    fragmentReferenceWithOwnVariables.__fragments,\n                    nextVariables,\n                  )\n                ) {\n                  const nextFragmentReference: FragmentReference = {\n                    __fragments: nextVariables,\n                  };\n                  if (fragmentReference.id !== undefined) {\n                    nextFragmentReference.id = fragmentReference.id;\n                  }\n                  setFragmentReferenceWithOwnVariables(nextFragmentReference);\n                }\n              }\n            });\n          },\n          (error) => {\n            // Be sure not to keep a retain cycle\n            subscription?.unsubscribe();\n            subscription = undefined;\n\n            if (options?.UNSTABLE_onCompletedWithData) {\n              options.UNSTABLE_onCompletedWithData(error, null);\n            } else {\n              getOnCompleteCallback(options)?.(error);\n            }\n          },\n        );\n      disposable.current = { dispose: () => subscription?.unsubscribe() };\n      return disposable.current;\n    },\n    [\n      client,\n      executionQueryDocument,\n      fragmentReference.id,\n      fragmentReference.__fragments,\n      fragmentReferenceWithOwnVariables.__fragments,\n    ],\n  );\n\n  return [data, refetch];\n}\n"],
  "mappings": ";AAAA,SAAS,UAAU,aAAa,QAAQ,iBAAiB;AACzD,SAAS,+BAA+B;AACxC,OAAO,eAAe;AACtB,SAAS,2BAA2B;AACpC,SAAS,eAAe;AACxB,SAAS,0BAA0B;AACnC,SAAS,yCAAyC;AAwBlD,SAAS,sBACP,SAC6C;AAhC/C;AAiCE,UAAO,wCAAS,eAAT,YAAuB,mCAAS;AACzC;AAmBO,SAAS,+BACd,WACA,mBACoC;AACpC,QAAM,EAAE,wBAAwB,SAAS,IAAI;AAC7C;AAAA,IACE,YAAY,SAAS;AAAA,IACrB;AAAA,EAEF;AACA;AAAA,IACE;AAAA,IACA;AAAA,IAGA,SAAS,aAAa;AAAA,EACxB;AAEA,QAAM,SAAS,kCAAkC;AAGjD,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,EACF,IAAI,SAAS,iBAAiB;AAE9B,QAAM,OAAO;AAAA,IACX;AAAA,IACA;AAAA,EACF;AAEA,QAAM,aAAa,OAAmB;AACtC;AAAA,IACE,MAAM,MAAM;AACV,UAAI,WAAW,SAAS;AACtB,mBAAW,QAAQ,QAAQ;AAC3B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