-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathmutations.html
More file actions
170 lines (148 loc) · 4.52 KB
/
mutations.html
File metadata and controls
170 lines (148 loc) · 4.52 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>hyperHTML Smart Diffing</title>
<style>
user-info {
display: block;
}
user-info small {
font-size: .7em;
}
div.cleared {
clear: left;
}
div > div {
min-width: 120px;
float: left;
}
</style>
<script src="../index.js"></script>
<script src="https://unpkg.com/ce-v0@latest/min.js"></script>
<script src="https://unpkg.com/ce-v0@latest/comp.js"></script>
</head>
<body>
<div class=cleared>
<div id="user-list-majinbuu"></div>
<div id="user-list-default"></div>
</div>
<div class=cleared>
<div id="logs-majinbuu"></div>
<div id="logs-default"></div>
</div>
</body>
<script>
var DELAY = 1000;
var AMOUNT = 500;
var target = 'user-list-majinbuu';
var logs = 'logs-majinbuu';
// basic Custom Element
Component({
name: 'user-info',
// every time a node is inserted live on DOM
// it will log the information
onconnected() {
log('connected: ' + this.getAttribute('name'));
}
});
function nameGenerator(length) {
var name = [];
while (length--) {
name.push(65 + Math.floor(Math.random() * 58));
}
return String.fromCharCode.apply(null, name);
}
function usersGenerator(many) {
var users = [];
while (many--) {
users.push({
name: nameGenerator(10),
age: Math.floor(Math.random() * 100)
});
}
return users;
}
var perf = console.time ? console : {
time() { this._ = (new Date).getTime(); },
timeEnd() {
console.log((new Date).getTime() - this._);
}
};
var wire = hyperHTML.wire;
var users = [
{name: 'A', age: 34},
{name: 'B', age: 29},
{name: 'D', age: 50},
{name: 'C', age: 72},
{name: 'E', age: 14},
{name: 'F', age: 31}
];
// var users = usersGenerator(AMOUNT);
var initial = JSON.stringify(users);
window.results = {majinbuu: [], merge: []};
var log = users.length < 100 ?
function (text) {
window[logs].innerHTML += text + '<br>';
} :
function (text) {};
// first update, populate the body
log('<strong>DOM changes</strong>');
log('- - - - - - - - - -');
console.log('majinbuu');
update();
results.majinbuu.push(window[target].innerHTML);
// after one second
setTimeout(() => {
// sort the users list by name
users.sort((a, b) => a.name < b.name ? -1 : 1);
// update + see which user was moved around
update();
results.majinbuu.push(window[target].innerHTML);
// after another second
setTimeout(() => {
// sort the users list by age
users.sort((a, b) => a.age - b.age);
// update + see which user was moved around
update();
results.majinbuu.push(window[target].innerHTML);
setTimeout(() => {
target = 'user-list-default';
logs = 'logs-default';
log('<strong>DOM changes</strong>');
log('- - - - - - - - - -');
console.log('default');
users = JSON.parse(initial);
update();
results.merge.push(window[target].innerHTML);
setTimeout(() => {
users.sort((a, b) => a.name < b.name ? -1 : 1);
update();
results.merge.push(window[target].innerHTML);
setTimeout(() => {
users.sort((a, b) => a.age - b.age);
update();
results.merge.push(window[target].innerHTML);
console.assert(results.majinbuu.every((html, i) => html === results.merge[i]), 'same results');
}, DELAY);
}, DELAY);
setTimeout(() => {});
}, DELAY);
}, DELAY);
}, DELAY);
function update() {
if (users.length) log(`<small>${users.map(user => user.name).join(', ')}</small>`);
perf.time();
hyperHTML.bind(window[target])`${
users.map(user => wire(user)
`<user-info name=${user.name}>
<small>${user.age}</small> ${user.name}
</user-info>`
)
}`;
perf.timeEnd();
if (users.length) setTimeout(log, DELAY / 10, '- - - - - - - - - -');
}
</script>
</html>