Skip to content

Commit d13cf73

Browse files
committed
fix: update keyedById to prevent infinite loops
This modifies the service plugin’s `addItems` mutation to prevent overwriting all of `keyedById` and `tempsById`. When queries are made using data derived from the store, since the `addItems` mutation was overwriting the entire `keyedById` object, the query would run again. These infinite loops were running even if you used a computed property as a go-between to cache the parts of the query derived from the store. In scenarios where the query uses data derived from the store and then updates the store when results arrive, this change allows using a computed property to properly cache and prevent infinite loops.
1 parent 84e722f commit d13cf73

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

src/service-module/service-module.mutations.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ export default function makeServiceMutations() {
2323
const Model = _get(models, `[${serverAlias}][${modelName}]`)
2424
const BaseModel = _get(models, `[${state.serverAlias}].BaseModel`)
2525

26-
const newKeyedById = { ...state.keyedById }
27-
const newTempsById = { ...state.tempsById }
28-
2926
for (let item of items) {
3027
const id = getId(item, idField)
3128
const isTemp = id === null || id === undefined
@@ -45,18 +42,15 @@ export default function makeServiceMutations() {
4542
tempId = assignTempId(state, item)
4643
}
4744
item.__isTemp = true
48-
newTempsById[tempId] = item
45+
Vue.set(state.tempsById, tempId, item)
4946
} else {
5047
// Only add the id if it's not already in the `ids` list.
5148
if (!state.ids.includes(id)) {
5249
state.ids.push(id)
5350
}
54-
newKeyedById[id] = item
51+
Vue.set(state.keyedById, id, item)
5552
}
5653
}
57-
58-
state.keyedById = newKeyedById
59-
state.tempsById = newTempsById
6054
}
6155

6256
function updateItems(state, items) {

0 commit comments

Comments
 (0)