All files / packages/react-tao/src Reactor.js

100% Statements 20/20
100% Branches 6/6
100% Functions 8/8
100% Lines 20/20
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        76x       95x           33x         13x 22x       5x 5x       22x 18x 27x 17x         19x                                                         8x         31x 24x 14x   8x 8x 1x   7x                  
import { Component, createElement } from 'react';
import PropTypes from 'prop-types';
import Adapter from './Adapter';
 
const DUMMY_STATE = {};
 
class Reactor extends Component {
  static get propTypes() {
    return {
      adapter: PropTypes.instanceOf(Adapter).isRequired
    };
  }
 
  constructor(props) {
    super(props);
    // this._adapter = adapter;
  }
 
  componentDidMount() {
    const { adapter } = this.props;
    adapter.registerReactor(this, this.onNotifyChange.bind(this));
  }
 
  componentWillUnmount() {
    const { adapter } = this.props;
    adapter.unregisterReactor(this);
  }
 
  componentDidUpdate(prevProps) {
    const { adapter } = this.props;
    if (adapter !== prevProps.adapter) {
      prevProps.adapter.unregisterReactor(this);
      adapter.registerReactor(this, this.onNotifyChange.bind(this));
    }
  }
 
  shouldComponentUpdate(nextProps) {
    return true;
  }
  //   // TODO: Implement this properly
  //   // CURRENTLY: this.props.adapter already has the changes after a TAO Handler
  //   const { adapter, children, ...props } = this.props;
  //   if (adapter !== nextProps.adapter) {
  //     return true;
  //   }
  //   if (
  //     (adapter.current == null && nextProps.adapter.current != null) ||
  //     (adapter.current != null && nextProps.adapter.current == null)
  //   ) {
  //     return true;
  //   }
  //   if (adapter.current == null && nextProps.adapter.current == null) {
  //     return false;
  //   }
  //   // TODO: if the term, action, orient or any other props change for the same
  //   //  Component, if this returns `false` will React still call render on the
  //   //  Component
  //   if (adapter.current.Component !== nextProps.adapter.current.Component) {
  //     return true;
  //   }
  //   // TODO: figure out if need to perform props equality check
  //   //  - by checking if updated props get sent to re-rendered child
  //   return false;
  // }
 
  onNotifyChange() {
    this.setState(DUMMY_STATE);
  }
 
  render() {
    // NOTE: Currently swallows any children
    const { adapter, children, ...props } = this.props;
    if (!adapter.current) {
      return null;
    }
    const { ComponentHandler, tao, props: childProps } = adapter.current;
    if (!ComponentHandler) {
      return null;
    }
    return createElement(ComponentHandler, {
      ...tao,
      ...props,
      ...childProps
    });
  }
}
 
export default Reactor;