Skip to content

Commit 117f723

Browse files
author
Daminski
committed
Clean up code; remove useless condition checks
1 parent fddd33f commit 117f723

File tree

3 files changed

+47
-54
lines changed

3 files changed

+47
-54
lines changed

src/lib/VirtualList.svelte

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828
children
2929
} = $props();
3030
31-
let wrapper = $state();
32-
let items = $state.raw([]);
31+
let wrapper = $state.raw();
32+
33+
let styleCache = $state({});
34+
let wrapperStyle = $state.raw('');
35+
let innerStyle = $state.raw('');
3336
34-
const _state = new ListState(scrollOffset || (scrollToIndex != null && items.length && getOffsetForIndex(scrollToIndex)) || 0);
37+
let items = $state.raw([]);
3538
3639
const _props = new ListProps(
3740
scrollToIndex,
@@ -45,9 +48,7 @@
4548
stickyIndices
4649
);
4750
48-
let styleCache = $state({});
49-
let wrapperStyle = $state('');
50-
let innerStyle = $state('');
51+
const _state = new ListState(scrollOffset || 0);
5152
5253
const sizeAndPositionManager = new SizeAndPositionManager({
5354
itemCount,
@@ -63,52 +64,44 @@
6364
};
6465
});
6566
66-
// Effect 1: Updates props from user provided vars
67+
// Effect 1: Update props from user provided props
6768
$effect(() => {
6869
_props.listen(scrollToIndex, scrollToAlignment, scrollOffset, itemCount, itemSize, estimatedItemSize, height, width, stickyIndices);
6970
7071
untrack(() => {
72+
let doRecomputeSizes = false;
73+
7174
if (_props.haveSizesChanged) {
7275
sizeAndPositionManager.updateConfig({
7376
itemSize,
7477
itemCount,
7578
estimatedItemSize: _props.estimatedItemSize
7679
});
77-
recomputeSizes();
80+
doRecomputeSizes = true;
7881
}
79-
8082
81-
if (_props.hasScrollOffsetChanged) {
83+
if (_props.hasScrollOffsetChanged)
8284
_state.listen(_props.scrollOffset, SCROLL_CHANGE_REASON.REQUESTED);
83-
} else if (_props.hasScrollIndexChanged) {
84-
_state.listen(getOffsetForIndex(scrollToIndex, scrollToAlignment, itemCount), SCROLL_CHANGE_REASON.REQUESTED);
85-
}
85+
else if (_props.hasScrollIndexChanged)
86+
_state.listen(getOffsetForIndex(scrollToIndex, scrollToAlignment), SCROLL_CHANGE_REASON.REQUESTED);
8687
87-
if (_props.haveDimsOrStickyIndicesChanged) {
88-
recomputeSizes(0);
89-
}
88+
if (_props.haveDimsOrStickyIndicesChanged || doRecomputeSizes)
89+
recomputeSizes();
9090
9191
_props.update();
9292
});
9393
});
9494
95-
// Effect 2: Update state offset from props scrollOffset
96-
$effect(() => {
97-
_state.listen(_props.scrollOffset);
98-
})
99-
100-
// Effect 3: Update UI from state
95+
// Effect 2: Update UI from state
10196
$effect(() => {
10297
_state.offset;
10398
10499
untrack(() => {
105-
if (_state.doRefresh) {
100+
if (_state.doRefresh)
106101
refresh();
107-
}
108102
109-
if (_state.doScrollToOffset) {
103+
if (_state.doScrollToOffset)
110104
scrollTo(_state.offset);
111-
}
112105
113106
_state.update();
114107
});
@@ -121,11 +114,12 @@
121114
overscanCount
122115
});
123116
124-
const updatedItems = [];
117+
const visibleItems = [];
125118
126119
const totalSize = sizeAndPositionManager.getTotalSize();
127120
const heightUnit = typeof height === 'number' ? 'px' : '';
128121
const widthUnit = typeof width === 'number' ? 'px' : '';
122+
129123
if (scrollDirection === DIRECTION.VERTICAL) {
130124
wrapperStyle = `height:${height}${heightUnit};width:${width}${widthUnit};`;
131125
innerStyle = `flex-direction:column;height:${totalSize}px;`;
@@ -134,11 +128,11 @@
134128
innerStyle = `min-height:100%;width:${totalSize}px;`;
135129
}
136130
137-
const hasStickyIndices = stickyIndices != null && stickyIndices.length !== 0;
131+
const hasStickyIndices = Array.isArray(stickyIndices) && stickyIndices.length > 0;
138132
if (hasStickyIndices) {
139133
for (let i = 0; i < stickyIndices.length; i++) {
140134
const index = stickyIndices[i];
141-
updatedItems.push({
135+
visibleItems.push({
142136
index,
143137
style: getStyle(index, true)
144138
});
@@ -147,11 +141,10 @@
147141
148142
if (start !== undefined && stop !== undefined) {
149143
for (let index = start; index <= stop; index++) {
150-
if (hasStickyIndices && stickyIndices.includes(index)) {
144+
if (hasStickyIndices && stickyIndices.includes(index))
151145
continue;
152-
}
153146
154-
updatedItems.push({
147+
visibleItems.push({
155148
index,
156149
style: getStyle(index, false)
157150
});
@@ -160,30 +153,29 @@
160153
onListItemsUpdate({ start, end: stop });
161154
}
162155
163-
items = updatedItems;
156+
items = visibleItems;
164157
};
165158
166159
const scrollTo = (value) => {
167-
if ('scroll' in wrapper) {
160+
if ('scroll' in wrapper)
168161
wrapper.scroll({
169162
[SCROLL_PROP[scrollDirection]]: value,
170163
behavior: scrollToBehaviour
171164
});
172-
} else {
165+
else
173166
wrapper[SCROLL_PROP_LEGACY[scrollDirection]] = value;
174-
}
175167
};
176168
177-
export const recomputeSizes = (startIndex = 0) => {
169+
export const recomputeSizes = (startIndex = scrollToIndex) => {
178170
styleCache = {};
179-
sizeAndPositionManager.resetItem(startIndex);
171+
if (startIndex >= 0)
172+
sizeAndPositionManager.resetItem(startIndex);
180173
refresh();
181174
};
182175
183-
const getOffsetForIndex = (index, align = scrollToAlignment, _itemCount = itemCount) => {
184-
if (index < 0 || index >= _itemCount) {
176+
const getOffsetForIndex = (index, align = scrollToAlignment) => {
177+
if (index < 0 || index >= itemCount)
185178
index = 0;
186-
}
187179
188180
return sizeAndPositionManager.getUpdatedOffsetForIndex({
189181
align,
@@ -219,22 +211,22 @@
219211
if (scrollDirection === DIRECTION.VERTICAL) {
220212
style = `left:0;width:100%;height:${size}px;`;
221213
222-
if (sticky) {
214+
if (sticky)
223215
style += `position:sticky;flex-grow:0;z-index:1;top:0;margin-top:${offset}px;margin-bottom:${-(offset + size)}px;`;
224-
} else {
216+
else
225217
style += `position:absolute;top:${offset}px;`;
226-
}
227218
} else {
228219
style = `top:0;width:${size}px;`;
229220
230-
if (sticky) {
221+
if (sticky)
231222
style += `position:sticky;z-index:1;left:0;margin-left:${offset}px;margin-right:${-(offset + size)}px;`;
232-
} else {
223+
else
233224
style += `position:absolute;height:100%;left:${offset}px;`;
234-
}
235225
}
236226
237-
return (styleCache[index] = style);
227+
styleCache[index] = style;
228+
229+
return styleCache[index];
238230
};
239231
</script>
240232

src/lib/utils/listState.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class ListState {
2424
};
2525

2626
listen (offset, scrollChangeReason) {
27-
if (typeof offset === "number")
27+
if (typeof offset === "number" && offset >= 0)
2828
this.offset = offset;
2929

3030
if (typeof scrollChangeReason === "string")

src/routes/examples/scroll-to-index/+page.svelte

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
import VirtualList from '$lib/VirtualList.svelte';
33
44
let virtualList;
5-
let rowHeights = $state([]);
5+
let rowHeights = $state.raw([]);
66
77
let scrollToIndex = $state();
88
let scrollToAlignment = $state('start');
99
let scrollToBehaviour = $state('instant');
1010
11-
randomize();
11+
const NUM_ROWS = 10000;
1212
1313
function randomize() {
14-
let newRowHeights = [];
14+
const newRowHeights = [];
1515
16-
for (let i = 0; i < 10000; i++) {
16+
for (let i = 0; i < NUM_ROWS; i++)
1717
newRowHeights.push(Math.random() * (155 - 50) + 50);
18-
}
1918
2019
rowHeights = newRowHeights;
2120
};
21+
22+
randomize();
2223
</script>
2324

2425
<svelte:head>

0 commit comments

Comments
 (0)