All files Waterflow.js

100% Statements 8/8
50% Branches 1/2
100% Functions 6/6
100% Lines 6/6
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          4x                                   7x 28x                 6x       7x       1x  
import React from 'react';
import PropTypes from 'prop-types';
import memoizeState from 'memoize-state';
import polyfill from 'react-lifecycles-compat';
 
const genFlow = flow => flow.map(fn => memoizeState(fn));
 
export class MemoizedFlow extends React.Component {
  static propTypes = {
    /* eslint-disable */
    input: PropTypes.object.isRequired,
    /* eslint-enable */
    flow: PropTypes.arrayOf(PropTypes.func).isRequired,
    children: PropTypes.func.isRequired,
 
    pure: PropTypes.bool,
  };
 
  static defaultProps = {
    pure: false,
  };
 
  static getDerivedStateFromProps(props, state) {
    return {
      value: state.flow.reduce((value, fn) => Object.assign({}, value, fn(value)), props.input),
    };
  }
 
  state = {
    flow: genFlow(this.props.flow),
  };
 
  shouldComponentUpdate(nextProps, nextState) {
    return !this.props.pure || nextState.value !== this.state.value;
  }
 
  render() {
    return this.props.children(this.state.value);
  }
}
 
polyfill(MemoizedFlow);