All files / src Subscriptions.tsx

98.18% Statements 54/55
81.25% Branches 13/16
100% Functions 12/12
98.04% Lines 50/51
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 15127x 27x               27x 27x                                                 27x       27x       27x                     12x   12x       12x 12x 12x     27x 11x     27x       6x     6x 6x 1x     6x 3x 3x 3x 3x 3x 3x   3x 3x     27x 11x     27x 40x     40x     12x 18x 15x           12x 17x 14x           15x           12x 21x             12x 1x           12x 14x 14x 14x     27x   27x  
import * as React from 'react';
import * as PropTypes from 'prop-types';
import ApolloClient, { ApolloError } from 'apollo-client';
import { Observable } from 'apollo-client/util/Observable';
 
import { DocumentNode } from 'graphql';
import { ZenObservable } from 'zen-observable-ts';
import { OperationVariables } from './types';
 
const shallowEqual = require('fbjs/lib/shallowEqual');
const invariant = require('invariant');
 
export interface SubscriptionResult<TData = any> {
  loading: boolean;
  data?: TData;
  error?: ApolloError;
}
 
export interface SubscriptionProps<TData = any, TVariables = OperationVariables> {
  subscription: DocumentNode;
  variables?: TVariables;
  shouldResubscribe?: boolean;
  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.isRequired,
  };
 
  private client: ApolloClient<any>;
  private queryObservable: Observable<any>;
  private querySubscription: ZenObservable.Subscription;
 
  constructor(props: SubscriptionProps<TData, TVariables>, context: SubscriptionContext) {
    super(props, context);
 
    invariant(
      !!context.client,
      `Could not find "client" in the context of Subscription. Wrap the root component in an <ApolloProvider>`,
    );
    this.client = context.client;
    this.initialize(props);
    this.state = this.getInitialState();
  }
 
  componentDidMount() {
    this.startSubscription();
  }
 
  componentWillReceiveProps(
    nextProps: SubscriptionProps<TData, TVariables>,
    nextContext: SubscriptionContext,
  ) {
    Iif (shallowEqual(this.props, nextProps) && this.client === nextContext.client) {
      return;
    }
    const shouldNotResubscribe = this.props.shouldResubscribe === false;
    if (this.client !== nextContext.client) {
      this.client = nextContext.client;
    }
 
    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 result = Object.assign({}, this.state, {
      variables: this.props.variables,
    });
    return this.props.children(result);
  }
 
  private initialize = (props: SubscriptionProps<TData, TVariables>) => {
    if (this.queryObservable) return;
    this.queryObservable = this.client.subscribe({
      query: props.subscription,
      variables: props.variables,
    });
  };
 
  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>) => {
    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;