Skip to content

Commit 581d7cc

Browse files
committed
address review
1 parent c9a8721 commit 581d7cc

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

packages/electric-db-collection/src/electric.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ type InferSchemaOutput<T> = T extends StandardSchemaV1
8383
* - collection will be marked as ready once the sync is complete
8484
* - there is no incremental sync
8585
* - `on-demand`:
86-
* - syncs data synced in incremental snapshots as the collection is queried
86+
* - syncs data in incremental snapshots when the collection is queried
8787
* - collection will be marked as ready immediately after the first snapshot is synced
8888
* - `progressive`:
89-
* - syncs all data in the shape in the background
89+
* - syncs all data for the collection in the background
9090
* - uses incremental snapshots during the initial sync to provide a fast path to the data required for queries
91-
* - collection will be marked as ready once the initial sync is complete
91+
* - collection will be marked as ready once the full sync is complete
9292
*/
9393
export type SyncMode = `eager` | `on-demand` | `progressive`
9494

@@ -851,7 +851,7 @@ function createElectricSync<T extends Row<unknown>>(
851851
})
852852

853853
// Only set onLoadMore if the sync mode is not eager, this indicates to the sync
854-
// layer can load more data on demand via the requestSnapshot method when,
854+
// layer that it can load more data on demand via the requestSnapshot method when,
855855
// the syncMode = `on-demand` or `progressive`
856856
const onLoadMore =
857857
syncMode === `eager`

packages/electric-db-collection/tests/electric-live-query.test.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -471,25 +471,25 @@ describe.each([
471471
// Create a new electric collection with on-demand syncMode for this test
472472
vi.clearAllMocks()
473473

474-
const testSubscriber = vi.fn<(messages: Array<Message<User>>) => void>()
474+
let testSubscriber: (messages: Array<Message<User>>) => void = () => {}
475475
mockSubscribe.mockImplementation((callback) => {
476-
testSubscriber.mockImplementation(callback)
476+
testSubscriber = callback
477477
return () => {}
478478
})
479479

480-
const testElectricCollection = createCollection({
481-
...electricCollectionOptions({
480+
const testElectricCollection = createCollection(
481+
electricCollectionOptions({
482482
id: `test-incremental-loading`,
483483
shapeOptions: {
484484
url: `http://test-url`,
485485
params: { table: `users` },
486486
},
487487
syncMode: `on-demand`,
488488
getKey: (user: User) => user.id,
489-
}),
490-
startSync: true,
491-
autoIndex: `eager` as const,
492-
})
489+
startSync: true,
490+
autoIndex: `eager` as const,
491+
})
492+
)
493493

494494
mockRequestSnapshot.mockResolvedValue({
495495
data: [],
@@ -718,8 +718,15 @@ describe(`Electric Collection with Live Query - syncMode integration`, () => {
718718
// Wait for the live query to process
719719
await new Promise((resolve) => setTimeout(resolve, 0))
720720

721-
// Should have requested more data from Electric
722-
expect(mockRequestSnapshot).toHaveBeenCalled()
721+
// Should have requested more data from Electric with correct parameters
722+
expect(mockRequestSnapshot).toHaveBeenCalledWith(
723+
expect.objectContaining({
724+
limit: 5, // Requests full limit from Electric
725+
orderBy: `age NULLS FIRST`,
726+
where: `active = $1`,
727+
params: { 1: `true` }, // Parameters are stringified
728+
})
729+
)
723730
expect(liveQuery.size).toBeGreaterThan(2)
724731
})
725732

@@ -757,8 +764,15 @@ describe(`Electric Collection with Live Query - syncMode integration`, () => {
757764
// Wait for the live query to process
758765
await new Promise((resolve) => setTimeout(resolve, 0))
759766

760-
// Should have requested more data from Electric
761-
expect(mockRequestSnapshot).toHaveBeenCalled()
767+
// Should have requested more data from Electric with correct parameters
768+
// First request asks for the full limit
769+
expect(mockRequestSnapshot).toHaveBeenCalledWith(
770+
expect.objectContaining({
771+
limit: 3, // Requests full limit from Electric
772+
orderBy: `id NULLS FIRST`,
773+
params: {},
774+
})
775+
)
762776
})
763777

764778
it(`should NOT trigger requestSnapshot in eager mode even when live query needs more data`, async () => {

0 commit comments

Comments
 (0)