UNPKG

8.17 kBSource Map (JSON)View Raw
1{"version":3,"file":"subscription-hoc.js","sourceRoot":"","sources":["../../../src/react/hoc/subscription-hoc.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,oBAAoB,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EACL,cAAc,EACd,WAAW,EACX,2BAA2B,EAC3B,wBAAwB,EACxB,qBAAqB,EACtB,MAAM,aAAa,CAAC;AAGrB,MAAM,UAAU,gBAAgB,CAM9B,QAAsB,EACtB,gBAKM;IALN,iCAAA,EAAA,qBAKM;IAGN,IAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;IAGjC,IAAA,KAIE,gBAAgB,QAJgB,EAAlC,OAAO,mBAAG,wBAAwB,KAAA,EAClC,KAGE,gBAAgB,KAHU,EAA5B,IAAI,mBAAG,qBAAqB,KAAA,EAC5B,KAEE,gBAAgB,MAFF,EAAhB,KAAK,mBAAG,QAAQ,KAAA,EAChB,iBAAiB,GACf,gBAAgB,kBADD,CACE;IAErB,IAAI,iBAAiB,GAAG,OAA2C,CAAC;IACpE,IAAI,OAAO,iBAAiB,KAAK,UAAU;QACzC,iBAAiB,GAAG,cAAM,OAAA,OAA2B,EAA3B,CAA2B,CAAC;IAExD,IAAI,cAAc,GAAG,IAA+B,CAAC;IACrD,IAAI,OAAO,cAAc,KAAK,UAAU;QAAE,cAAc,GAAG,cAAM,OAAA,IAAW,EAAX,CAAW,CAAC;IAG7E,IAAI,eAAmC,CAAC;IACxC,OAAO,UACL,gBAA2D;QAE3D,IAAM,kBAAkB,GAAG,UAAG,KAAK,cAAI,cAAc,CAAC,gBAAgB,CAAC,MAAG,CAAC;QAC3E;YAAsB,2BAIrB;YAGC,iBAAY,KAAa;gBAAzB,YACE,kBAAM,K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AAgB,CAAC,OAAO,EAAE;wBAC5B,KAAI,CAAC,OAAO,GAAG,IAAI,CAAC;wBACpB,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE;4BAC/B,GAAG,EAAE,KAAI,CAAC,kBAAkB;yBAC7B,CAAC,CAAC;qBACJ;oBAED,IAAI,UAAU,EAAE;wBACd,OAAO,CACL,oBAAC,gBAAgB,eACV,KAAgB,EAChB,EAAkB,EACvB,CACH,CAAC;qBACH;oBAKD,IAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;oBAC5C,IAAM,IAAI,GAAG,gBAAgB,CAAC,IAAI,IAAI,MAAM,CAAC;oBAC7C,IAAI,UAAU,aAAK,GAAC,IAAI,IAAG,MAAM,KAAE,CAAC;oBACpC,IAAI,gBAAgB,CAAC,KAAK,EAAE;wBAC1B,IAAM,SAAS;4BAKb,GAAC,IAAI,IAAG,MAAM;4BACd,WAAQ,GAAE,KAAe;+BAC1B,CAAC;wBACF,eAAe,GAAG,gBAAgB,CAAC,KAAK,CACtC,SAAS,EACT,eAAe,CAChB,CAAC;wBACF,UAAU,GAAG,eAAe,CAAC;qBAC9B;oBAED,OAAO,CACL,oBAAC,gBAAgB,eACV,KAAgB,EAChB,UAA0B,EAC/B,CACH,CAAC;gBACJ,CAAC,CACY,CAChB,CAAC;YACJ,CAAC;YAvFM,mBAAW,GAAG,kBAAkB,CAAC;YACjC,wBAAgB,GAAG,gBAAgB,CAAC;YAuF7C,cAAC;SAAA,AA7FD,CAAsB,WAAW,GA6FhC;QAGD,OAAO,oBAAoB,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,CAAC,CAAC;IAC7D,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import * as React from 'react';\nimport { DocumentNode } from 'graphql';\nimport hoistNonReactStatics from 'hoist-non-react-statics';\n\nimport { parser } from '../parser';\nimport { BaseQueryOptions } from '../types/types';\nimport { Subscription } from '../components';\nimport {\n getDisplayName,\n GraphQLBase,\n calculateVariablesFromProps,\n defaultMapPropsToOptions,\n defaultMapPropsToSkip\n} from './hoc-utils';\nimport { OperationOption, OptionProps, DataProps } from './types';\n\nexport function withSubscription<\n TProps extends TGraphQLVariables | {} = {},\n TData extends object = {},\n TGraphQLVariables extends object = {},\n TChildProps extends object = DataProps<TData, TGraphQLVariables>\n>(\n document: DocumentNode,\n operationOptions: OperationOption<\n TProps,\n TData,\n TGraphQLVariables,\n TChildProps\n > = {}\n) {\n // this is memoized so if coming from `graphql` there is nearly no extra cost\n const operation = parser(document);\n // extract options\n const {\n options = defaultMapPropsToOptions,\n skip = defaultMapPropsToSkip,\n alias = 'Apollo',\n shouldResubscribe\n } = operationOptions;\n\n let mapPropsToOptions = options as (props: any) => BaseQueryOptions;\n if (typeof mapPropsToOptions !== 'function')\n mapPropsToOptions = () => options as BaseQueryOptions;\n\n let mapPropsToSkip = skip as (props: any) => boolean;\n if (typeof mapPropsToSkip !== 'function') mapPropsToSkip = () => skip as any;\n\n // allow for advanced referential equality checks\n let lastResultProps: TChildProps | void;\n return (\n WrappedComponent: React.ComponentType<TProps & TChildProps>\n ): React.ComponentClass<TProps> => {\n const graphQLDisplayName = `${alias}(${getDisplayName(WrappedComponent)})`;\n class GraphQL extends GraphQLBase<\n TProps,\n TChildProps,\n { resubscribe: boolean }\n > {\n static displayName = graphQLDisplayName;\n static WrappedComponent = WrappedComponent;\n constructor(props: TProps) {\n super(props);\n this.state = { resubscribe: false };\n }\n\n updateResubscribe(resubscribe: boolean) {\n this.setState({ resubscribe });\n }\n\n componentDidUpdate(prevProps: TProps) {\n const resubscribe = !!(\n shouldResubscribe &&\n shouldResubscribe(prevProps, this.props)\n );\n if (this.state.resubscribe !== resubscribe) {\n this.updateResubscribe(resubscribe);\n }\n }\n\n render() {\n let props = this.props;\n const shouldSkip = mapPropsToSkip(props);\n const opts = shouldSkip\n ? Object.create(null)\n : mapPropsToOptions(props);\n\n if (!shouldSkip && !opts.variables && operation.variables.length > 0) {\n opts.variables = calculateVariablesFromProps(operation, props);\n }\n return (\n <Subscription\n {...opts}\n displayName={graphQLDisplayName}\n skip={shouldSkip}\n subscription={document}\n shouldResubscribe={this.state.resubscribe}\n >\n {({ data, ...r }: any) => {\n if (operationOptions.withRef) {\n this.withRef = true;\n props = Object.assign({}, props, {\n ref: this.setWrappedInstance\n });\n }\n // if we have skipped, no reason to manage any reshaping\n if (shouldSkip) {\n return (\n <WrappedComponent\n {...(props as TProps)}\n {...({} as TChildProps)}\n />\n );\n }\n\n // the HOC's historically hoisted the data from the execution result\n // up onto the result since it was passed as a nested prop\n // we massage the Query components shape here to replicate that\n const result = Object.assign(r, data || {});\n const name = operationOptions.name || 'data';\n let childProps = { [name]: result };\n if (operationOptions.props) {\n const newResult: OptionProps<\n TProps,\n TData,\n TGraphQLVariables\n > = {\n [name]: result,\n ownProps: props as TProps\n };\n lastResultProps = operationOptions.props(\n newResult,\n lastResultProps\n );\n childProps = lastResultProps;\n }\n\n return (\n <WrappedComponent\n {...(props as TProps)}\n {...(childProps as TChildProps)}\n />\n );\n }}\n </Subscription>\n );\n }\n }\n\n // Make sure we preserve any custom statics on the original component.\n return hoistNonReactStatics(GraphQL, WrappedComponent, {});\n };\n}\n"]}
\No newline at end of file