Skip to content

Commit ee642bc

Browse files
committed
Batch updates and reduce array creation
1 parent 7121ef0 commit ee642bc

14 files changed

Lines changed: 117 additions & 121 deletions

e2e/VList.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test.describe("smoke", () => {
7979
// check if last is displayed
8080
const last = component.getByText("999", { exact: true });
8181
await expect(last).toBeVisible();
82-
expect(await relativeBottom(component, last)).toEqual(0);
82+
expectInRange(await relativeBottom(component, last), { min: 0, max: 0.5 });
8383
});
8484

8585
test("display: none", async ({ page }) => {

e2e/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const expectInRange = (
3636
) => {
3737
// sometimes it may not be 0 because of sub pixel value
3838
expect(value).toBeGreaterThanOrEqual(min);
39-
expect(value).toBeLessThan(max);
39+
expect(value).toBeLessThanOrEqual(max);
4040
};
4141

4242
export const approxymate = (v: number): number => Math.round(v / 100) * 100;

src/core/resizer.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export const createResizer = (
5252
const mountedIndexes = new WeakMap<Element, number>();
5353

5454
const resizeObserver = createResizeObserver((entries) => {
55-
const resizes: ItemResize[] = [];
5655
for (const { target, contentRect } of entries) {
5756
// Skip zero-sized rects that may be observed under `display: none` style
5857
if (!(target as HTMLElement).offsetParent) continue;
@@ -62,14 +61,10 @@ export const createResizer = (
6261
} else {
6362
const index = mountedIndexes.get(target);
6463
if (index != NULL) {
65-
resizes.push([index, contentRect[sizeKey]]);
64+
store.$update(ACTION_ITEM_RESIZE, [index, contentRect[sizeKey]]);
6665
}
6766
}
6867
}
69-
70-
if (resizes.length) {
71-
store.$update(ACTION_ITEM_RESIZE, resizes);
72-
}
7368
});
7469

7570
return {
@@ -106,20 +101,15 @@ export const createWindowResizer = (
106101
const mountedIndexes = new WeakMap<Element, number>();
107102

108103
const resizeObserver = createResizeObserver((entries) => {
109-
const resizes: ItemResize[] = [];
110104
for (const { target, contentRect } of entries) {
111105
// Skip zero-sized rects that may be observed under `display: none` style
112106
if (!(target as HTMLElement).offsetParent) continue;
113107

114108
const index = mountedIndexes.get(target);
115109
if (index != NULL) {
116-
resizes.push([index, contentRect[sizeKey]]);
110+
store.$update(ACTION_ITEM_RESIZE, [index, contentRect[sizeKey]]);
117111
}
118112
}
119-
120-
if (resizes.length) {
121-
store.$update(ACTION_ITEM_RESIZE, resizes);
122-
}
123113
});
124114

125115
let cleanupOnWindowResize: (() => void) | undefined;
@@ -221,7 +211,6 @@ export const createGridResizer = (
221211
}
222212

223213
if (resizedRows.size) {
224-
const heightResizes: ItemResize[] = [];
225214
resizedRows.forEach((rowIndex) => {
226215
let maxHeight = 0;
227216
maybeCachedColIndexes.forEach((colIndex) => {
@@ -231,13 +220,11 @@ export const createGridResizer = (
231220
}
232221
});
233222
if (maxHeight) {
234-
heightResizes.push([rowIndex, maxHeight]);
223+
vStore.$update(ACTION_ITEM_RESIZE, [rowIndex, maxHeight]);
235224
}
236225
});
237-
vStore.$update(ACTION_ITEM_RESIZE, heightResizes);
238226
}
239227
if (resizedCols.size) {
240-
const widthResizes: ItemResize[] = [];
241228
resizedCols.forEach((colIndex) => {
242229
let maxWidth = 0;
243230
maybeCachedRowIndexes.forEach((rowIndex) => {
@@ -247,10 +234,9 @@ export const createGridResizer = (
247234
}
248235
});
249236
if (maxWidth) {
250-
widthResizes.push([colIndex, maxWidth]);
237+
hStore.$update(ACTION_ITEM_RESIZE, [colIndex, maxWidth]);
251238
}
252239
});
253-
hStore.$update(ACTION_ITEM_RESIZE, widthResizes);
254240
}
255241
});
256242

@@ -269,20 +255,20 @@ export const createGridResizer = (
269255
};
270256
},
271257
$resizeCols(cols: ItemResize[]) {
272-
for (const [c] of cols) {
258+
for (const col of cols) {
273259
for (let r = 0; r < vStore.$getItemsLength(); r++) {
274-
sizeCache.delete(getKey(r, c));
260+
sizeCache.delete(getKey(r, col[0]));
275261
}
262+
hStore.$update(ACTION_ITEM_RESIZE, col);
276263
}
277-
hStore.$update(ACTION_ITEM_RESIZE, cols);
278264
},
279265
$resizeRows(rows: ItemResize[]) {
280-
for (const [r] of rows) {
266+
for (const row of rows) {
281267
for (let c = 0; c < hStore.$getItemsLength(); c++) {
282-
sizeCache.delete(getKey(r, c));
268+
sizeCache.delete(getKey(row[0], c));
283269
}
270+
vStore.$update(ACTION_ITEM_RESIZE, row);
284271
}
285-
vStore.$update(ACTION_ITEM_RESIZE, rows);
286272
},
287273
$dispose: resizeObserver._dispose,
288274
};

0 commit comments

Comments
 (0)