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AI,CAAC,kBAAkB;wBACzB,CAAC,CAAC,SAAS;iBACd,CAAC,CAAC;gBACH,OAAO,oBAAC,gBAAgB,eAAK,KAAK,EAAI,CAAC;YACzC,CAAC,CACc,CAClB,CAAC;QACJ,CAAC;QAvCM,sBAAW,GAAG,eAAe,CAAC;QAC9B,2BAAgB,GAAG,gBAAgB,CAAC;QAuC7C,iBAAC;KAAA,AAzCD,CAAyB,KAAK,CAAC,SAAS,GAyCvC;IAGD,OAAO,oBAAoB,CAAC,UAAU,EAAE,gBAAgB,EAAE,EAAE,CAAC,CAAC;AAChE,CAAC","sourcesContent":["import { invariant } from '../../utilities/globals';\nimport * as React from 'react';\nimport hoistNonReactStatics from 'hoist-non-react-statics';\n\nimport { ApolloConsumer } from '../context';\nimport { OperationOption, WithApolloClient } from './types';\n\nfunction getDisplayName<P>(WrappedComponent: React.ComponentType<P>) {\n return WrappedComponent.displayName || WrappedComponent.name || 'Component';\n}\n\nexport function withApollo<TProps, TResult = any>(\n WrappedComponent: React.ComponentType<\n WithApolloClient<Omit<TProps, 'client'>>\n >,\n operationOptions: OperationOption<TProps, TResult> = {}\n): React.ComponentClass<Omit<TProps, 'client'>> {\n const withDisplayName = `withApollo(${getDisplayName(WrappedComponent)})`;\n\n class WithApollo extends React.Component<Omit<TProps, 'client'>> {\n static displayName = withDisplayName;\n static WrappedComponent = WrappedComponent;\n\n // wrapped instance\n private wrappedInstance: any;\n\n constructor(props: Omit<TProps, 'client'>) {\n super(props);\n this.setWrappedInstance = this.setWrappedInstance.bind(this);\n }\n\n getWrappedInstance() {\n invariant(\n operationOptions.withRef,\n `To access the wrapped instance, you need to specify ` +\n `{ withRef: true } in the options`\n );\n\n return this.wrappedInstance;\n }\n\n setWrappedInstance(ref: React.ComponentType<WithApolloClient<TProps>>) {\n this.wrappedInstance = ref;\n }\n\n render() {\n return (\n <ApolloConsumer>\n {client => {\n const props = Object.assign({}, this.props, {\n client,\n ref: operationOptions.withRef\n ? this.setWrappedInstance\n : undefined\n });\n return <WrappedComponent {...props} />;\n }}\n </ApolloConsumer>\n );\n }\n }\n\n // Make sure we preserve any custom statics on the original component.\n return hoistNonReactStatics(WithApollo, WrappedComponent, {});\n}\n"]}
\No newline at end of file