Last active
March 20, 2019 22:53
-
-
Save Sawtaytoes/0c6bf010abe5e6862a36c6bda1188f4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node extends Component { | |
componentDidMount() { | |
this.queueUpdate() | |
} | |
componentDidUpdate() { | |
this.queueUpdate() | |
} | |
queueUpdate = () => { | |
const { | |
dispatch, | |
id, | |
} = this.props | |
this | |
.timeoutId = ( | |
setTimeout( | |
() => { | |
dispatch( | |
queueAction( | |
updateNode(id) | |
) | |
) | |
}, | |
getRandomTimeout(), | |
) | |
) | |
} | |
render() { | |
const { | |
color, | |
value, | |
x, | |
y, | |
} = this.props | |
return ( | |
<div | |
style={{ | |
color, | |
left: `${x}ch`, | |
position: 'absolute', | |
top: `${y}em`, | |
}} | |
> | |
{value} | |
</div> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Nodes extends PureComponent { | |
componentDidMount() { | |
setInterval( | |
this.updateNodes, | |
40, | |
) | |
} | |
updateNodes = () => { | |
const { | |
dispatch, | |
queue, | |
} = this.props | |
queue | |
.length > 0 | |
&& ( | |
queue | |
.forEach(dispatch) | |
) | |
&& ( | |
dispatch(resetQueue()) | |
) | |
} | |
render() { | |
const { nodes } = this.props | |
return ( | |
nodes | |
.map(({ | |
id, | |
}) => ( | |
<Node | |
id={id} | |
key={id} | |
/> | |
)) | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialState = generateNodes(1500) | |
const nodesReducerActions = { | |
[UPDATE_NODE]: ( | |
nodes, | |
{ | |
id, | |
updatedValues, | |
}, | |
) => { | |
const nodeIndex = ( | |
nodes | |
.findIndex(({ | |
id: nodeId, | |
}) => ( | |
nodeId === id | |
)) | |
) | |
return ( | |
nodes | |
.slice(0, nodeIndex) | |
.concat({ | |
...nodes[nodeIndex], | |
...updatedValues, | |
}) | |
.concat( | |
nodes | |
.slice(nodeIndex + 1) | |
) | |
) | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialState = [] | |
const queueReducerActions = { | |
[RESET_QUEUE]: () => ( | |
initialState | |
), | |
[QUEUE_ACTION]: ( | |
actions, | |
{ action }, | |
) => ( | |
actions | |
.concat(action) | |
), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment