Skip to content

Ensure liveQueryCollections are not ready until all it's source collections are ready #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/db/src/query/live-query-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ export function liveQueryCollectionOptions<

const allCollectionsReady = () => {
return Object.values(collections).every(
(collection) =>
collection.status === `ready` || collection.status === `initialCommit`
(collection) => collection.status === `ready`
)
}

Expand Down
9 changes: 6 additions & 3 deletions packages/db/tests/query/indexes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe(`Query Index Optimization`, () => {
getKey: (item) => item.id,
startSync: true,
sync: {
sync: ({ begin, write, commit }) => {
sync: ({ begin, write, commit, markReady }) => {
// Provide initial data through sync
begin()
for (const item of testData) {
Expand All @@ -247,6 +247,7 @@ describe(`Query Index Optimization`, () => {
})
}
commit()
markReady()

// Listen for mutations and sync them back (only register once)
if (!emitter.all.has(`sync`)) {
Expand Down Expand Up @@ -534,7 +535,7 @@ describe(`Query Index Optimization`, () => {
getKey: (item) => item.id,
startSync: true,
sync: {
sync: ({ begin, write, commit }) => {
sync: ({ begin, write, commit, markReady }) => {
begin()
write({
type: `insert`,
Expand All @@ -547,6 +548,7 @@ describe(`Query Index Optimization`, () => {
},
})
commit()
markReady()
},
},
})
Expand Down Expand Up @@ -622,7 +624,7 @@ describe(`Query Index Optimization`, () => {
getKey: (item) => item.id,
startSync: true,
sync: {
sync: ({ begin, write, commit }) => {
sync: ({ begin, write, commit, markReady }) => {
begin()
write({
type: `insert`,
Expand All @@ -645,6 +647,7 @@ describe(`Query Index Optimization`, () => {
},
})
commit()
markReady()
},
},
})
Expand Down
19 changes: 19 additions & 0 deletions packages/db/tests/query/live-query-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,23 @@ describe(`createLiveQueryCollection`, () => {
expect(liveQuery.status).toBe(`ready`)
expect(liveQuery.size).toBe(0)
})

it(`shouldn't call markReady when source collection sync doesn't call markReady`, async () => {
const collection = createCollection<{ id: string }>({
sync: {
sync({ begin, commit }) {
begin()
commit()
},
},
getKey: (item) => item.id,
startSync: true,
})

const liveQuery = createLiveQueryCollection({
query: (q) => q.from({ collection }),
startSync: true,
})
expect(liveQuery.isReady()).toBe(false)
})
})
Loading