Skip to content

Commit 646c7af

Browse files
committed
Frontend: Fix race condition
It resulted from yesterday's refactor c2681d4. Oops.
1 parent 39b2582 commit 646c7af

3 files changed

Lines changed: 51 additions & 19 deletions

File tree

apps/desktop/src/lib/file-explorer/BriefList.svelte

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
createParentEntry,
1111
getEntryAt as getEntryAtUtil,
1212
fetchVisibleRange as fetchVisibleRangeUtil,
13+
calculateFetchRange,
14+
isRangeCached,
1315
shouldResetCache,
1416
} from './file-list-utils'
1517
@@ -87,18 +89,6 @@
8789
containerWidth > 0 ? Math.min(calculatedColumnWidth, containerWidth) : calculatedColumnWidth,
8890
)
8991
90-
// Debug logging
91-
$effect(() => {
92-
// eslint-disable-next-line no-console
93-
console.log('[BRIEF] maxFilenameWidth:', {
94-
backendMaxWidth,
95-
containerWidth,
96-
calculatedColumnWidth,
97-
maxFilenameWidth,
98-
COLUMN_PADDING,
99-
})
100-
})
101-
10292
// Total number of columns needed
10393
const totalColumns = $derived(Math.ceil(totalCount / itemsPerColumn))
10494
@@ -129,6 +119,13 @@
129119
const startItem = startCol * itemsPerColumn
130120
const endItem = Math.min(endCol * itemsPerColumn, totalCount)
131121
122+
// Check if range is already cached BEFORE setting isFetching
123+
// This prevents blocking subsequent fetches when data is already available
124+
const { fetchStart, fetchEnd } = calculateFetchRange({ startItem, endItem, hasParent, totalCount })
125+
if (isRangeCached(fetchStart, fetchEnd, cachedRange)) {
126+
return // Already cached
127+
}
128+
132129
isFetching = true
133130
try {
134131
const result = await fetchVisibleRangeUtil({

apps/desktop/src/lib/file-explorer/FullList.svelte

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
createParentEntry,
1010
getEntryAt as getEntryAtUtil,
1111
fetchVisibleRange as fetchVisibleRangeUtil,
12+
calculateFetchRange,
13+
isRangeCached,
1214
shouldResetCache,
1315
} from './file-list-utils'
1416
@@ -88,12 +90,22 @@
8890
async function fetchVisibleRange() {
8991
if (!listingId || isFetching) return
9092
93+
const startItem = virtualWindow.startIndex
94+
const endItem = virtualWindow.endIndex
95+
96+
// Check if range is already cached BEFORE setting isFetching
97+
// This prevents blocking subsequent fetches when data is already available
98+
const { fetchStart, fetchEnd } = calculateFetchRange({ startItem, endItem, hasParent, totalCount })
99+
if (isRangeCached(fetchStart, fetchEnd, cachedRange)) {
100+
return // Already cached
101+
}
102+
91103
isFetching = true
92104
try {
93105
const result = await fetchVisibleRangeUtil({
94106
listingId,
95-
startItem: virtualWindow.startIndex,
96-
endItem: virtualWindow.endIndex,
107+
startItem,
108+
endItem,
97109
hasParent,
98110
totalCount,
99111
includeHidden,

apps/desktop/src/lib/file-explorer/file-list-utils.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ export interface FetchRangeResult {
8585
range: { start: number; end: number }
8686
}
8787

88-
/** Fetches entries for a visible range with prefetch buffer */
89-
export async function fetchVisibleRange(params: FetchRangeParams): Promise<FetchRangeResult | null> {
90-
const { listingId, startItem, endItem, hasParent, totalCount, includeHidden, cachedRange, onSyncStatusRequest } =
91-
params
88+
/** Calculates the fetch range for visible items with prefetch buffer */
89+
export function calculateFetchRange(params: {
90+
startItem: number
91+
endItem: number
92+
hasParent: boolean
93+
totalCount: number
94+
}): { fetchStart: number; fetchEnd: number } {
95+
const { startItem, endItem, hasParent, totalCount } = params
9296

9397
// Account for ".." entry
9498
let adjustedStart = startItem
@@ -102,8 +106,27 @@ export async function fetchVisibleRange(params: FetchRangeParams): Promise<Fetch
102106
const fetchStart = Math.max(0, adjustedStart - PREFETCH_BUFFER / 2)
103107
const fetchEnd = Math.min(hasParent ? totalCount - 1 : totalCount, adjustedEnd + PREFETCH_BUFFER / 2)
104108

109+
return { fetchStart, fetchEnd }
110+
}
111+
112+
/** Checks if the needed range is already cached */
113+
export function isRangeCached(
114+
fetchStart: number,
115+
fetchEnd: number,
116+
cachedRange: { start: number; end: number },
117+
): boolean {
118+
return fetchStart >= cachedRange.start && fetchEnd <= cachedRange.end
119+
}
120+
121+
/** Fetches entries for a visible range with prefetch buffer */
122+
export async function fetchVisibleRange(params: FetchRangeParams): Promise<FetchRangeResult | null> {
123+
const { listingId, startItem, endItem, hasParent, totalCount, includeHidden, cachedRange, onSyncStatusRequest } =
124+
params
125+
126+
const { fetchStart, fetchEnd } = calculateFetchRange({ startItem, endItem, hasParent, totalCount })
127+
105128
// Only fetch if needed range isn't cached
106-
if (fetchStart >= cachedRange.start && fetchEnd <= cachedRange.end) {
129+
if (isRangeCached(fetchStart, fetchEnd, cachedRange)) {
107130
return null // Already cached
108131
}
109132

0 commit comments

Comments
 (0)