All files / src throughContainer.js

100% Statements 31/31
58.33% Branches 7/12
100% Functions 8/8
100% Lines 30/30
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      2x   2x             24x 24x     24x 24x 24x 24x 24x       24x 24x 24x 24x         22x 22x       66x 66x   66x 42x 42x   42x     24x 24x 11x 11x   11x 11x   11x           79x           2x        
import React, { Children } from 'react'
import PropTypes from 'prop-types'
 
const MAX_DATA_NUM = 1000000
 
const throughContainer = (area) => (ThroughComponent) => {
  class ThroughContainer extends React.Component {
    static contextTypes = {
      through: PropTypes.object,
    }
 
    constructor(props, context) {
      super(props, context)
      this.state = {
        dataNum: MAX_DATA_NUM
      }
      this.dataNum = MAX_DATA_NUM
      this.data = {}
      this.timer = undefined
      this.mounted = false
      this.canSetState = false
   }
 
    componentDidMount() {
      this.unsubscribe = this.context.through.subscribe(area, this.doUpdate)
      this.canSetState = true
      Eif(this.state.dataNum != this.dataNum) {
         this.doUpdate(this.data, true)
      }
    }
 
    componentWillUnmount() {
      this.unsubscribe()
      this.canSetState = false
    }
 
    doUpdate = (data, syncUpdate) => {
      this.data = data
      ++this.dataNum
 
      if( syncUpdate ) {
        Eif(this.canSetState) {
          this.setState({dataNum: this.dataNum})
        }
        return
      }
 
      Eif( !this.timer ) {
        this.timer = setTimeout(() => {
          Eif(this.dataNum > MAX_DATA_NUM ) {
            this.dataNum = 0
          }
          Eif(this.canSetState) {
            this.setState({dataNum: this.dataNum})
          }
          this.timer = undefined
        }, 0)
      }
    }
 
    render() {
      return (
        <ThroughComponent {...this.props} {...{[area]: this.data}} />
      )
    }
  }
 
  return ThroughContainer
}
 
export default throughContainer