Skip to content

Commit 1f2aafc

Browse files
committed
fix: check if collection wasn't removed in query tracking
1 parent 2eab3fc commit 1f2aafc

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

packages/vue/src/tracking.ts

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,19 @@ export function useQueryTracking<TResult>(options: UseQueryTrackingOptions<TResu
7373

7474
// Mark new tracked items as fresh
7575
for (const collectionName in newQueryTracking.items) {
76-
const collection = store.$collections.find(c => c.name === collectionName)!
77-
const oldKeys = queryTracking?.items[collectionName]
78-
for (const key of newQueryTracking.items[collectionName]!) {
79-
const item = store.$cache.readItem({
80-
collection,
81-
key,
82-
}) as WrappedItemBase<Collection, CollectionDefaults, StoreSchema> | undefined
83-
if (item) {
84-
item.$meta.queries.add(trackingQueryId)
85-
item.$meta.dirtyQueries.delete(trackingQueryId)
86-
oldKeys?.delete(key)
76+
const collection = store.$collections.find(c => c.name === collectionName)
77+
if (collection) {
78+
const oldKeys = queryTracking?.items[collectionName]
79+
for (const key of newQueryTracking.items[collectionName]!) {
80+
const item = store.$cache.readItem({
81+
collection,
82+
key,
83+
}) as WrappedItemBase<Collection, CollectionDefaults, StoreSchema> | undefined
84+
if (item) {
85+
item.$meta.queries.add(trackingQueryId)
86+
item.$meta.dirtyQueries.delete(trackingQueryId)
87+
oldKeys?.delete(key)
88+
}
8789
}
8890
}
8991
}
@@ -92,26 +94,28 @@ export function useQueryTracking<TResult>(options: UseQueryTrackingOptions<TResu
9294
let hasAddedDirty = false
9395
if (queryTracking && markPreviousItemsAsDirty) {
9496
for (const collectionName in queryTracking.items) {
95-
const collection = store.$collections.find(c => c.name === collectionName)!
96-
for (const key of queryTracking.items[collectionName]!) {
97-
const item = store.$cache.readItem({
98-
collection,
99-
key,
100-
}) as WrappedItemBase<Collection, CollectionDefaults, StoreSchema> | undefined
101-
if (item) {
102-
item.$meta.queries.delete(trackingQueryId)
103-
item.$meta.dirtyQueries.add(trackingQueryId)
104-
105-
hasAddedDirty = true
106-
107-
// Clean garbage after the dirty items have a change to be removed from other queries
108-
// (e.g. after `dataKey.value++` updates the `cached` computed property)
109-
nextTick(() => {
110-
store.$cache.garbageCollectItem({
111-
collection,
112-
item: item as any,
97+
const collection = store.$collections.find(c => c.name === collectionName)
98+
if (collection) {
99+
for (const key of queryTracking.items[collectionName]!) {
100+
const item = store.$cache.readItem({
101+
collection,
102+
key,
103+
}) as WrappedItemBase<Collection, CollectionDefaults, StoreSchema> | undefined
104+
if (item) {
105+
item.$meta.queries.delete(trackingQueryId)
106+
item.$meta.dirtyQueries.add(trackingQueryId)
107+
108+
hasAddedDirty = true
109+
110+
// Clean garbage after the dirty items have a change to be removed from other queries
111+
// (e.g. after `dataKey.value++` updates the `cached` computed property)
112+
nextTick(() => {
113+
store.$cache.garbageCollectItem({
114+
collection,
115+
item: item as any,
116+
})
113117
})
114-
})
118+
}
115119
}
116120
}
117121
}
@@ -131,15 +135,17 @@ export function useQueryTracking<TResult>(options: UseQueryTrackingOptions<TResu
131135
tryOnScopeDispose(() => {
132136
for (const [, queryTracking] of queryTrackings) {
133137
for (const collectionName in queryTracking.items) {
134-
const collection = store.$collections.find(c => c.name === collectionName)!
135-
for (const key of queryTracking.items[collectionName]!) {
136-
const item = store.$cache.readItem({
137-
collection,
138-
key,
139-
}) as WrappedItemBase<Collection, CollectionDefaults, StoreSchema> | undefined
140-
if (item) {
141-
item.$meta.queries.delete(trackingQueryId)
142-
item.$meta.dirtyQueries.add(trackingQueryId)
138+
const collection = store.$collections.find(c => c.name === collectionName)
139+
if (collection) {
140+
for (const key of queryTracking.items[collectionName]!) {
141+
const item = store.$cache.readItem({
142+
collection,
143+
key,
144+
}) as WrappedItemBase<Collection, CollectionDefaults, StoreSchema> | undefined
145+
if (item) {
146+
item.$meta.queries.delete(trackingQueryId)
147+
item.$meta.dirtyQueries.add(trackingQueryId)
148+
}
143149
}
144150
}
145151
}

0 commit comments

Comments
 (0)