Skip to content

Commit 761f6ab

Browse files
committed
Rough first pass at the benchmark
1 parent 134e8c4 commit 761f6ab

File tree

5 files changed

+117
-8
lines changed

5 files changed

+117
-8
lines changed

frameworks/keyed/butterfloat/_build.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { writeFile } from 'node:fs/promises'
2-
import { buildStamp } from 'butterfloat'
2+
import { buildStamp, makeTestComponentContext, makeTestEvent } from 'butterfloat'
33
import { build } from 'esbuild'
44
import { JSDOM } from 'jsdom'
5+
import { NEVER } from 'rxjs'
56

67
await build({
7-
entryPoints: ['./app.tsx'],
8+
entryPoints: ['./app-vm.ts', './app.tsx', './data.ts', './row-vm.ts', './row.tsx'],
89
bundle: false,
910
format: 'esm',
1011
outdir: '.',
@@ -40,8 +41,28 @@ globalThis.document = document
4041
globalThis.window = window
4142

4243
const { App } = await import('./app.js')
43-
const appStamp = buildStamp(App(), document)
44+
const { context: appContext } = makeTestComponentContext({
45+
run: makeTestEvent(NEVER),
46+
runlots: makeTestEvent(NEVER),
47+
add: makeTestEvent(NEVER),
48+
update: makeTestEvent(NEVER),
49+
clear: makeTestEvent(NEVER),
50+
swaprows: makeTestEvent(NEVER),
51+
})
52+
const appStamp = buildStamp(App({}, appContext), document)
4453
appStamp.id = 'app'
4554
document.body.append(appStamp)
4655

56+
const { Row } = await import('./row.js')
57+
const { AppViewModel } = await import('./app-vm.js')
58+
const { RowViewModel } = await import('./row-vm.js')
59+
const vm = new RowViewModel(new AppViewModel(), -999)
60+
const { context: rowContext } = makeTestComponentContext({
61+
select: makeTestEvent(NEVER),
62+
remove: makeTestEvent(NEVER),
63+
})
64+
const rowStamp = buildStamp(Row({ vm }, rowContext), document)
65+
rowStamp.id = 'row'
66+
document.body.append(rowStamp)
67+
4768
await writeFile('./index.html', dom.serialize())

frameworks/keyed/butterfloat/app-vm.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { butterfly, StateSetter } from 'butterfloat'
22
import { filter, map, mergeMap, Observable, range } from 'rxjs'
3-
import { RowViewModel } from './row-vm'
3+
import { RowViewModel } from './row-vm.js'
44

55
export interface IdRange {
66
min: number
@@ -53,7 +53,15 @@ export class AppViewModel {
5353
this.#setSelectedId(id)
5454
}
5555

56-
addRows(count: number) {
56+
createRows(count: number) {
57+
this.#setIdRange((current) => {
58+
const min = current.max
59+
const max = current.max + count
60+
return { min, max, added: [current.max, count] }
61+
})
62+
}
63+
64+
appendRows(count: number) {
5765
this.#setIdRange((current) => {
5866
const min = current.min
5967
const max = current.max + count

frameworks/keyed/butterfloat/app.tsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
import { jsx } from 'butterfloat'
1+
import { ComponentContext, jsx, ObservableEvent } from 'butterfloat'
2+
import { AppViewModel } from './app-vm.js'
3+
import { Row } from './row.js'
4+
import { map } from 'rxjs'
5+
6+
interface AppEvents {
7+
run: ObservableEvent<MouseEvent>
8+
runlots: ObservableEvent<MouseEvent>
9+
add: ObservableEvent<MouseEvent>
10+
update: ObservableEvent<MouseEvent>
11+
clear: ObservableEvent<MouseEvent>
12+
swaprows: ObservableEvent<MouseEvent>
13+
}
14+
15+
export function App(
16+
_props: unknown,
17+
{ bindImmediateEffect, events }: ComponentContext<AppEvents>,
18+
) {
19+
const vm = new AppViewModel()
20+
21+
const children = vm.rows.pipe(map((row) => () => <Row vm={row} />))
22+
23+
bindImmediateEffect(events.run, () => {
24+
vm.createRows(1000)
25+
})
26+
bindImmediateEffect(events.runlots, () => {
27+
vm.createRows(10000)
28+
})
29+
bindImmediateEffect(events.add, () => vm.appendRows(1000))
30+
bindImmediateEffect(events.clear, () => vm.clear())
231

3-
export function App() {
432
return (
533
<div class="container">
634
<div class="jumbotron">
@@ -15,6 +43,7 @@ export function App() {
1543
type="button"
1644
class="btn btn-primary btn-block"
1745
id="run"
46+
events={{ click: events.run }}
1847
>
1948
Create 1,000 rows
2049
</button>
@@ -24,6 +53,7 @@ export function App() {
2453
type="button"
2554
class="btn btn-primary btn-block"
2655
id="runlots"
56+
events={{ click: events.runlots }}
2757
>
2858
Create 10,000 rows
2959
</button>
@@ -33,6 +63,7 @@ export function App() {
3363
type="button"
3464
class="btn btn-primary btn-block"
3565
id="add"
66+
events={{ click: events.add }}
3667
>
3768
Append 1,000 rows
3869
</button>
@@ -42,6 +73,7 @@ export function App() {
4273
type="button"
4374
class="btn btn-primary btn-block"
4475
id="update"
76+
events={{ click: events.update }}
4577
>
4678
Update every 10th row
4779
</button>
@@ -51,6 +83,7 @@ export function App() {
5183
type="button"
5284
class="btn btn-primary btn-block"
5385
id="clear"
86+
events={{ click: events.clear }}
5487
>
5588
Clear
5689
</button>
@@ -60,6 +93,7 @@ export function App() {
6093
type="button"
6194
class="btn btn-primary btn-block"
6295
id="swaprows"
96+
events={{ click: events.swaprows }}
6397
>
6498
Swap Rows
6599
</button>
@@ -69,7 +103,11 @@ export function App() {
69103
</div>
70104
</div>
71105
<table class="table table-hover table-striped test-data">
72-
<tbody id="tbody"></tbody>
106+
<tbody
107+
id="tbody"
108+
childrenBind={children}
109+
childrenBindMode="append"
110+
></tbody>
73111
</table>
74112
</div>
75113
)

frameworks/keyed/butterfloat/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { runStamps, StampCollection } from 'butterfloat'
22
import { App } from './app.js'
3+
import { Row } from './row.js'
34

45
const stamps = new StampCollection()
56

@@ -8,5 +9,10 @@ if (appStamp) {
89
stamps.registerOnlyStamp(App, appStamp)
910
}
1011

12+
const rowStamp = document.querySelector<HTMLTemplateElement>('template#row')
13+
if (rowStamp) {
14+
stamps.registerOnlyStamp(Row, rowStamp)
15+
}
16+
1117
const main = document.querySelector('#main')!
1218
runStamps(main, App, stamps)

frameworks/keyed/butterfloat/row.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ComponentContext, jsx, ObservableEvent } from 'butterfloat'
2+
import { RowViewModel } from './row-vm'
3+
import { of } from 'rxjs'
4+
5+
export interface RowProps {
6+
vm: RowViewModel
7+
}
8+
9+
export interface RowEvents {
10+
select: ObservableEvent<MouseEvent>
11+
remove: ObservableEvent<MouseEvent>
12+
}
13+
14+
export function Row(
15+
{ vm }: RowProps,
16+
{ bindImmediateEffect, events }: ComponentContext<RowEvents>,
17+
) {
18+
bindImmediateEffect(vm.alive, () => {})
19+
bindImmediateEffect(events.select, () => vm.select())
20+
bindImmediateEffect(events.remove, () => vm.remove())
21+
22+
return (
23+
<tr classBind={{ danger: vm.selected }}>
24+
<td class="col-md-1" bind={{ innerText: of(vm.id.toString()) }}></td>
25+
<td class="col-md-4">
26+
<a bind={{ innerText: vm.label }} events={{ click: events.select }}></a>
27+
</td>
28+
<td class="col-md-1">
29+
<a events={{ click: events.remove }}>
30+
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
31+
</a>
32+
</td>
33+
<td class="col-md-6"></td>
34+
</tr>
35+
)
36+
}

0 commit comments

Comments
 (0)