All files / src QueryRecyclerProvider.tsx

41.18% Statements 7/17
0% Branches 0/4
0% Functions 0/5
41.18% Lines 7/17
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 572x 2x 2x                 2x       2x       2x                                                                       2x  
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { ObservableQueryRecycler } from './queryRecycler';
 
export interface QueryRecyclerProviderProps {
  children: React.ReactNode;
}
 
class QueryRecyclerProvider extends React.Component<
  QueryRecyclerProviderProps
> {
  static propTypes = {
    children: PropTypes.element.isRequired,
  };
 
  static contextTypes = {
    client: PropTypes.object,
  };
 
  static childContextTypes = {
    getQueryRecycler: PropTypes.func.isRequired,
  };
 
  private recyclers: WeakMap<React.Component, ObservableQueryRecycler>;
 
  constructor(props: QueryRecyclerProviderProps) {
    super(props);
    this.recyclers = new WeakMap();
    this.getQueryRecycler = this.getQueryRecycler.bind(this);
  }
 
  componentWillReceiveProps(_: any, nextContext: any) {
    if (this.context.client !== nextContext.client) {
      this.recyclers = new WeakMap();
    }
  }
 
  getQueryRecycler(component: React.Component) {
    if (!this.recyclers.has(component)) {
      this.recyclers.set(component, new ObservableQueryRecycler());
    }
    return this.recyclers.get(component);
  }
 
  getChildContext() {
    return {
      getQueryRecycler: this.getQueryRecycler,
    };
  }
 
  render() {
    return this.props.children;
  }
}
 
export default QueryRecyclerProvider;