Skip to content

Commit f78c8e1

Browse files
committed
loadSubset fn return promise or true
1 parent 53fa027 commit f78c8e1

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

packages/db/src/collection/subscription.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,18 @@ export class CollectionSubscription
174174

175175
// Request the sync layer to load more data
176176
// don't await it, we will load the data into the collection when it comes in
177-
const syncPromise = this.collection._sync.loadSubset({
177+
const syncResult = this.collection._sync.loadSubset({
178178
where: stateOpts.where,
179179
subscription: this,
180180
})
181181

182-
// Track the promise if it exists
183-
if (syncPromise) {
184-
this.pendingLoadSubsetPromises.add(syncPromise)
182+
// Track the promise if it's actually a promise (async work)
183+
if (syncResult instanceof Promise) {
184+
this.pendingLoadSubsetPromises.add(syncResult)
185185
this.setStatus(`loadingMore`)
186186

187-
syncPromise.finally(() => {
188-
this.pendingLoadSubsetPromises.delete(syncPromise)
187+
syncResult.finally(() => {
188+
this.pendingLoadSubsetPromises.delete(syncResult)
189189
if (this.pendingLoadSubsetPromises.size === 0) {
190190
this.setStatus(`ready`)
191191
}
@@ -282,20 +282,20 @@ export class CollectionSubscription
282282

283283
// Request the sync layer to load more data
284284
// don't await it, we will load the data into the collection when it comes in
285-
const syncPromise = this.collection._sync.loadSubset({
285+
const syncResult = this.collection._sync.loadSubset({
286286
where: whereWithValueFilter,
287287
limit,
288288
orderBy,
289289
subscription: this,
290290
})
291291

292-
// Track the promise if it exists
293-
if (syncPromise) {
294-
this.pendingLoadSubsetPromises.add(syncPromise)
292+
// Track the promise if it's actually a promise (async work)
293+
if (syncResult instanceof Promise) {
294+
this.pendingLoadSubsetPromises.add(syncResult)
295295
this.setStatus(`loadingMore`)
296296

297-
syncPromise.finally(() => {
298-
this.pendingLoadSubsetPromises.delete(syncPromise)
297+
syncResult.finally(() => {
298+
this.pendingLoadSubsetPromises.delete(syncResult)
299299
if (this.pendingLoadSubsetPromises.size === 0) {
300300
this.setStatus(`ready`)
301301
}

packages/db/src/collection/sync.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class CollectionSyncManager<
3939
public preloadPromise: Promise<void> | null = null
4040
public syncCleanupFn: (() => void) | null = null
4141
public syncLoadSubsetFn:
42-
| ((options: LoadSubsetOptions) => void | Promise<void>)
42+
| ((options: LoadSubsetOptions) => true | Promise<void>)
4343
| null = null
4444

4545
private pendingLoadSubsetPromises: Set<Promise<void>> = new Set()
@@ -302,27 +302,24 @@ export class CollectionSyncManager<
302302
* Requests the sync layer to load more data.
303303
* @param options Options to control what data is being loaded
304304
* @returns If data loading is asynchronous, this method returns a promise that resolves when the data is loaded.
305-
* If data loading is synchronous, the data is loaded when the method returns.
306-
* Returns undefined if no sync function is configured or if syncMode is 'eager'.
305+
* Returns true if no sync function is configured, if syncMode is 'eager', or if there is no work to do.
307306
*/
308-
public loadSubset(options: LoadSubsetOptions): Promise<void> | undefined {
307+
public loadSubset(options: LoadSubsetOptions): Promise<void> | true {
309308
// Bypass loadSubset when syncMode is 'eager'
310309
if (this.syncMode === `eager`) {
311-
return undefined
310+
return true
312311
}
313312

314313
if (this.syncLoadSubsetFn) {
315314
const result = this.syncLoadSubsetFn(options)
316-
317-
// If the result is void (synchronous), wrap in Promise.resolve()
318-
const promise = result === undefined ? Promise.resolve() : result
319-
320-
// Track the promise
321-
this.trackLoadPromise(promise)
322-
323-
return promise
315+
// If the result is a promise, track it
316+
if (result instanceof Promise) {
317+
this.trackLoadPromise(result)
318+
return result
319+
}
324320
}
325-
return undefined
321+
322+
return true
326323
}
327324

328325
public cleanup(): void {

packages/db/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export type LoadSubsetOptions = {
221221
subscription?: Subscription
222222
}
223223

224-
export type LoadSubsetFn = (options: LoadSubsetOptions) => void | Promise<void>
224+
export type LoadSubsetFn = (options: LoadSubsetOptions) => true | Promise<void>
225225

226226
export type CleanupFn = () => void
227227

packages/db/tests/collection.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,4 +1567,28 @@ describe(`Collection isLoadingMore property`, () => {
15671567

15681568
expect(collection.isLoadingMore).toBe(false)
15691569
})
1570+
1571+
it(`isLoadingMore stays false when loadSubset returns true (no work to do)`, () => {
1572+
const collection = createCollection<{ id: string; value: string }>({
1573+
id: `test`,
1574+
getKey: (item) => item.id,
1575+
syncMode: `on-demand`,
1576+
startSync: true,
1577+
sync: {
1578+
sync: ({ markReady }) => {
1579+
markReady()
1580+
return {
1581+
loadSubset: () => true, // No work to do
1582+
}
1583+
},
1584+
},
1585+
})
1586+
1587+
expect(collection.isLoadingMore).toBe(false)
1588+
1589+
// Call loadSubset - it should return true and not track any promise
1590+
const result = collection._sync.loadSubset({})
1591+
expect(result).toBe(true)
1592+
expect(collection.isLoadingMore).toBe(false)
1593+
})
15701594
})

0 commit comments

Comments
 (0)