|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | +import {connect} from 'react-redux'; |
| 5 | +import { |
| 6 | + buildData, |
| 7 | + remove, |
| 8 | + run, |
| 9 | + add, |
| 10 | + update, |
| 11 | + select, |
| 12 | + runLots, |
| 13 | + clear, |
| 14 | + swapRows |
| 15 | +} from './store'; |
| 16 | + |
| 17 | + |
| 18 | +var startTime; |
| 19 | +var lastMeasure; |
| 20 | +var startMeasure = function(name) { |
| 21 | + performance.mark('mark_start_'+name); |
| 22 | + startTime = performance.now(); |
| 23 | + lastMeasure = name; |
| 24 | +} |
| 25 | +var stopMeasure = function() { |
| 26 | + var last = lastMeasure; |
| 27 | + if (lastMeasure) { |
| 28 | + window.setTimeout(function () { |
| 29 | + lastMeasure = null; |
| 30 | + var stop = performance.now(); |
| 31 | + var duration = 0; |
| 32 | + performance.mark('mark_end_' + last); |
| 33 | + window.performance.measure('measure_' + last, 'mark_start_' + last, 'mark_end_' + last); |
| 34 | + var items = performance.getEntriesByType('measure'); |
| 35 | + for (var i = 0; i < items.length; ++i) { |
| 36 | + var req = items[i]; |
| 37 | + duration = req.duration; |
| 38 | + console.log(req.name + ' took ' + req.duration + 'ms'); |
| 39 | + } |
| 40 | + performance.clearMeasures(); |
| 41 | + }, 0); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export class Row extends React.Component { |
| 46 | + constructor(props) { |
| 47 | + super(props); |
| 48 | + this.click = this.click.bind(this); |
| 49 | + this.del = this.del.bind(this); |
| 50 | + } |
| 51 | + shouldComponentUpdate(nextProps, nextState) { |
| 52 | + const {data,styleClass}=this.props; |
| 53 | + const nextStyleClass=nextProps.styleClass; |
| 54 | + const nextData= nextProps.data; |
| 55 | + return data !== nextData||styleClass!=nextStyleClass |
| 56 | + } |
| 57 | + click() { |
| 58 | + this.props.onClick(this.props.data.id); |
| 59 | + } |
| 60 | + del() { |
| 61 | + this.props.onDelete(this.props.data.id); |
| 62 | + } |
| 63 | + render() { |
| 64 | + let {styleClass, onClick, onDelete, data} = this.props; |
| 65 | + return (<tr className={styleClass}> |
| 66 | + <td className="col-md-1">{data.id}</td> |
| 67 | + <td className="col-md-4"> |
| 68 | + <a onClick={this.click}>{data.label}</a> |
| 69 | + </td> |
| 70 | + <td className="col-md-1"><a onClick={this.del}><span className="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td> |
| 71 | + <td className="col-md-6"></td> |
| 72 | + </tr>); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export class Controller extends React.Component{ |
| 77 | + constructor(props) { |
| 78 | + super(props); |
| 79 | + this.select = this.select.bind(this); |
| 80 | + this.delete = this.delete.bind(this); |
| 81 | + this.add = this.add.bind(this); |
| 82 | + this.run = this.run.bind(this); |
| 83 | + this.update = this.update.bind(this); |
| 84 | + this.runLots = this.runLots.bind(this); |
| 85 | + this.clear = this.clear.bind(this); |
| 86 | + this.swapRows = this.swapRows.bind(this); |
| 87 | + this.componentDidUpdate = this.componentDidUpdate.bind(this); |
| 88 | + this.componentDidMount = this.componentDidMount.bind(this); |
| 89 | + this.start = 0; |
| 90 | + } |
| 91 | + shouldComponentUpdate(nextProps, nextState) { |
| 92 | + |
| 93 | + const {data,selected}=this.props; |
| 94 | + const nextData= nextProps.data; |
| 95 | + const nextSelected= nextProps.selected; |
| 96 | + return data !== nextData||selected!=nextSelected; |
| 97 | + } |
| 98 | + printDuration() { |
| 99 | + stopMeasure(); |
| 100 | + } |
| 101 | + componentDidUpdate() { |
| 102 | + this.printDuration(); |
| 103 | + } |
| 104 | + componentDidMount() { |
| 105 | + this.printDuration(); |
| 106 | + } |
| 107 | + run() { |
| 108 | + startMeasure("run"); |
| 109 | + this.props.run(); |
| 110 | + } |
| 111 | + add() { |
| 112 | + startMeasure("add"); |
| 113 | + this.props.add(); |
| 114 | + } |
| 115 | + update() { |
| 116 | + startMeasure("updater"); |
| 117 | + this.props.update(); |
| 118 | + } |
| 119 | + select(id) { |
| 120 | + startMeasure("select"); |
| 121 | + this.props.select(id); |
| 122 | + } |
| 123 | + delete(id) { |
| 124 | + startMeasure("delete"); |
| 125 | + this.props.remove(id); |
| 126 | + } |
| 127 | + runLots() { |
| 128 | + startMeasure("runLots"); |
| 129 | + this.props.runLots(); |
| 130 | + } |
| 131 | + clear() { |
| 132 | + startMeasure("clear"); |
| 133 | + this.props.clear(); |
| 134 | + } |
| 135 | + swapRows() { |
| 136 | + startMeasure("swapRows"); |
| 137 | + this.props.swapRows(); |
| 138 | + } |
| 139 | + render () { |
| 140 | + const selected = this.props.selected; |
| 141 | + var rows = this.props.data.map((d,i) => { |
| 142 | + const id = d.id; |
| 143 | + var className = id === selected ? 'danger':''; |
| 144 | + return <Row key={id} data={d} onClick={this.select} onDelete={this.delete} styleClass={className}></Row> |
| 145 | + }) |
| 146 | + return (<div className="container"> |
| 147 | + <div className="jumbotron"> |
| 148 | + <div className="row"> |
| 149 | + <div className="col-md-6"> |
| 150 | + <h1>React + Redux-Combiner</h1> |
| 151 | + </div> |
| 152 | + <div className="col-md-6"> |
| 153 | + <div className="row"> |
| 154 | + <div className="col-sm-6 smallpad"> |
| 155 | + <button type="button" className="btn btn-primary btn-block" id="run" onClick={this.run}>Create 1,000 rows</button> |
| 156 | + </div> |
| 157 | + <div className="col-sm-6 smallpad"> |
| 158 | + <button type="button" className="btn btn-primary btn-block" id="runlots" onClick={this.runLots}>Create 10,000 rows</button> |
| 159 | + </div> |
| 160 | + <div className="col-sm-6 smallpad"> |
| 161 | + <button type="button" className="btn btn-primary btn-block" id="add" onClick={this.add}>Append 1,000 rows</button> |
| 162 | + </div> |
| 163 | + <div className="col-sm-6 smallpad"> |
| 164 | + <button type="button" className="btn btn-primary btn-block" id="update" onClick={this.update}>Update every 10th row</button> |
| 165 | + </div> |
| 166 | + <div className="col-sm-6 smallpad"> |
| 167 | + <button type="button" className="btn btn-primary btn-block" id="clear" onClick={this.clear}>Clear</button> |
| 168 | + </div> |
| 169 | + <div className="col-sm-6 smallpad"> |
| 170 | + <button type="button" className="btn btn-primary btn-block" id="swaprows" onClick={this.swapRows}>Swap Rows</button> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + <table className="table table-hover table-striped test-data"> |
| 177 | + <tbody>{rows}</tbody> |
| 178 | + </table> |
| 179 | + <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 180 | + </div>); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +export default connect( |
| 185 | + state => { |
| 186 | + return ({ |
| 187 | + data: state.store.data, |
| 188 | + selected: state.store.selected |
| 189 | + }) |
| 190 | +}, |
| 191 | + { |
| 192 | + buildData: () => buildData(), |
| 193 | + remove: id => remove(id), |
| 194 | + run: () => run(), |
| 195 | + add: () => add(), |
| 196 | + update: () => update(), |
| 197 | + select: id => select(id), |
| 198 | + runLots: () => runLots(), |
| 199 | + clear: () => clear(), |
| 200 | + swapRows: () => swapRows() |
| 201 | + } |
| 202 | +)(Controller); |
0 commit comments