All files / src Subscriptions.tsx

100% Statements 58/58
94.74% Branches 18/19
100% Functions 12/12
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 16127x 27x               27x 27x                                                 27x       27x       27x                       15x   15x       15x 15x 15x     27x 14x     27x       9x         4x     5x 5x 1x   5x 5x 1x     5x 3x 3x 3x 3x 3x 3x   2x 2x     27x 14x     27x 49x     49x     15x 20x 18x           15x 19x 17x           18x           15x 24x             15x 1x           15x 17x 17x 17x     27x   27x  
import * as React from 'react';
import * as PropTypes from 'prop-types';
import ApolloClient, { ApolloError } from 'apollo-client';
import { Observable } from 'apollo-link';
 
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?: 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.isRequired,
    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);
 
    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,
  ) {
    if (
      shallowEqual(this.props.variables, nextProps.variables) &&
      this.client === nextContext.client &&
      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 !== 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;