All files / src Subscriptions.tsx

100% Statements 67/67
95.65% Branches 22/23
100% Functions 12/12
100% Lines 61/61
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 17727x 27x             27x   27x 27x                                                                 27x       27x       27x                         16x   16x 16x 16x     27x 15x     27x       9x   9x         4x     5x 5x 1x   5x 5x 1x     5x 3x 3x 3x 3x 3x 3x   2x 2x     27x 15x     27x 54x 54x 49x     49x     16x 21x 19x             16x 20x 18x           19x           16x 28x 28x 28x   28x 28x             16x 1x           16x 18x 18x 18x     27x   27x  
import * as React from 'react';
import * as PropTypes from 'prop-types';
import ApolloClient, { ApolloError, FetchPolicy } from 'apollo-client';
import { Observable } from 'apollo-link';
import { DocumentNode } from 'graphql';
import { ZenObservable } from 'zen-observable-ts';
 
import { OperationVariables } from './types';
import { getClient } from './component-utils';
 
const shallowEqual = require('fbjs/lib/shallowEqual');
const invariant = require('invariant');
 
export interface SubscriptionResult<TData = any> {
  loading: boolean;
  data?: TData;
  error?: ApolloError;
}
 
export interface OnSubscriptionDataOptions<TData = any> {
  client: ApolloClient<Object>;
  subscriptionData: SubscriptionResult<TData>;
}
 
export interface SubscriptionProps<TData = any, TVariables = OperationVariables> {
  subscription: DocumentNode;
  variables?: TVariables;
  fetchPolicy?: FetchPolicy;
  shouldResubscribe?: any;
  client?: ApolloClient<Object>;
  onSubscriptionData?: (options: OnSubscriptionDataOptions<TData>) => any;
  children?: (result: SubscriptionResult<TData>) => React.ReactNode;
}
 
export interface SubscriptionState<TData = any> {
  loading: boolean;
  data?: TData;
  error?: ApolloError;
}
 
export interface SubscriptionContext {
  client?: ApolloClient<Object>;
}
 
class Subscription<TData = any, TVariables = any> extends React.Component<
  SubscriptionProps<TData, TVariables>,
  SubscriptionState<TData>
> {
  static contextTypes = {
    client: PropTypes.object.isRequired,
  };
 
  static propTypes = {
    subscription: PropTypes.object.isRequired,
    variables: PropTypes.object,
    children: PropTypes.func,
    onSubscriptionData: PropTypes.func,
    shouldResubscribe: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
  };
 
  private client: ApolloClient<any>;
  private queryObservable?: Observable<any>;
  private querySubscription?: ZenObservable.Subscription;
 
  constructor(props: SubscriptionProps<TData, TVariables>, context: SubscriptionContext) {
    super(props, context);
 
    this.client = getClient(props, context);
    this.initialize(props);
    this.state = this.getInitialState();
  }
 
  componentDidMount() {
    this.startSubscription();
  }
 
  componentWillReceiveProps(
    nextProps: SubscriptionProps<TData, TVariables>,
    nextContext: SubscriptionContext,
  ) {
    const nextClient = getClient(nextProps, nextContext);
 
    if (
      shallowEqual(this.props.variables, nextProps.variables) &&
      this.client === nextClient &&
      this.props.subscription === nextProps.subscription
    ) {
      return;
    }
 
    let shouldResubscribe = nextProps.shouldResubscribe;
    if (typeof shouldResubscribe === 'function') {
      shouldResubscribe = !!shouldResubscribe(this.props, nextProps);
    }
    const shouldNotResubscribe = shouldResubscribe === false;
    if (this.client !== nextClient) {
      this.client = nextClient;
    }
 
    if (!shouldNotResubscribe) {
      this.endSubscription();
      delete this.queryObservable;
      this.initialize(nextProps);
      this.startSubscription();
      this.setState(this.getInitialState());
      return;
    }
    this.initialize(nextProps);
    this.startSubscription();
  }
 
  componentWillUnmount() {
    this.endSubscription();
  }
 
  render() {
    const renderFn: any = this.props.children;
    if (!renderFn) return null;
    const result = Object.assign({}, this.state, {
      variables: this.props.variables,
    });
    return renderFn(result);
  }
 
  private initialize = (props: SubscriptionProps<TData, TVariables>) => {
    if (this.queryObservable) return;
    this.queryObservable = this.client.subscribe({
      query: props.subscription,
      variables: props.variables,
      fetchPolicy: props.fetchPolicy,
    });
  };
 
  private startSubscription = () => {
    if (this.querySubscription) return;
    this.querySubscription = this.queryObservable!.subscribe({
      next: this.updateCurrentData,
      error: this.updateError,
    });
  };
 
  private getInitialState = () => ({
    loading: true,
    error: undefined,
    data: undefined,
  });
 
  private updateCurrentData = (result: SubscriptionResult<TData>) => {
    const {
      client,
      props: { onSubscriptionData },
    } = this;
    if (onSubscriptionData) onSubscriptionData({ client, subscriptionData: result });
    this.setState({
      data: result.data,
      loading: false,
      error: undefined,
    });
  };
 
  private updateError = (error: any) => {
    this.setState({
      error,
      loading: false,
    });
  };
 
  private endSubscription = () => {
    Eif (this.querySubscription) {
      this.querySubscription.unsubscribe();
      delete this.querySubscription;
    }
  };
}
 
export default Subscription;