1
- import { observable , observableBatcher } from "@legendapp/state" ;
2
- import { observer , For } from "@legendapp/state/react" ;
1
+ import { beginBatch , endBatch , observable } from "@legendapp/state" ;
2
+ import { For , useSelector } from "@legendapp/state/react" ;
3
3
import React , { memo } from "react" ;
4
4
import ReactDOM from "react-dom" ;
5
5
@@ -13,10 +13,9 @@ const N = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "s
13
13
const random = ( max ) => Math . round ( Math . random ( ) * 1000 ) % max ;
14
14
15
15
let nextId = 1 ;
16
- function buildData ( count ) {
16
+ function buildData ( count ) {
17
17
let data = new Array ( count ) ;
18
- for ( let i = 0 ; i < count ; i ++ )
19
- {
18
+ for ( let i = 0 ; i < count ; i ++ ) {
20
19
data [ i ] = {
21
20
id : nextId ++ ,
22
21
label : `${ A [ random ( A . length ) ] } ${ C [ random ( C . length ) ] } ${ N [ random ( N . length ) ] } ` ,
@@ -26,69 +25,66 @@ function buildData (count) {
26
25
}
27
26
28
27
let selectedItem = undefined ;
29
- const state = observable ( { data : [ ] , num : 2 } ) ;
28
+ const arr$ = observable ( [ ] ) ;
30
29
31
- function run ( ) {
32
- state . data . set ( buildData ( 1000 ) ) ;
30
+ function run ( ) {
31
+ arr$ . set ( buildData ( 1000 ) ) ;
33
32
}
34
- function runLots ( ) {
35
- state . data . set ( buildData ( 10000 ) ) ;
33
+ function runLots ( ) {
34
+ arr$ . set ( buildData ( 10000 ) ) ;
36
35
}
37
- function add ( ) {
38
- state . data . set ( state . data . concat ( buildData ( 1000 ) ) ) ;
36
+ function add ( ) {
37
+ arr$ . set ( arr$ . concat ( buildData ( 1000 ) ) ) ;
39
38
}
40
- function update ( ) {
41
- observableBatcher . begin ( ) ;
42
- const list = state . data ;
43
- const length = list . length ;
44
- for ( let i = 0 ; i < length ; i += 10 )
45
- {
46
- const r = list [ i ] ;
47
- list [ i ] . label . set ( r . label + " !!!" ) ;
39
+ function update ( ) {
40
+ beginBatch ( ) ;
41
+ const length = arr$ . length ;
42
+ for ( let i = 0 ; i < length ; i += 10 ) {
43
+ arr$ [ i ] . label . set ( v => v + " !!!" ) ;
48
44
}
49
- observableBatcher . end ( ) ;
45
+ endBatch ( ) ;
50
46
}
51
- function remove ( id ) {
52
- const idx = state . data . findIndex ( ( d ) => d ?. id === id ) ;
53
- state . data . splice ( idx , 1 ) ;
47
+ function remove ( itemData ) {
48
+ const idx = arr$ . peek ( ) . indexOf ( itemData ) ;
49
+ arr$ . splice ( idx , 1 ) ;
54
50
}
55
- function select ( item ) {
56
- if ( selectedItem !== undefined )
57
- {
58
- selectedItem . set ( 'selected' , false ) ;
51
+ function select ( item ) {
52
+ if ( selectedItem !== undefined ) {
53
+ selectedItem . selected . set ( false ) ;
59
54
}
60
- item . set ( 'selected' , true ) ;
55
+ item . selected . set ( true ) ;
61
56
selectedItem = item ;
62
57
}
63
- function swapRows ( ) {
64
- const arr = state . data . slice ( ) ;
58
+ function swapRows ( ) {
59
+ const arr = arr$ . slice ( ) ;
65
60
const tmp = arr [ 1 ] ;
66
61
arr [ 1 ] = arr [ 998 ] ;
67
62
arr [ 998 ] = tmp ;
68
- state . data . set ( arr ) ;
63
+ arr$ . set ( arr ) ;
69
64
}
70
- function clear ( ) {
71
- state . data . set ( [ ] ) ;
65
+ function clear ( ) {
66
+ arr$ . set ( [ ] ) ;
72
67
}
73
68
74
69
const GlyphIcon = < span className = "glyphicon glyphicon-remove" aria-hidden = "true" > </ span > ;
75
70
76
- const Row = observer ( ( { item } ) => {
77
- const { label, selected, id } = item . get ( ) ;
71
+ const Row = ( { item } ) => {
72
+ const itemData = useSelector ( item ) ;
73
+ const { label, selected, id } = itemData ;
78
74
79
75
return (
80
76
< tr className = { selected ? "danger" : "" } >
81
77
< td className = "col-md-1" > { id } </ td >
82
78
< td className = "col-md-4" > < a onClick = { ( ) => select ( item ) } > { label } </ a > </ td >
83
- < td className = "col-md-1" > < a onClick = { ( ) => remove ( id ) } > { GlyphIcon } </ a > </ td >
79
+ < td className = "col-md-1" > < a onClick = { ( ) => remove ( itemData ) } > { GlyphIcon } </ a > </ td >
84
80
< td className = "col-md-6" > </ td >
85
81
</ tr >
86
82
)
87
- } ) ;
83
+ }
88
84
89
- const RowList = observer ( ( ) => {
90
- return < For each = { state . data } item = { Row } /> ;
91
- } ) ;
85
+ const RowList = ( ) => {
86
+ return < For each = { arr$ } item = { Row } /> ;
87
+ } ;
92
88
93
89
const Button = memo ( ( { id, title, cb } ) => (
94
90
< div className = "col-sm-6 smallpad" >
@@ -101,7 +97,7 @@ const Main = () => {
101
97
< div className = "container" >
102
98
< div className = "jumbotron" >
103
99
< div className = "row" >
104
- < div className = "col-md-6" > < h1 > Legend-State v0.14.0 </ h1 > </ div >
100
+ < div className = "col-md-6" > < h1 > Legend-State v1.2.9 </ h1 > </ div >
105
101
< div className = "col-md-6" > < div className = "row" >
106
102
< Button id = "run" title = "Create 1,000 rows" cb = { run } />
107
103
< Button id = "runlots" title = "Create 10,000 rows" cb = { runLots } />
0 commit comments