@@ -63,8 +63,8 @@ const random = (max) => Math.round(Math.random() * 1000) % max;
63
63
64
64
let nextId = 1 ;
65
65
let list = [ ] ;
66
- let main ;
67
66
let selected = 0 ;
67
+ let main ;
68
68
69
69
const clear = ( ) => {
70
70
list = [ ] ;
@@ -85,44 +85,44 @@ const buildData = (count) => {
85
85
return data ;
86
86
} ;
87
87
88
- const create1k = ( event ) => {
89
- event . stopPropagation ( ) ;
88
+ const create1k = ( ) => {
90
89
if ( list . length ) clear ( ) ;
91
90
list = buildData ( 1000 ) ;
92
91
update ( ) ;
92
+ return false ;
93
93
} ;
94
94
95
- const create10k = ( event ) => {
96
- event . stopPropagation ( ) ;
95
+ const create10k = ( ) => {
97
96
if ( list . length ) clear ( ) ;
98
97
list = buildData ( 10000 ) ;
99
98
update ( ) ;
99
+ return false ;
100
100
} ;
101
101
102
- const append1k = ( event ) => {
103
- event . stopPropagation ( ) ;
102
+ const append1k = ( ) => {
104
103
list = list . concat ( buildData ( 1000 ) ) ;
105
104
update ( ) ;
105
+ return false ;
106
106
} ;
107
107
108
- const updateEvery10 = ( event ) => {
109
- event . stopPropagation ( ) ;
108
+ const updateEvery10 = ( ) => {
110
109
let i = 0 ;
111
110
while ( i < list . length ) {
112
111
list [ i ] . label = `${ list [ i ] . label } !!!` ;
113
112
i += 10 ;
114
113
}
115
114
update ( ) ;
115
+ return false ;
116
116
} ;
117
117
118
- const swapRows = ( event ) => {
119
- event . stopPropagation ( ) ;
118
+ const swapRows = ( ) => {
120
119
if ( list . length > 998 ) {
121
120
const item = list [ 1 ] ;
122
121
list [ 1 ] = list [ 998 ] ;
123
122
list [ 998 ] = item ;
124
123
}
125
124
update ( ) ;
125
+ return false ;
126
126
} ;
127
127
128
128
const select = ( id ) => {
@@ -132,19 +132,12 @@ const select = (id) => {
132
132
133
133
const remove = ( id ) => {
134
134
list . splice (
135
- list . findIndex ( ( z ) => z . id === id ) ,
135
+ list . findIndex ( ( item ) => item . id === id ) ,
136
136
1
137
137
) ;
138
138
update ( ) ;
139
139
} ;
140
140
141
- const shouldUpdate = ( oldProps , newProps ) => {
142
- return (
143
- oldProps . label !== newProps . label ||
144
- oldProps . className !== newProps . className
145
- ) ;
146
- } ;
147
-
148
141
const Main = createBlock ( ( { rows } ) => (
149
142
< div class = "container" >
150
143
< div class = "jumbotron" >
@@ -199,10 +192,10 @@ const Main = createBlock(({ rows }) => (
199
192
type = "button"
200
193
class = "btn btn-primary btn-block"
201
194
id = "clear"
202
- onClick = { ( event ) => {
203
- event . stopPropagation ( ) ;
195
+ onClick = { ( ) => {
204
196
clear ( ) ;
205
197
update ( ) ;
198
+ return false ;
206
199
} }
207
200
>
208
201
Clear
@@ -232,66 +225,82 @@ const Main = createBlock(({ rows }) => (
232
225
</ div >
233
226
) ) ;
234
227
235
- const Row = createBlock ( ( { className, id, select, remove, label } ) => {
236
- return (
237
- < tr class = { className } >
238
- < td class = "col-md-1" > { id } </ td >
239
- < td class = "col-md-4" >
240
- < a onClick = { select } > { label } </ a >
241
- </ td >
242
- < td class = "col-md-1" >
243
- < a onClick = { remove } >
244
- < span class = "glyphicon glyphicon-remove" aria-hidden = "true" > </ span >
245
- </ a >
246
- </ td >
247
- < td class = "col-md-6" > </ td >
248
- </ tr >
249
- ) ;
250
- } ) ;
228
+ const Row = createBlock ( ( { className, id, select, remove, label } ) => (
229
+ < tr class = { className } >
230
+ < td class = "col-md-1" > { id } </ td >
231
+ < td class = "col-md-4" >
232
+ < a onClick = { select } > { label } </ a >
233
+ </ td >
234
+ < td class = "col-md-1" >
235
+ < a onClick = { remove } >
236
+ < span class = "glyphicon glyphicon-remove" aria-hidden = "true" > </ span >
237
+ </ a >
238
+ </ td >
239
+ < td class = "col-md-6" > </ td >
240
+ </ tr >
241
+ ) ) ;
242
+
243
+ const shouldUpdate = ( oldProps , newProps ) => {
244
+ return oldProps . valueKey !== newProps . valueKey ;
245
+ } ;
251
246
252
- function Rows ( { oldCache, newCache } ) {
247
+ const cache = ( map , key , listener ) => {
248
+ if ( has$ . call ( map , key ) ) {
249
+ return get$ . call ( selectCache , key ) ;
250
+ } else {
251
+ set$ . call ( map , key , listener ) ;
252
+ return listener ;
253
+ }
254
+ } ;
255
+
256
+ const removeCache = new Map ( ) ;
257
+ const selectCache = new Map ( ) ;
258
+
259
+ const Map$ = Map . prototype ;
260
+ const has$ = Map$ . has ;
261
+ const get$ = Map$ . get ;
262
+ const set$ = Map$ . set ;
263
+
264
+ function render ( oldCache , newCache ) {
253
265
return fragment (
254
266
list . map ( ( item ) => {
255
267
const isSelected = selected === item . id ;
256
- const cachedItem = oldCache [ item . id ] ;
257
- if ( cachedItem ) {
258
- const [ cachedLabel , cachedIsSelected ] = cachedItem . _data ;
259
- if ( cachedLabel === item . label && cachedIsSelected === isSelected ) {
260
- return ( newCache [ item . id ] = cachedItem ) ;
261
- }
268
+ const key = String ( item . id ) ;
269
+ const cachedItem = oldCache [ key ] ;
270
+ const valueKey = `${ item . label } ${ isSelected } ` ;
271
+ if ( cachedItem ?. props . valueKey === valueKey ) {
272
+ return ( newCache [ key ] = cachedItem ) ;
262
273
}
263
- const row = (
264
- < Row
265
- id = { item . id }
266
- label = { item . label }
267
- className = { isSelected ? 'danger' : '' }
268
- remove = { ( event ) => {
269
- event . stopPropagation ( ) ;
274
+
275
+ const row = Row (
276
+ {
277
+ id : item . id ,
278
+ label : item . label ,
279
+ className : isSelected ? 'danger' : '' ,
280
+ remove : cache ( removeCache , key , ( ) => {
270
281
remove ( item . id ) ;
271
- } }
272
- select = { ( event ) => {
273
- event . stopPropagation ( ) ;
282
+ return false ;
283
+ } ) ,
284
+ select : cache ( selectCache , key , ( ) => {
274
285
select ( item . id ) ;
275
- } }
276
- />
286
+ return false ;
287
+ } ) ,
288
+ valueKey,
289
+ } ,
290
+ key ,
291
+ shouldUpdate
277
292
) ;
278
- row . _data = [ item . label , isSelected ] ;
279
- row . key = String ( item . id ) ;
280
- row . shouldUpdate = shouldUpdate ;
281
- newCache [ item . id ] = row ;
293
+ newCache [ key ] = row ;
282
294
return row ;
283
295
} )
284
296
) ;
285
297
}
286
298
287
- function render ( oldCache , newCache ) {
288
- return < Rows oldCache = { oldCache } newCache = { newCache } /> ;
289
- }
290
-
291
299
let oldCache = { } ;
292
300
293
- main = render ( { } , oldCache ) ;
294
- ( < Main rows = { main } /> ) . mount ( document . getElementById ( 'main' ) ) ;
301
+ Main ( { rows : ( main = render ( { } , oldCache ) ) } ) . mount (
302
+ document . getElementById ( 'main' )
303
+ ) ;
295
304
296
305
function update ( ) {
297
306
let newCache = { } ;
0 commit comments