Skip to content

Commit 3fb8d3e

Browse files
committed
tweaks
1 parent f1b24c2 commit 3fb8d3e

File tree

9 files changed

+242
-50
lines changed

9 files changed

+242
-50
lines changed

packages/db/src/collection.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,12 @@ export class CollectionImpl<
957957
for (const listener of this.changeListeners) {
958958
listener([])
959959
}
960+
// Emit to key-specific listeners
961+
for (const [_key, keyListeners] of this.changeKeyListeners) {
962+
for (const listener of keyListeners) {
963+
listener([])
964+
}
965+
}
960966
}
961967

962968
/**

packages/db/src/query/live-query-collection.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ export function liveQueryCollectionOptions<
168168
)
169169
}
170170

171+
const allCollectionsReadyOrInitialCommit = () => {
172+
return Object.values(collections).every(
173+
(collection) =>
174+
collection.status === `ready` || collection.status === `initialCommit`
175+
)
176+
}
177+
171178
let graphCache: D2 | undefined
172179
let inputsCache: Record<string, RootStreamBuilder<unknown>> | undefined
173180
let pipelineCache: ResultStream | undefined
@@ -293,7 +300,7 @@ export function liveQueryCollectionOptions<
293300

294301
const maybeRunGraph = () => {
295302
// We only run the graph if all the collections are ready
296-
if (allCollectionsReady()) {
303+
if (allCollectionsReadyOrInitialCommit()) {
297304
graph.run()
298305
// On the initial run, we may need to do an empty commit to ensure that
299306
// the collection is initialized
@@ -302,7 +309,9 @@ export function liveQueryCollectionOptions<
302309
commit()
303310
}
304311
// Mark the collection as ready after the first successful run
305-
markReady()
312+
if (allCollectionsReady()) {
313+
markReady()
314+
}
306315
}
307316
}
308317

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe(`createLiveQueryCollection`, () => {
171171
expect(liveQuery.size).toBe(0)
172172
})
173173

174-
it(`shouldn't call markReady when source collection sync doesn't call markReady`, async () => {
174+
it(`shouldn't call markReady when source collection sync doesn't call markReady`, () => {
175175
const collection = createCollection<{ id: string }>({
176176
sync: {
177177
sync({ begin, commit }) {

packages/react-db/tests/useLiveQuery.test.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,15 +1077,17 @@ describe(`Query Collections`, () => {
10771077
it(`should update isLoading when collection status changes`, async () => {
10781078
let beginFn: (() => void) | undefined
10791079
let commitFn: (() => void) | undefined
1080+
let markReadyFn: (() => void) | undefined
10801081

10811082
const collection = createCollection<Person>({
10821083
id: `status-change-has-loaded-test`,
10831084
getKey: (person: Person) => person.id,
10841085
startSync: false,
10851086
sync: {
1086-
sync: ({ begin, commit }) => {
1087+
sync: ({ begin, commit, markReady }) => {
10871088
beginFn = begin
10881089
commitFn = commit
1090+
markReadyFn = markReady
10891091
// Don't sync immediately
10901092
},
10911093
},
@@ -1116,9 +1118,10 @@ describe(`Query Collections`, () => {
11161118

11171119
// Trigger the first commit to make collection ready
11181120
act(() => {
1119-
if (beginFn && commitFn) {
1121+
if (beginFn && commitFn && markReadyFn) {
11201122
beginFn()
11211123
commitFn()
1124+
markReadyFn()
11221125
}
11231126
})
11241127

@@ -1202,8 +1205,10 @@ describe(`Query Collections`, () => {
12021205
it(`should handle isLoading with complex queries including joins`, async () => {
12031206
let personBeginFn: (() => void) | undefined
12041207
let personCommitFn: (() => void) | undefined
1208+
let personMarkReadyFn: (() => void) | undefined
12051209
let issueBeginFn: (() => void) | undefined
12061210
let issueCommitFn: (() => void) | undefined
1211+
let issueMarkReadyFn: (() => void) | undefined
12071212

12081213
const personCollection = createCollection<Person>({
12091214
id: `join-has-loaded-persons`,
@@ -1212,10 +1217,8 @@ describe(`Query Collections`, () => {
12121217
sync: {
12131218
sync: ({ begin, commit, markReady }) => {
12141219
personBeginFn = begin
1215-
personCommitFn = () => {
1216-
commit()
1217-
markReady()
1218-
}
1220+
personCommitFn = commit
1221+
personMarkReadyFn = markReady
12191222
// Don't sync immediately
12201223
},
12211224
},
@@ -1231,10 +1234,8 @@ describe(`Query Collections`, () => {
12311234
sync: {
12321235
sync: ({ begin, commit, markReady }) => {
12331236
issueBeginFn = begin
1234-
issueCommitFn = () => {
1235-
commit()
1236-
markReady()
1237-
}
1237+
issueCommitFn = commit
1238+
issueMarkReadyFn = markReady
12381239
// Don't sync immediately
12391240
},
12401241
},
@@ -1269,13 +1270,15 @@ describe(`Query Collections`, () => {
12691270

12701271
// Trigger the first commit for both collections to make them ready
12711272
act(() => {
1272-
if (personBeginFn && personCommitFn) {
1273+
if (personBeginFn && personCommitFn && personMarkReadyFn) {
12731274
personBeginFn()
12741275
personCommitFn()
1276+
personMarkReadyFn()
12751277
}
1276-
if (issueBeginFn && issueCommitFn) {
1278+
if (issueBeginFn && issueCommitFn && issueMarkReadyFn) {
12771279
issueBeginFn()
12781280
issueCommitFn()
1281+
issueMarkReadyFn()
12791282
}
12801283
})
12811284

packages/solid-db/tests/useLiveQuery.test.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,15 +1053,17 @@ describe(`Query Collections`, () => {
10531053
it(`should update isLoading when collection status changes`, async () => {
10541054
let beginFn: (() => void) | undefined
10551055
let commitFn: (() => void) | undefined
1056+
let markReadyFn: (() => void) | undefined
10561057

10571058
const collection = createCollection<Person>({
10581059
id: `status-change-has-loaded-test`,
10591060
getKey: (person: Person) => person.id,
10601061
startSync: false,
10611062
sync: {
1062-
sync: ({ begin, commit }) => {
1063+
sync: ({ begin, commit, markReady }) => {
10631064
beginFn = begin
10641065
commitFn = commit
1066+
markReadyFn = markReady
10651067
// Don't sync immediately
10661068
},
10671069
},
@@ -1089,9 +1091,10 @@ describe(`Query Collections`, () => {
10891091
collection.preload()
10901092

10911093
// Trigger the first commit to make collection ready
1092-
if (beginFn && commitFn) {
1094+
if (beginFn && commitFn && markReadyFn) {
10931095
beginFn()
10941096
commitFn()
1097+
markReadyFn()
10951098
}
10961099

10971100
// Insert data
@@ -1172,8 +1175,10 @@ describe(`Query Collections`, () => {
11721175
it(`should handle isLoading with complex queries including joins`, async () => {
11731176
let personBeginFn: (() => void) | undefined
11741177
let personCommitFn: (() => void) | undefined
1178+
let personMarkReadyFn: (() => void) | undefined
11751179
let issueBeginFn: (() => void) | undefined
11761180
let issueCommitFn: (() => void) | undefined
1181+
let issueMarkReadyFn: (() => void) | undefined
11771182

11781183
const personCollection = createCollection<Person>({
11791184
id: `join-has-loaded-persons`,
@@ -1182,10 +1187,8 @@ describe(`Query Collections`, () => {
11821187
sync: {
11831188
sync: ({ begin, commit, markReady }) => {
11841189
personBeginFn = begin
1185-
personCommitFn = () => {
1186-
commit()
1187-
markReady()
1188-
}
1190+
personCommitFn = commit
1191+
personMarkReadyFn = markReady
11891192
// Don't sync immediately
11901193
},
11911194
},
@@ -1201,10 +1204,8 @@ describe(`Query Collections`, () => {
12011204
sync: {
12021205
sync: ({ begin, commit, markReady }) => {
12031206
issueBeginFn = begin
1204-
issueCommitFn = () => {
1205-
commit()
1206-
markReady()
1207-
}
1207+
issueCommitFn = commit
1208+
issueMarkReadyFn = markReady
12081209
// Don't sync immediately
12091210
},
12101211
},
@@ -1236,13 +1237,15 @@ describe(`Query Collections`, () => {
12361237
issueCollection.preload()
12371238

12381239
// Trigger the first commit for both collections to make them ready
1239-
if (personBeginFn && personCommitFn) {
1240+
if (personBeginFn && personCommitFn && personMarkReadyFn) {
12401241
personBeginFn()
12411242
personCommitFn()
1243+
personMarkReadyFn()
12421244
}
1243-
if (issueBeginFn && issueCommitFn) {
1245+
if (issueBeginFn && issueCommitFn && issueMarkReadyFn) {
12441246
issueBeginFn()
12451247
issueCommitFn()
1248+
issueMarkReadyFn()
12461249
}
12471250

12481251
// Insert data into both collections

packages/svelte-db/src/useLiveQuery.svelte.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ export function useLiveQuery(
246246

247247
if (isCollection) {
248248
// It's already a collection, ensure sync is started for Svelte helpers
249-
unwrappedParam.startSyncImmediate()
249+
// Only start sync if the collection is in idle state
250+
if (unwrappedParam.status === `idle`) {
251+
unwrappedParam.startSyncImmediate()
252+
}
250253
return unwrappedParam
251254
}
252255

0 commit comments

Comments
 (0)