@svgrid/grid 2.6.21 → 2.6.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +62 -0
- package/README.md +22 -0
- package/dist/GridMenus.svelte +17 -12
- package/dist/SvGrid.controller.svelte.d.ts +13 -8
- package/dist/SvGrid.controller.svelte.js +150 -72
- package/dist/SvGrid.css +1 -1
- package/dist/SvGrid.svelte +110 -56
- package/dist/SvGrid.types.d.ts +41 -1
- package/dist/cdn/{GridMenus-BfTAKn84.js → GridMenus-BuoBPqxx.js} +137 -132
- package/dist/cdn/GridMenus-n4llxoOI.js +494 -0
- package/dist/cdn/column-resize-DsfNXMom.js +102 -0
- package/dist/cdn/row-resize-BRcimkUT.js +95 -0
- package/dist/cdn/{src-BYq-qyrp.js → src-C9Hihx1W.js} +3456 -3459
- package/dist/cdn/{src-DBel9wRZ.js → src-D1lXwq1l.js} +8283 -8286
- package/dist/cdn/svgrid.js +10 -8
- package/dist/cdn/svgrid.svelte-external.js +10 -8
- package/dist/column-groups.js +1 -1
- package/dist/column-resize.d.ts +46 -0
- package/dist/column-resize.js +205 -0
- package/dist/columns.d.ts +0 -3
- package/dist/columns.js +0 -57
- package/dist/core.d.ts +19 -4
- package/dist/core.js +460 -119
- package/dist/filtering/excel-filters.js +28 -0
- package/dist/group-display.d.ts +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +6 -0
- package/dist/menus.js +1 -1
- package/dist/row-resize.d.ts +11 -0
- package/dist/row-resize.js +7 -1
- package/dist/selection.js +9 -0
- package/dist/spreadsheet.d.ts +1 -1
- package/dist/spreadsheet.js +1 -1
- package/package.json +1 -1
- package/src/GridMenus.svelte +17 -12
- package/src/SvGrid.controller.svelte.ts +155 -74
- package/src/SvGrid.css +1 -1
- package/src/SvGrid.svelte +110 -56
- package/src/SvGrid.types.ts +41 -1
- package/src/column-groups.ts +1 -1
- package/src/column-resize.test.ts +381 -0
- package/src/column-resize.ts +227 -0
- package/src/columns.test.ts +0 -103
- package/src/columns.ts +0 -58
- package/src/core.aggregate.test.ts +134 -0
- package/src/core.filter.test.ts +156 -0
- package/src/core.grouping.test.ts +146 -0
- package/src/core.row-shape.test.ts +119 -0
- package/src/core.rowmodel-cache.test.ts +121 -0
- package/src/core.sort.test.ts +293 -0
- package/src/core.ts +516 -119
- package/src/filtering/excel-filters.ts +30 -0
- package/src/filtering/normalize-fast-path.test.ts +104 -0
- package/src/group-display.ts +1 -1
- package/src/index.ts +12 -1
- package/src/menus.ts +1 -1
- package/src/resize-props.test.ts +361 -0
- package/src/row-resize.test.ts +31 -0
- package/src/row-resize.ts +21 -3
- package/src/selection.ts +9 -0
- package/src/spreadsheet.ts +1 -1
- package/dist/cdn/GridMenus-C3bJd7w8.js +0 -489
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Equivalence + work-budget tests for `createFilteredRowModel`.
|
|
3
|
+
*
|
|
4
|
+
* The filter path used to read a cell with
|
|
5
|
+
* `row.getAllCells().find((c) => c.column.id === id)?.getValue()`. That builds
|
|
6
|
+
* and caches the row's ENTIRE `Cell[]` just to read one field, which defeats
|
|
7
|
+
* the lazy-cell design the row factory goes out of its way to provide - a
|
|
8
|
+
* one-operator filter over 100k rows materialised 100,000 cell arrays
|
|
9
|
+
* (measured; `pnpm bench --case=filter-1op`).
|
|
10
|
+
*
|
|
11
|
+
* `getCellValueByColumnId` reads the same `cachedValues` array those cells read
|
|
12
|
+
* from, so the swap is a pure win - but "same array" is a claim about today's
|
|
13
|
+
* implementation, so the equivalence half of this file pins the observable
|
|
14
|
+
* behaviour rather than trusting it.
|
|
15
|
+
*/
|
|
16
|
+
import { describe, expect, it } from 'vitest'
|
|
17
|
+
import {
|
|
18
|
+
createCoreRowModel,
|
|
19
|
+
createFilteredRowModel,
|
|
20
|
+
createSvGridCore,
|
|
21
|
+
filterFns,
|
|
22
|
+
tableFeatures,
|
|
23
|
+
type ColumnDef,
|
|
24
|
+
type ColumnFiltersState,
|
|
25
|
+
} from './core'
|
|
26
|
+
|
|
27
|
+
type Row = Record<string, unknown>
|
|
28
|
+
|
|
29
|
+
const COLUMNS = [
|
|
30
|
+
{ field: 'text' },
|
|
31
|
+
{ field: 'num' },
|
|
32
|
+
{ field: 'flag' },
|
|
33
|
+
// A computed column: `fieldFn` rather than `field`, to prove the value path
|
|
34
|
+
// is the same one the cell objects use.
|
|
35
|
+
{ field: 'derived', fieldFn: (r: Row) => `${String(r.text).toUpperCase()}!` },
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
/** The original implementation, verbatim, as the oracle. */
|
|
39
|
+
function referenceFilter(rows: Row[], columns: typeof COLUMNS, filters: ColumnFiltersState): Row[] {
|
|
40
|
+
const value = (row: Row, id: string) => {
|
|
41
|
+
const col = columns.find((c) => c.field === id)
|
|
42
|
+
if (!col) return undefined
|
|
43
|
+
return col.fieldFn ? col.fieldFn(row) : row[col.field]
|
|
44
|
+
}
|
|
45
|
+
return rows.filter((row) =>
|
|
46
|
+
filters.every((filter) => {
|
|
47
|
+
const fn = filter.fn ? filterFns[filter.fn] : filterFns.includesString
|
|
48
|
+
return fn(value(row, filter.id), filter.value as never)
|
|
49
|
+
}),
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function actualFilter(rows: Row[], filters: ColumnFiltersState): Row[] {
|
|
54
|
+
const grid = createSvGridCore({
|
|
55
|
+
_features: tableFeatures({}),
|
|
56
|
+
_rowModels: {
|
|
57
|
+
coreRowModel: createCoreRowModel(),
|
|
58
|
+
filteredRowModel: createFilteredRowModel(),
|
|
59
|
+
},
|
|
60
|
+
columns: COLUMNS as unknown as Array<ColumnDef<ReturnType<typeof tableFeatures>, Row>>,
|
|
61
|
+
data: rows,
|
|
62
|
+
state: { columnFilters: filters },
|
|
63
|
+
})
|
|
64
|
+
return grid.getRowModel().rows.map((r) => r.original as Row)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const ROWS: Row[] = [
|
|
68
|
+
{ text: 'banana', num: 2, flag: true },
|
|
69
|
+
{ text: 'Apple', num: 10, flag: false },
|
|
70
|
+
{ text: 'apple', num: -3, flag: true },
|
|
71
|
+
{ text: null, num: null, flag: null },
|
|
72
|
+
{ text: undefined, num: undefined, flag: undefined },
|
|
73
|
+
{ text: '', num: 0, flag: '' },
|
|
74
|
+
{ text: 'APPLE pie', num: NaN, flag: 'yes' },
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
describe('createFilteredRowModel - equivalence with the original', () => {
|
|
78
|
+
const cases: Array<[string, ColumnFiltersState]> = [
|
|
79
|
+
['no filters', []],
|
|
80
|
+
['substring, case-insensitive', [{ id: 'text', value: 'app' }]],
|
|
81
|
+
['substring matching nothing', [{ id: 'text', value: 'zzz' }]],
|
|
82
|
+
['empty needle matches everything', [{ id: 'text', value: '' }]],
|
|
83
|
+
['equals operator', [{ id: 'num', value: 10, fn: 'equals' }]],
|
|
84
|
+
['equals against null', [{ id: 'num', value: null, fn: 'equals' }]],
|
|
85
|
+
['two filters ANDed', [{ id: 'text', value: 'a' }, { id: 'flag', value: 'true' }]],
|
|
86
|
+
['unknown column id', [{ id: 'nope', value: 'x' }]],
|
|
87
|
+
['computed column via fieldFn', [{ id: 'derived', value: 'APPLE' }]],
|
|
88
|
+
['filter on undefined values', [{ id: 'flag', value: 'undefined' }]],
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
for (const [name, filters] of cases) {
|
|
92
|
+
it(`matches: ${name}`, () => {
|
|
93
|
+
const actual = actualFilter(ROWS, filters)
|
|
94
|
+
const expected = referenceFilter(ROWS, COLUMNS, filters)
|
|
95
|
+
expect(actual.length).toBe(expected.length)
|
|
96
|
+
// Identity, so row order and object identity are both checked.
|
|
97
|
+
for (let i = 0; i < actual.length; i++) expect(actual[i]).toBe(expected[i])
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('createFilteredRowModel - work budget', () => {
|
|
103
|
+
/**
|
|
104
|
+
* Count how many rows materialise their full `Cell[]` during filtering.
|
|
105
|
+
*
|
|
106
|
+
* Wrapping each row in a proxy that tallies `getAllCells` is the same
|
|
107
|
+
* technique tools/bench uses, kept in-process here so the invariant is
|
|
108
|
+
* enforced by the unit suite rather than only by the bench.
|
|
109
|
+
*/
|
|
110
|
+
function countCellMaterialisations(rowCount: number, filters: ColumnFiltersState): number {
|
|
111
|
+
const rows: Row[] = Array.from({ length: rowCount }, (_, i) => ({
|
|
112
|
+
text: `row-${i % 7}`,
|
|
113
|
+
num: i,
|
|
114
|
+
flag: i % 2 === 0,
|
|
115
|
+
}))
|
|
116
|
+
let calls = 0
|
|
117
|
+
const grid = createSvGridCore({
|
|
118
|
+
_features: tableFeatures({}),
|
|
119
|
+
_rowModels: {
|
|
120
|
+
coreRowModel: createCoreRowModel(),
|
|
121
|
+
filteredRowModel: (args) =>
|
|
122
|
+
createFilteredRowModel<Row>()({
|
|
123
|
+
...args,
|
|
124
|
+
rows: args.rows.map(
|
|
125
|
+
(row) =>
|
|
126
|
+
new Proxy(row, {
|
|
127
|
+
get(obj, prop, recv) {
|
|
128
|
+
if (prop === 'getAllCells') calls++
|
|
129
|
+
return Reflect.get(obj, prop, recv)
|
|
130
|
+
},
|
|
131
|
+
}) as typeof row,
|
|
132
|
+
),
|
|
133
|
+
}),
|
|
134
|
+
},
|
|
135
|
+
columns: COLUMNS as unknown as Array<ColumnDef<ReturnType<typeof tableFeatures>, Row>>,
|
|
136
|
+
data: rows,
|
|
137
|
+
state: { columnFilters: filters },
|
|
138
|
+
})
|
|
139
|
+
grid.getRowModel()
|
|
140
|
+
return calls
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
it('never materialises a row\'s cell array to read one field', () => {
|
|
144
|
+
expect(countCellMaterialisations(2_000, [{ id: 'text', value: 'row-1' }])).toBe(0)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('stays at zero with several filters', () => {
|
|
148
|
+
expect(
|
|
149
|
+
countCellMaterialisations(2_000, [
|
|
150
|
+
{ id: 'text', value: 'row' },
|
|
151
|
+
{ id: 'num', value: '1' },
|
|
152
|
+
{ id: 'flag', value: 'true' },
|
|
153
|
+
]),
|
|
154
|
+
).toBe(0)
|
|
155
|
+
})
|
|
156
|
+
})
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grouping: the ancestor short-circuit in `resolveColumnValue`.
|
|
3
|
+
*
|
|
4
|
+
* A group row shows, for each non-aggregated column, the value its children
|
|
5
|
+
* agree on - or `undefined` when they disagree. That used to be found by
|
|
6
|
+
* scanning every child, even for a column an ANCESTOR level had already grouped
|
|
7
|
+
* by, where the answer is fixed by construction. On a 100k x 9 two-level group
|
|
8
|
+
* that was ~100,000 reads for a value already in hand.
|
|
9
|
+
*
|
|
10
|
+
* The shortcut has one genuinely sharp edge, which is what most of this file is
|
|
11
|
+
* about: buckets are keyed by `String(value ?? '')`, so `null`, `undefined` and
|
|
12
|
+
* `''` all land in the SAME bucket. A scan of that bucket reports disagreement,
|
|
13
|
+
* so the shortcut has to report disagreement too rather than picking whichever
|
|
14
|
+
* raw value happened to arrive first.
|
|
15
|
+
*/
|
|
16
|
+
import { describe, expect, it } from 'vitest'
|
|
17
|
+
import {
|
|
18
|
+
createCoreRowModel,
|
|
19
|
+
createGroupedRowModel,
|
|
20
|
+
createSvGridCore,
|
|
21
|
+
columnGroupingFeature,
|
|
22
|
+
tableFeatures,
|
|
23
|
+
type ColumnDef,
|
|
24
|
+
type Row,
|
|
25
|
+
} from './core'
|
|
26
|
+
|
|
27
|
+
type Data = Record<string, unknown>
|
|
28
|
+
|
|
29
|
+
function grouped(data: Data[], fields: string[], grouping: string[]) {
|
|
30
|
+
const grid = createSvGridCore({
|
|
31
|
+
_features: tableFeatures({ columnGroupingFeature }),
|
|
32
|
+
_rowModels: { coreRowModel: createCoreRowModel(), groupedRowModel: createGroupedRowModel() },
|
|
33
|
+
columns: fields.map((f) => ({ field: f })) as unknown as Array<
|
|
34
|
+
ColumnDef<ReturnType<typeof tableFeatures>, Data>
|
|
35
|
+
>,
|
|
36
|
+
data,
|
|
37
|
+
state: { grouping },
|
|
38
|
+
onGroupingChange: () => {},
|
|
39
|
+
})
|
|
40
|
+
return grid.getRowModel().rows
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Walk to the group rows at a given depth. */
|
|
44
|
+
function atDepth(rows: Array<Row<Data>>, depth: number): Array<Row<Data>> {
|
|
45
|
+
const out: Array<Row<Data>> = []
|
|
46
|
+
const visit = (rs: Array<Row<Data>>) => {
|
|
47
|
+
for (const r of rs) {
|
|
48
|
+
if (r.depth === depth) out.push(r)
|
|
49
|
+
if (r.subRows?.length) visit(r.subRows as Array<Row<Data>>)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
visit(rows)
|
|
53
|
+
return out
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe('createGroupedRowModel - ancestor value resolution', () => {
|
|
57
|
+
const FIELDS = ['region', 'status', 'owner', 'amount']
|
|
58
|
+
|
|
59
|
+
it('a second-level group still reports the first level value', () => {
|
|
60
|
+
const data: Data[] = [
|
|
61
|
+
{ region: 'EMEA', status: 'open', owner: 'ada', amount: 1 },
|
|
62
|
+
{ region: 'EMEA', status: 'open', owner: 'ada', amount: 2 },
|
|
63
|
+
{ region: 'EMEA', status: 'shut', owner: 'bob', amount: 3 },
|
|
64
|
+
{ region: 'APAC', status: 'open', owner: 'cy', amount: 4 },
|
|
65
|
+
]
|
|
66
|
+
const inner = atDepth(grouped(data, FIELDS, ['region', 'status']), 1)
|
|
67
|
+
expect(inner.length).toBe(3)
|
|
68
|
+
for (const g of inner) {
|
|
69
|
+
// `region` is fixed by the ancestor bucket, so every inner group must
|
|
70
|
+
// report it rather than undefined.
|
|
71
|
+
expect(['EMEA', 'APAC']).toContain(g.getCellValueByColumnId('region'))
|
|
72
|
+
}
|
|
73
|
+
// And the columns that genuinely vary still resolve the old way.
|
|
74
|
+
const emeaOpen = inner.find((g) => g.getCellValueByColumnId('status') === 'open')!
|
|
75
|
+
expect(emeaOpen.getCellValueByColumnId('owner')).toBe('ada')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('reports disagreement when a bucket mixed null, undefined and empty string', () => {
|
|
79
|
+
// All three key to '' and share one bucket. A scan would find they disagree,
|
|
80
|
+
// so the shortcut must not report whichever arrived first.
|
|
81
|
+
const data: Data[] = [
|
|
82
|
+
{ region: null, status: 'x', owner: 'a', amount: 1 },
|
|
83
|
+
{ region: undefined, status: 'y', owner: 'b', amount: 2 },
|
|
84
|
+
{ region: '', status: 'z', owner: 'c', amount: 3 },
|
|
85
|
+
]
|
|
86
|
+
const inner = atDepth(grouped(data, FIELDS, ['region', 'status']), 1)
|
|
87
|
+
expect(inner.length).toBe(3)
|
|
88
|
+
for (const g of inner) {
|
|
89
|
+
// Each inner bucket holds exactly one row, so `region` is whatever that
|
|
90
|
+
// row had - not a value borrowed from a sibling.
|
|
91
|
+
const status = g.getCellValueByColumnId('status')
|
|
92
|
+
const expected = data.find((d) => d.status === status)!.region
|
|
93
|
+
expect(g.getCellValueByColumnId('region')).toBe(expected)
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('a mixed ancestor bucket resolves to undefined at the deeper level', () => {
|
|
98
|
+
// One inner bucket spanning rows whose raw region values differ but key the
|
|
99
|
+
// same. `status` is identical so they stay together at level two.
|
|
100
|
+
const data: Data[] = [
|
|
101
|
+
{ region: null, status: 'same', owner: 'a', amount: 1 },
|
|
102
|
+
{ region: '', status: 'same', owner: 'a', amount: 2 },
|
|
103
|
+
]
|
|
104
|
+
const inner = atDepth(grouped(data, FIELDS, ['region', 'status']), 1)
|
|
105
|
+
expect(inner.length).toBe(1)
|
|
106
|
+
// A scan of [null, ''] disagrees, so this must be undefined.
|
|
107
|
+
expect(inner[0]!.getCellValueByColumnId('region')).toBeUndefined()
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('preserves the raw type of an ancestor value rather than its bucket key', () => {
|
|
111
|
+
// Buckets are keyed by String(value), so a numeric grouping column would
|
|
112
|
+
// report '2024' instead of 2024 if the shortcut returned the key.
|
|
113
|
+
const data: Data[] = [
|
|
114
|
+
{ region: 2024, status: 'a', owner: 'x', amount: 1 },
|
|
115
|
+
{ region: 2024, status: 'b', owner: 'y', amount: 2 },
|
|
116
|
+
]
|
|
117
|
+
const inner = atDepth(grouped(data, FIELDS, ['region', 'status']), 1)
|
|
118
|
+
expect(inner.length).toBe(2)
|
|
119
|
+
for (const g of inner) {
|
|
120
|
+
expect(g.getCellValueByColumnId('region')).toBe(2024)
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('handles three levels', () => {
|
|
125
|
+
const data: Data[] = [
|
|
126
|
+
{ region: 'EMEA', status: 'open', owner: 'ada', amount: 1 },
|
|
127
|
+
{ region: 'EMEA', status: 'open', owner: 'ada', amount: 2 },
|
|
128
|
+
{ region: 'EMEA', status: 'open', owner: 'bob', amount: 3 },
|
|
129
|
+
]
|
|
130
|
+
const deepest = atDepth(grouped(data, FIELDS, ['region', 'status', 'owner']), 2)
|
|
131
|
+
expect(deepest.length).toBe(2)
|
|
132
|
+
for (const g of deepest) {
|
|
133
|
+
expect(g.getCellValueByColumnId('region')).toBe('EMEA')
|
|
134
|
+
expect(g.getCellValueByColumnId('status')).toBe('open')
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('still returns undefined for a non-grouping column whose children disagree', () => {
|
|
139
|
+
const data: Data[] = [
|
|
140
|
+
{ region: 'EMEA', status: 'open', owner: 'ada', amount: 1 },
|
|
141
|
+
{ region: 'EMEA', status: 'open', owner: 'bob', amount: 2 },
|
|
142
|
+
]
|
|
143
|
+
const inner = atDepth(grouped(data, FIELDS, ['region', 'status']), 1)
|
|
144
|
+
expect(inner[0]!.getCellValueByColumnId('owner')).toBeUndefined()
|
|
145
|
+
})
|
|
146
|
+
})
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public shape of a `Row`.
|
|
3
|
+
*
|
|
4
|
+
* Rows were rewritten from object literals with per-row closures to objects
|
|
5
|
+
* carrying shared `this`-based methods, which cut mount allocation from 56.5 MB
|
|
6
|
+
* to 14.5 MB. Those shared methods need a pointer back to the table, and the
|
|
7
|
+
* first version stored it as a plain `_ctx` property - which made every row
|
|
8
|
+
* serialise the whole grid, because `options.data` is reachable through it.
|
|
9
|
+
* `JSON.stringify(oneRow)` grew with the dataset: 981 characters at 3 rows,
|
|
10
|
+
* 67,719 at 3,000. Stringifying a row model was quadratic.
|
|
11
|
+
*
|
|
12
|
+
* The fix is to key the internals with symbols: invisible to `JSON.stringify`,
|
|
13
|
+
* `Object.keys` and `for...in`, but still copied by object spread, which
|
|
14
|
+
* several row models rely on when they do `{ ...row, depth }`.
|
|
15
|
+
*
|
|
16
|
+
* These tests pin all four of those properties. `Row` is public API and its
|
|
17
|
+
* observable shape is part of the contract.
|
|
18
|
+
*/
|
|
19
|
+
import { describe, expect, it } from 'vitest'
|
|
20
|
+
import {
|
|
21
|
+
createCoreRowModel,
|
|
22
|
+
createGroupedRowModel,
|
|
23
|
+
createSvGridCore,
|
|
24
|
+
columnGroupingFeature,
|
|
25
|
+
tableFeatures,
|
|
26
|
+
type ColumnDef,
|
|
27
|
+
} from './core'
|
|
28
|
+
|
|
29
|
+
type Data = Record<string, unknown>
|
|
30
|
+
|
|
31
|
+
function makeGrid(count: number, grouping: string[] = []) {
|
|
32
|
+
const data: Data[] = Array.from({ length: count }, (_, i) => ({
|
|
33
|
+
name: `row-${i}`,
|
|
34
|
+
score: i,
|
|
35
|
+
bucket: i % 2 === 0 ? 'even' : 'odd',
|
|
36
|
+
}))
|
|
37
|
+
return createSvGridCore({
|
|
38
|
+
_features: tableFeatures({ columnGroupingFeature }),
|
|
39
|
+
_rowModels: grouping.length
|
|
40
|
+
? { coreRowModel: createCoreRowModel(), groupedRowModel: createGroupedRowModel() }
|
|
41
|
+
: { coreRowModel: createCoreRowModel() },
|
|
42
|
+
columns: [{ field: 'name' }, { field: 'score' }, { field: 'bucket' }] as unknown as Array<
|
|
43
|
+
ColumnDef<ReturnType<typeof tableFeatures>, Data>
|
|
44
|
+
>,
|
|
45
|
+
data,
|
|
46
|
+
state: { grouping },
|
|
47
|
+
onGroupingChange: () => {},
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const PUBLIC_KEYS = [
|
|
52
|
+
'id',
|
|
53
|
+
'index',
|
|
54
|
+
'original',
|
|
55
|
+
'depth',
|
|
56
|
+
'getCanExpand',
|
|
57
|
+
'getIsExpanded',
|
|
58
|
+
'toggleExpanded',
|
|
59
|
+
'getIsSelected',
|
|
60
|
+
'toggleSelected',
|
|
61
|
+
'getAllCells',
|
|
62
|
+
'getCellValueByColumnId',
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
describe('Row public shape', () => {
|
|
66
|
+
it('exposes exactly the documented keys, with no internals', () => {
|
|
67
|
+
const row = makeGrid(10).getRowModel().rows[0]!
|
|
68
|
+
expect(Object.keys(row).sort()).toEqual([...PUBLIC_KEYS].sort())
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('does not leak internals through for...in', () => {
|
|
72
|
+
const row = makeGrid(10).getRowModel().rows[0]!
|
|
73
|
+
const seen: string[] = []
|
|
74
|
+
for (const k in row) seen.push(k)
|
|
75
|
+
expect(seen.sort()).toEqual([...PUBLIC_KEYS].sort())
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('serialises to a constant size regardless of dataset size', () => {
|
|
79
|
+
// The regression this guards: `_ctx` reached `options.data`, so a single
|
|
80
|
+
// row carried the entire grid into JSON. Sizes must not grow with count.
|
|
81
|
+
const sizes = [10, 100, 1000].map(
|
|
82
|
+
(n) => JSON.stringify(makeGrid(n).getRowModel().rows[0]).length,
|
|
83
|
+
)
|
|
84
|
+
expect(new Set(sizes).size).toBe(1)
|
|
85
|
+
// And it must stay small - this is 4 scalar fields, not a grid.
|
|
86
|
+
expect(sizes[0]!).toBeLessThan(200)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('serialises a whole row model linearly, not quadratically', () => {
|
|
90
|
+
const small = JSON.stringify(makeGrid(50).getRowModel().rows).length
|
|
91
|
+
const large = JSON.stringify(makeGrid(500).getRowModel().rows).length
|
|
92
|
+
// Ten times the rows should cost about ten times the characters. Quadratic
|
|
93
|
+
// growth would put this well past 20x.
|
|
94
|
+
expect(large).toBeLessThan(small * 20)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('survives an object spread with its methods intact', () => {
|
|
98
|
+
// `createGroupedRowModel` does `{ ...row, depth }` for leaf rows; the clone
|
|
99
|
+
// has to keep working, which is why the internals are symbol-keyed rather
|
|
100
|
+
// than non-enumerable.
|
|
101
|
+
const row = makeGrid(10).getRowModel().rows[0]!
|
|
102
|
+
const clone = { ...row, depth: 3 }
|
|
103
|
+
expect(clone.depth).toBe(3)
|
|
104
|
+
expect(typeof clone.getCellValueByColumnId).toBe('function')
|
|
105
|
+
expect(clone.getCellValueByColumnId('name')).toBe(row.getCellValueByColumnId('name'))
|
|
106
|
+
expect(clone.getIsSelected()).toBe(false)
|
|
107
|
+
// The clone must not have grown a public surface either.
|
|
108
|
+
expect(Object.keys(clone).sort()).toEqual([...PUBLIC_KEYS].sort())
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('keeps grouped leaf rows working after the model clones them', () => {
|
|
112
|
+
const rows = makeGrid(10, ['bucket']).getRowModel().rows
|
|
113
|
+
const group = rows[0]!
|
|
114
|
+
expect(group.subRows?.length).toBeGreaterThan(0)
|
|
115
|
+
const leaf = group.subRows![0]!
|
|
116
|
+
expect(leaf.getCellValueByColumnId('name')).toMatch(/^row-\d+$/)
|
|
117
|
+
expect(leaf.depth).toBe(1)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row-model memo invalidation.
|
|
3
|
+
*
|
|
4
|
+
* `getRowModel()` caches on the identity of the data, the columns, and a set of
|
|
5
|
+
* state slices. `rowSelection` was one of those slices, so ticking a single
|
|
6
|
+
* checkbox on a 100k-row grid re-ran the whole pipeline - re-filtering and
|
|
7
|
+
* re-sorting the entire dataset - to produce a row array that was, by
|
|
8
|
+
* construction, identical. Measured at 5 full re-runs for 5 toggles
|
|
9
|
+
* (`pnpm bench --case=selection-toggle`).
|
|
10
|
+
*
|
|
11
|
+
* Nothing in the pipeline reads selection. The two consumers, `getIsSelected`
|
|
12
|
+
* on data rows (core.ts) and on group rows, are closures that read
|
|
13
|
+
* `store.state` when called, so they see a selection change without the model
|
|
14
|
+
* being rebuilt. That is what the last test here pins: dropping the slice must
|
|
15
|
+
* not make a selected row report the wrong thing.
|
|
16
|
+
*/
|
|
17
|
+
import { describe, expect, it } from 'vitest'
|
|
18
|
+
import {
|
|
19
|
+
createCoreRowModel,
|
|
20
|
+
createFilteredRowModel,
|
|
21
|
+
createSortedRowModel,
|
|
22
|
+
createSvGridCore,
|
|
23
|
+
rowSelectionFeature,
|
|
24
|
+
tableFeatures,
|
|
25
|
+
type ColumnDef,
|
|
26
|
+
type RowModelFactory,
|
|
27
|
+
} from './core'
|
|
28
|
+
|
|
29
|
+
type Row = { id: number; name: string; score: number }
|
|
30
|
+
|
|
31
|
+
const COLUMNS = [
|
|
32
|
+
{ field: 'id', editorType: 'number' },
|
|
33
|
+
{ field: 'name' },
|
|
34
|
+
{ field: 'score', editorType: 'number' },
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
function makeRows(n: number): Row[] {
|
|
38
|
+
return Array.from({ length: n }, (_, i) => ({
|
|
39
|
+
id: i,
|
|
40
|
+
name: `name-${(i * 7919) % n}`,
|
|
41
|
+
score: (i * 31) % 1000,
|
|
42
|
+
}))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Wrap a stage so we can count how many times the pipeline actually ran it. */
|
|
46
|
+
function counted<T extends Row>(factory: RowModelFactory<T>, tally: { n: number }): RowModelFactory<T> {
|
|
47
|
+
return (args) => {
|
|
48
|
+
tally.n++
|
|
49
|
+
return factory(args)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function makeGrid(rows: Row[], tallies: { filter: { n: number }; sort: { n: number } }) {
|
|
54
|
+
return createSvGridCore({
|
|
55
|
+
_features: tableFeatures({ rowSelectionFeature }),
|
|
56
|
+
_rowModels: {
|
|
57
|
+
coreRowModel: createCoreRowModel(),
|
|
58
|
+
filteredRowModel: counted(createFilteredRowModel<Row>(), tallies.filter),
|
|
59
|
+
sortedRowModel: counted(createSortedRowModel<Row>(), tallies.sort),
|
|
60
|
+
},
|
|
61
|
+
columns: COLUMNS as unknown as Array<ColumnDef<ReturnType<typeof tableFeatures>, Row>>,
|
|
62
|
+
data: rows,
|
|
63
|
+
state: {
|
|
64
|
+
columnFilters: [{ id: 'name', value: 'name-' }],
|
|
65
|
+
sorting: [{ id: 'score', desc: false }],
|
|
66
|
+
rowSelection: {},
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe('row-model cache: selection', () => {
|
|
72
|
+
it('does not re-run the pipeline when only the selection changes', () => {
|
|
73
|
+
const tallies = { filter: { n: 0 }, sort: { n: 0 } }
|
|
74
|
+
const grid = makeGrid(makeRows(500), tallies)
|
|
75
|
+
|
|
76
|
+
grid.getRowModel() // first build - expected to run
|
|
77
|
+
const after = { filter: tallies.filter.n, sort: tallies.sort.n }
|
|
78
|
+
expect(after.filter).toBe(1)
|
|
79
|
+
expect(after.sort).toBe(1)
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < 5; i++) {
|
|
82
|
+
grid.setRowSelection((prev) => ({ ...prev, [String(i)]: true }))
|
|
83
|
+
grid.getRowModel()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
expect(tallies.filter.n - after.filter).toBe(0)
|
|
87
|
+
expect(tallies.sort.n - after.sort).toBe(0)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('still re-runs when a slice the pipeline DOES read changes', () => {
|
|
91
|
+
const tallies = { filter: { n: 0 }, sort: { n: 0 } }
|
|
92
|
+
const grid = makeGrid(makeRows(200), tallies)
|
|
93
|
+
grid.getRowModel()
|
|
94
|
+
const before = tallies.sort.n
|
|
95
|
+
|
|
96
|
+
// No `setSorting` on the core - sorting is driven through the store (or a
|
|
97
|
+
// column's `toggleSorting`), which is also how <SvGrid> does it.
|
|
98
|
+
grid.store.setState((prev) => ({ ...prev, sorting: [{ id: 'score', desc: true }] }))
|
|
99
|
+
grid.getRowModel()
|
|
100
|
+
|
|
101
|
+
expect(tallies.sort.n).toBeGreaterThan(before)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('reports selection correctly even though the model was not rebuilt', () => {
|
|
105
|
+
const tallies = { filter: { n: 0 }, sort: { n: 0 } }
|
|
106
|
+
const grid = makeGrid(makeRows(50), tallies)
|
|
107
|
+
|
|
108
|
+
const rows = grid.getRowModel().rows
|
|
109
|
+
const target = rows[3]!
|
|
110
|
+
expect(target.getIsSelected()).toBe(false)
|
|
111
|
+
|
|
112
|
+
grid.setRowSelection((prev) => ({ ...prev, [target.id]: true }))
|
|
113
|
+
|
|
114
|
+
// Same row object, no rebuild - the closure must observe the new state.
|
|
115
|
+
expect(grid.getRowModel().rows[3]).toBe(target)
|
|
116
|
+
expect(target.getIsSelected()).toBe(true)
|
|
117
|
+
|
|
118
|
+
grid.setRowSelection(() => ({}))
|
|
119
|
+
expect(target.getIsSelected()).toBe(false)
|
|
120
|
+
})
|
|
121
|
+
})
|