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 | 7x 14x 14x 14x 14x 14x 14x 14x 13x 13x 13x 13x 13x 13x 26x 13x 26x 13x 63x 63x 63x 13x 63x 44x 44x 44x 19x 15x 8x 5x 8x 62x 65x 65x 65x 65x | import React from 'react'
import PropTypes from 'prop-types'
const MAX_DATA_NUM = 1000000
class Through extends React.Component {
static contextTypes = {
through: PropTypes.object,
}
static propTypes = {
area: PropTypes.string.isRequired,
direct: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
]),
bearingKey: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
]),
default: PropTypes.any,
defaultValue: PropTypes.any,
map: PropTypes.func,
}
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.updateArea(this.props.area)
this.canSetState = true
Eif(this.state.dataNum != this.dataNum) {
this.doUpdate(this.data, true)
}
}
componentWillUnmount() {
this.updateArea(undefined)
this.canSetState = false
}
updateArea(area) {
if(this.unsubscribe)
this.unsubscribe()
if(area)
this.unsubscribe = this.context.through.subscribe(area, this.doUpdate)
}
doUpdate = (data, syncUpdate) => {
this.data = data
++this.dataNum
if( !this.timer && this.dataNum > MAX_DATA_NUM ) {
this.dataNum = 0
}
if( syncUpdate ) {
Eif(this.canSetState) {
this.setState({dataNum: this.dataNum})
}
return
}
if( !this.timer ) {
this.timer = setTimeout(() => {
if(this.canSetState) {
this.setState({dataNum: this.dataNum})
}
this.timer = undefined
}, 0)
}
}
render() {
const {
direct,
bearingKey,
default: defval,
defaultValue,
map = a=>a,
} = this.props
const key = bearingKey ?
bearingKey :
direct===true ? 'default' : direct
const data = key ? this.data[key] : this.data
return this.props.children(data ? map(data) : defaultValue || defval)
}
}
export default Through |