-
-
Notifications
You must be signed in to change notification settings - Fork 640
Expand file tree
/
Copy pathapp.js
More file actions
194 lines (158 loc) · 4.55 KB
/
Copy pathapp.js
File metadata and controls
194 lines (158 loc) · 4.55 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { version, Component, render } from 'inferno';
uibench.init('Inferno [same as react]', version);
class TableCell extends Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}
shouldComponentUpdate(nextProps, nextState) {
return this.props.text !== nextProps.text;
}
onClick(e) {
console.log('Clicked' + this.props.text);
e.stopPropagation();
}
render() {
return (
<td className="TableCell" onClick={this.onClick}>
{this.props.text}
</td>
);
}
}
class TableRow extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
const { data } = this.props;
// Interned strings
const classes = data.active ? 'TableRow active' : 'TableRow';
const cells = data.props;
const children = [<TableCell key={-1} text={'#' + data.id}></TableCell>];
for (let i = 0; i < cells.length; i++) {
// Key is used because React prints warnings that there should be a key, libraries that can detect that children
// shape isn't changing should render cells without keys.
children.push(<TableCell key={i} text={cells[i]}></TableCell>);
}
// First table cell is inserted this way to prevent react from printing warning that it doesn't have key property
return (
<tr className={classes} data-id={data.id}>
{children}
</tr>
);
}
}
class Table extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
const items = this.props.data.items;
const children = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
children.push(<TableRow key={item.id} data={item} />);
}
return (
<table className="Table">
<tbody>{children}</tbody>
</table>
);
}
}
class AnimBox extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
const { data } = this.props;
const time = data.time;
const style = {
'border-radius': (time % 10).toString() + 'px',
background: 'rgba(0,0,0,' + (0.5 + (time % 10) / 10).toString() + ')'
};
return <div className="AnimBox" data-id={data.id} style={style} />;
}
}
class Anim extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
const items = this.props.data.items;
const children = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
children.push(<AnimBox key={item.id} data={item} />);
}
return <div className="Anim">{children}</div>;
}
}
class TreeLeaf extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
return <li className="TreeLeaf">{this.props.data.id}</li>;
}
}
class TreeNode extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
const { data } = this.props;
const children = [];
for (let i = 0; i < data.children.length; i++) {
const n = data.children[i];
if (n.container) {
children.push(<TreeNode key={n.id} data={n} />);
} else {
children.push(<TreeLeaf key={n.id} data={n} />);
}
}
return <ul className="TreeNode">{children}</ul>;
}
}
class Tree extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
return (
<div className="Tree">
<TreeNode data={this.props.data.root} />
</div>
);
}
}
class Main extends Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.data !== nextProps.data;
}
render() {
const { data } = this.props;
const location = data.location;
var section;
if (location === 'table') {
section = <Table data={data.table}></Table>;
} else if (location === 'anim') {
section = <Anim data={data.anim}></Anim>;
} else if (location === 'tree') {
section = <Tree data={data.tree}></Tree>;
}
return <div className="Main">{section}</div>;
}
}
document.addEventListener('DOMContentLoaded', function (e) {
var container = document.querySelector('#App');
uibench.run(
function (state) {
render(<Main data={state} />, container);
},
function (samples) {
render(<pre>{JSON.stringify(samples, null, ' ')}</pre>, container);
}
);
});