-
Notifications
You must be signed in to change notification settings - Fork 46.9k
/
index.js
156 lines (145 loc) · 3.95 KB
/
index.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, {PureComponent, unstable_AsyncMode} from 'react';
import {flushSync, render, unstable_deferredUpdates} from 'react-dom';
import _ from 'lodash';
import Charts from './Charts';
import Clock from './Clock';
import './index.css';
let cachedData = new Map();
class App extends PureComponent {
state = {
value: '',
strategy: 'sync',
showDemo: true,
showClock: false,
};
// Random data for the chart
getStreamData(input) {
if (cachedData.has(input)) {
return cachedData.get(input);
}
const multiplier = input.length !== 0 ? input.length : 1;
const complexity =
(parseInt(window.location.search.substring(1), 10) / 100) * 25 || 25;
const data = _.range(5).map(t =>
_.range(complexity * multiplier).map((j, i) => {
return {
x: j,
y: (t + 1) * _.random(0, 255),
};
})
);
cachedData.set(input, data);
return data;
}
componentDidMount() {
window.addEventListener('keydown', e => {
if (e.key.toLowerCase() === '?') {
e.preventDefault();
this.setState(state => ({
showClock: !state.showClock,
}));
}
});
}
handleChartClick = e => {
if (this.state.showDemo) {
if (e.shiftKey) {
this.setState({showDemo: false});
}
return;
}
if (this.state.strategy !== 'async') {
this.setState(state => ({
showDemo: !state.showDemo,
}));
return;
}
if (this._ignoreClick) {
return;
}
this._ignoreClick = true;
// TODO: needing setTimeout here seems like a React bug.
setTimeout(() => {
unstable_deferredUpdates(() => {
this.setState({showDemo: true}, () => {
this._ignoreClick = false;
});
});
});
};
debouncedHandleChange = _.debounce(value => {
if (this.state.strategy === 'debounced') {
flushSync(() => {
this.setState({value: value});
});
}
}, 1000);
renderOption(strategy, label) {
const {strategy: currentStrategy} = this.state;
return (
<label className={strategy === currentStrategy ? 'selected' : null}>
<input
type="radio"
checked={strategy === currentStrategy}
onChange={() => this.setState({strategy})}
/>
{label}
</label>
);
}
handleChange = e => {
const value = e.target.value;
const {strategy} = this.state;
switch (strategy) {
case 'sync':
this.setState({value});
break;
case 'debounced':
this.debouncedHandleChange(value);
break;
case 'async':
// TODO: needing setTimeout here seems like a React bug.
setTimeout(() => {
unstable_deferredUpdates(() => {
this.setState({value});
});
});
break;
default:
break;
}
};
render() {
const Wrapper =
this.state.strategy === 'async' ? unstable_AsyncMode : 'div';
const {showClock} = this.state;
const data = this.getStreamData(this.state.value);
return (
<div className="container">
<div className="rendering">
{this.renderOption('sync', 'Synchronous')}
{this.renderOption('debounced', 'Debounced')}
{this.renderOption('async', 'Asynchronous')}
</div>
<input
className={'input ' + this.state.strategy}
placeholder="longer input → more components and DOM nodes"
defaultValue={this.state.input}
onChange={this.handleChange}
/>
<Wrapper>
<div className="demo" onClick={this.handleChartClick}>
{this.state.showDemo && (
<Charts data={data} onClick={this.handleChartClick} />
)}
<div style={{display: showClock ? 'block' : 'none'}}>
<Clock />
</div>
</div>
</Wrapper>
</div>
);
}
}
const container = document.getElementById('root');
render(<App />, container);