Skip to content

Commit ee1481b

Browse files
committed
refactor: render pattern is the same as React
1 parent ddba0ab commit ee1481b

File tree

6 files changed

+176
-111
lines changed

6 files changed

+176
-111
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ yarn-error.log
4646
/devtools.js.map
4747
/debug.js
4848
/debug.js.map
49-
devtools/*.d.ts
49+
devtools/*.d.ts
50+
build.js

benchmarks/uibench/app.js

Lines changed: 124 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,198 @@
1+
import { Component, createElement, render } from 'nervjs'
12

2-
class TableCell extends Nerv.PureComponent {
3+
class TableCell extends Component {
34
constructor(props) {
4-
super(props);
5-
this.onClick = this.onClick.bind(this);
5+
super(props)
6+
this.onClick = this.onClick.bind(this)
7+
}
8+
9+
shouldComponentUpdate(nextProps, nextState) {
10+
return this.props.text !== nextProps.text
611
}
712

813
onClick(e) {
9-
console.log('Clicked' + this.props.text);
10-
e.stopPropagation();
14+
console.log('Clicked' + this.props.text)
15+
e.stopPropagation()
1116
}
1217

1318
render() {
14-
return Nerv.createElement(
15-
'td',
16-
{ className: 'TableCell', onClick: this.onClick },
17-
this.props.text
18-
);
19+
return (
20+
<td className="TableCell" onClick={this.onClick}>
21+
{this.props.text}
22+
</td>
23+
)
1924
}
2025
}
2126

22-
class TableRow extends Nerv.PureComponent {
27+
class TableRow extends Component {
28+
shouldComponentUpdate(nextProps, nextState) {
29+
return this.props.data !== nextProps.data
30+
}
31+
2332
render() {
24-
const { data } = this.props;
33+
const { data } = this.props
2534

2635
// Interned strings
27-
const classes = data.active ? 'TableRow active' : 'TableRow';
36+
const classes = data.active ? 'TableRow active' : 'TableRow'
2837

29-
const cells = data.props;
38+
const cells = data.props
3039

31-
const children = [];
40+
const children = []
3241
for (let i = 0; i < cells.length; i++) {
33-
// Key is used because Nerv prints warnings that there should be a key, libraries that can detect that children
42+
// Key is used because React prints warnings that there should be a key, libraries that can detect that children
3443
// shape isn't changing should render cells without keys.
35-
children.push(Nerv.createElement(TableCell, { key: i, text: cells[i] }));
44+
children.push(<TableCell key={i} text={cells[i]} />)
3645
}
3746

38-
// First table cell is inserted this way to prevent Nerv from printing warning that it doesn't have key property
39-
return Nerv.createElement(
40-
'tr',
41-
{ className: classes, 'data-id': data.id },
42-
Nerv.createElement(TableCell, { text: '#' + data.id }),
43-
children
44-
);
47+
// First table cell is inserted this way to prevent react from printing warning that it doesn't have key property
48+
return (
49+
<tr className={classes} data-id={data.id}>
50+
<TableCell text={'#' + data.id} />
51+
{children}
52+
</tr>
53+
)
4554
}
4655
}
4756

48-
class Table extends Nerv.PureComponent {
57+
class Table extends Component {
58+
shouldComponentUpdate(nextProps, nextState) {
59+
return this.props.data !== nextProps.data
60+
}
61+
4962
render() {
50-
const items = this.props.data.items;
63+
const items = this.props.data.items
5164

52-
const children = [];
65+
const children = []
5366
for (let i = 0; i < items.length; i++) {
54-
const item = items[i];
55-
children.push(Nerv.createElement(TableRow, { key: item.id, data: item }));
67+
const item = items[i]
68+
children.push(<TableRow key={item.id} data={item} />)
5669
}
5770

58-
return Nerv.createElement(
59-
'table',
60-
{ className: 'Table' },
61-
Nerv.createElement(
62-
'tbody',
63-
null,
64-
children
65-
)
66-
);
71+
return (
72+
<table className="Table">
73+
<tbody>{children}</tbody>
74+
</table>
75+
)
6776
}
6877
}
6978

70-
class AnimBox extends Nerv.PureComponent {
79+
class AnimBox extends Component {
80+
shouldComponentUpdate(nextProps, nextState) {
81+
return this.props.data !== nextProps.data
82+
}
83+
7184
render() {
72-
const { data } = this.props;
73-
const time = data.time;
85+
const { data } = this.props
86+
const time = data.time
7487
const style = {
75-
'borderRadius': (time % 10).toString() + 'px',
76-
'background': 'rgba(0,0,0,' + (0.5 + time % 10 / 10).toString() + ')'
77-
};
88+
borderRadius: (time % 10).toString() + 'px',
89+
background: 'rgba(0,0,0,' + (0.5 + (time % 10) / 10).toString() + ')'
90+
}
7891

79-
return Nerv.createElement('div', { className: 'AnimBox', 'data-id': data.id, style: style });
92+
return <div className="AnimBox" data-id={data.id} style={style} />
8093
}
8194
}
8295

83-
class Anim extends Nerv.PureComponent {
96+
class Anim extends Component {
97+
shouldComponentUpdate(nextProps, nextState) {
98+
return this.props.data !== nextProps.data
99+
}
100+
84101
render() {
85-
const items = this.props.data.items;
102+
const items = this.props.data.items
86103

87-
const children = [];
104+
const children = []
88105
for (let i = 0; i < items.length; i++) {
89-
const item = items[i];
90-
children.push(Nerv.createElement(AnimBox, { key: item.id, data: item }));
106+
const item = items[i]
107+
children.push(<AnimBox key={item.id} data={item} />)
91108
}
92109

93-
return Nerv.createElement(
94-
'div',
95-
{ className: 'Anim' },
96-
children
97-
);
110+
return <div className="Anim">{children}</div>
98111
}
99112
}
100113

101-
class TreeLeaf extends Nerv.PureComponent {
114+
class TreeLeaf extends Component {
115+
shouldComponentUpdate(nextProps, nextState) {
116+
return this.props.data !== nextProps.data
117+
}
118+
102119
render() {
103-
return Nerv.createElement(
104-
'li',
105-
{ className: 'TreeLeaf' },
106-
this.props.data.id
107-
);
120+
return <li className="TreeLeaf">{this.props.data.id}</li>
108121
}
109122
}
110123

111-
class TreeNode extends Nerv.PureComponent {
124+
class TreeNode extends Component {
125+
shouldComponentUpdate(nextProps, nextState) {
126+
return this.props.data !== nextProps.data
127+
}
128+
112129
render() {
113-
const { data } = this.props;
114-
const children = [];
130+
const { data } = this.props
131+
const children = []
115132

116133
for (let i = 0; i < data.children.length; i++) {
117-
const n = data.children[i];
134+
const n = data.children[i]
118135
if (n.container) {
119-
children.push(Nerv.createElement(TreeNode, { key: n.id, data: n }));
136+
children.push(<TreeNode key={n.id} data={n} />)
120137
} else {
121-
children.push(Nerv.createElement(TreeLeaf, { key: n.id, data: n }));
138+
children.push(<TreeLeaf key={n.id} data={n} />)
122139
}
123140
}
124141

125-
return Nerv.createElement(
126-
'ul',
127-
{ className: 'TreeNode' },
128-
children
129-
);
142+
return <ul className="TreeNode">{children}</ul>
130143
}
131144
}
132145

133-
class Tree extends Nerv.PureComponent {
146+
class Tree extends Component {
147+
shouldComponentUpdate(nextProps, nextState) {
148+
return this.props.data !== nextProps.data
149+
}
150+
134151
render() {
135-
return Nerv.createElement(
136-
'div',
137-
{ className: 'Tree' },
138-
Nerv.createElement(TreeNode, { data: this.props.data.root })
139-
);
152+
return (
153+
<div className="Tree">
154+
<TreeNode data={this.props.data.root} />
155+
</div>
156+
)
140157
}
141158
}
142159

143-
class Main extends Nerv.PureComponent {
160+
class Main extends Component {
161+
shouldComponentUpdate(nextProps, nextState) {
162+
return this.props.data !== nextProps.data
163+
}
164+
144165
render() {
145-
const { data } = this.props;
146-
const location = data.location;
166+
const { data } = this.props
167+
const location = data.location
147168

148-
var section;
169+
var section
149170
if (location === 'table') {
150-
section = Nerv.createElement(Table, { data: data.table });
171+
section = <Table data={data.table} />
151172
} else if (location === 'anim') {
152-
section = Nerv.createElement(Anim, { data: data.anim });
173+
section = <Anim data={data.anim} />
153174
} else if (location === 'tree') {
154-
section = Nerv.createElement(Tree, { data: data.tree });
175+
section = <Tree data={data.tree} />
155176
}
156177

157-
return Nerv.createElement(
158-
'div',
159-
{ className: 'Main' },
160-
section
161-
);
178+
return <div className="Main">{section}</div>
162179
}
163180
}
164181

165-
uibench.init('Nerv[PC]', Nerv.version);
166-
167-
document.addEventListener('DOMContentLoaded', function (e) {
168-
const container = document.querySelector('#App');
169-
170-
uibench.run(function (state) {
171-
Nerv.render(Nerv.createElement(Main, { data: state }), container);
172-
}, function (samples) {
173-
Nerv.render(Nerv.createElement(
174-
'pre',
175-
null,
176-
JSON.stringify(samples, null, ' ')
177-
), container);
178-
});
179-
});
182+
uibench.init('Nerv', '1.0.0-alpha')
183+
184+
document.addEventListener('DOMContentLoaded', function(e) {
185+
const container = document.querySelector('#App')
186+
187+
uibench.run(
188+
function(state) {
189+
render(<Main data={state} />, container)
190+
},
191+
function(samples) {
192+
render(
193+
<pre>{JSON.stringify(samples, null, ' ')}</pre>,
194+
container
195+
)
196+
}
197+
)
198+
})

benchmarks/uibench/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
<body>
1111
<div id="App"></div>
1212
<script src="https://localvoid.github.io/uibench-base/0.1.0/uibench.js"></script>
13-
<script src="../../dist/nerv.min.js"></script>
14-
<script src="app.js"></script>
13+
<script src="build.js"></script>
1514
</body>
1615

1716
</html>

benchmarks/uibench/webpack.config.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const path = require('path')
2+
const webpack = require('webpack')
3+
4+
module.exports = {
5+
resolve: {
6+
alias: {
7+
nervjs: path.join(__dirname, '..', '..', './packages', 'nerv', 'dist', 'nerv.min.js')
8+
},
9+
extensions: ['.js', '.jsx']
10+
},
11+
entry: {
12+
main: path.join(__dirname, 'app.js')
13+
},
14+
output: {
15+
path: path.join(__dirname),
16+
filename: 'build.js'
17+
},
18+
module: {
19+
rules: [
20+
{
21+
test: /\.js$/,
22+
loader: 'babel-loader',
23+
exclude: /node_modules/
24+
}
25+
]
26+
},
27+
plugins: [
28+
new webpack.DefinePlugin({
29+
'process.env': { NODE_ENV: JSON.stringify('production') }
30+
}),
31+
new webpack.optimize.UglifyJsPlugin()
32+
]
33+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"lint-staged": "lint-staged",
1717
"test": "jest",
1818
"test:watch": "jest --watch",
19+
"bench": "webpack --config ./benchmarks/uibench/webpack.config.js",
1920
"test:coverage": "jest --coverage",
2021
"test:karma": "karma start karma.conf.js --single-run",
2122
"test:karma:watch": "npm run test:karma -- no-single-run",

0 commit comments

Comments
 (0)