Skip to content

Commit 063ffab

Browse files
committed
Merge branch 'jmeistrich-master'
2 parents 246a077 + fb8560c commit 063ffab

File tree

10 files changed

+4020
-65
lines changed

10 files changed

+4020
-65
lines changed

frameworks/non-keyed/legend-state/package-lock.json renamed to frameworks/keyed/legend-state/package-lock.json

Lines changed: 16 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/non-keyed/legend-state/package.json renamed to frameworks/keyed/legend-state/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-framework-benchmark-legend-state",
3-
"version": "0.14.0",
3+
"version": "1.2.8",
44
"description": "Legend-State demo",
55
"main": "index.js",
66
"sideEffects": false,
@@ -34,7 +34,7 @@
3434
"webpack-cli": "5.0.1"
3535
},
3636
"dependencies": {
37-
"@legendapp/state": "0.14.0",
37+
"@legendapp/state": "1.2.9",
3838
"react": "17.0.1",
3939
"react-dom": "17.0.1"
4040
}

frameworks/non-keyed/legend-state/src/main.jsx renamed to frameworks/keyed/legend-state/src/main.jsx

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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";
33
import React, { memo } from "react";
44
import ReactDOM from "react-dom";
55

@@ -13,10 +13,9 @@ const N = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "s
1313
const random = (max) => Math.round(Math.random() * 1000) % max;
1414

1515
let nextId = 1;
16-
function buildData (count) {
16+
function buildData(count) {
1717
let data = new Array(count);
18-
for (let i = 0; i < count; i++)
19-
{
18+
for (let i = 0; i < count; i++) {
2019
data[i] = {
2120
id: nextId++,
2221
label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)]}`,
@@ -26,69 +25,66 @@ function buildData (count) {
2625
}
2726

2827
let selectedItem = undefined;
29-
const state = observable({ data: [], num: 2 });
28+
const arr$ = observable([]);
3029

31-
function run () {
32-
state.data.set(buildData(1000));
30+
function run() {
31+
arr$.set(buildData(1000));
3332
}
34-
function runLots () {
35-
state.data.set(buildData(10000));
33+
function runLots() {
34+
arr$.set(buildData(10000));
3635
}
37-
function add () {
38-
state.data.set(state.data.concat(buildData(1000)));
36+
function add() {
37+
arr$.set(arr$.concat(buildData(1000)));
3938
}
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 + " !!!");
4844
}
49-
observableBatcher.end();
45+
endBatch();
5046
}
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);
5450
}
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);
5954
}
60-
item.set('selected', true);
55+
item.selected.set(true);
6156
selectedItem = item;
6257
}
63-
function swapRows () {
64-
const arr = state.data.slice();
58+
function swapRows() {
59+
const arr = arr$.slice();
6560
const tmp = arr[1];
6661
arr[1] = arr[998];
6762
arr[998] = tmp;
68-
state.data.set(arr);
63+
arr$.set(arr);
6964
}
70-
function clear () {
71-
state.data.set([]);
65+
function clear() {
66+
arr$.set([]);
7267
}
7368

7469
const GlyphIcon = <span className="glyphicon glyphicon-remove" aria-hidden="true"></span>;
7570

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;
7874

7975
return (
8076
<tr className={selected ? "danger" : ""}>
8177
<td className="col-md-1">{id}</td>
8278
<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>
8480
<td className="col-md-6"></td>
8581
</tr>
8682
)
87-
});
83+
}
8884

89-
const RowList = observer(() => {
90-
return <For each={state.data} item={Row} />;
91-
});
85+
const RowList = () => {
86+
return <For each={arr$} item={Row} />;
87+
};
9288

9389
const Button = memo(({ id, title, cb }) => (
9490
<div className="col-sm-6 smallpad">
@@ -101,7 +97,7 @@ const Main = () => {
10197
<div className="container">
10298
<div className="jumbotron">
10399
<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>
105101
<div className="col-md-6"><div className="row">
106102
<Button id="run" title="Create 1,000 rows" cb={run} />
107103
<Button id="runlots" title="Create 10,000 rows" cb={runLots} />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Legend-State</title>
6+
<link href="/css/currentStyle.css" rel="stylesheet"/>
7+
</head>
8+
<body>
9+
<div id='main'></div>
10+
<script src='dist/main.js'></script>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)