Skip to content

Commit 00e2886

Browse files
committed
Make clearRecords an atomic replacement to reduce reactive updates.
Make clearRecords test more thorough.
1 parent 6b07e2d commit 00e2886

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapi-vuex",
3-
"version": "5.11.1",
3+
"version": "5.12.0",
44
"description": "Access restructured JSONAPI data from a Vuex Store.",
55
"author": "Matthew Richardson <[email protected]>",
66
"scripts": {

src/mutations.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* @memberof module:jsonapi-vuex.jsonapiModule
66
*/
77

8-
import get from 'lodash/get'
98
import { utils } from './jsonapi-vuex'
109

1110
export default () => {
@@ -59,21 +58,13 @@ export default () => {
5958
utils.updateRecords(state, records, true)
6059
},
6160
/**
62-
* Delete all records from the store for a given type
61+
* Delete all records from the store (of a given type) other than those included in a given record
6362
* @memberof module:jsonapi-vuex.jsonapiModule.mutations
6463
* @param {object} state - The Vuex state object
6564
* @param {object} records - A record with type set.
6665
*/
6766
clearRecords: (state, records) => {
68-
const newRecords = utils.normToStore(records)
69-
for (let [type, item] of Object.entries(newRecords)) {
70-
const storeRecords = get(state, [type], {})
71-
for (let id of Object.keys(storeRecords)) {
72-
if (!utils.hasProperty(item, id)) {
73-
delete state[type][id]
74-
}
75-
}
76-
}
67+
Object.assign(state, utils.normToStore(records))
7768
},
7869
}
7970
}

tests/unit/jsonapi-vuex.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,12 @@ describe('jsonapi-vuex tests', function () {
205205
describe('clearRecords', function () {
206206
it('should remove records from the store not in the response (clearOnUpdate)', function () {
207207
const { clearRecords } = jm.mutations
208-
const state = { widget: { 4: { foo: 4 } } }
208+
const state = { widget: { 1: {}, 999: {} } }
209209
clearRecords(state, normRecord)
210-
// '4' not in storeRecord, so should no longer be present in state
211-
expect(state['widget']).to.not.have.property('4')
210+
// '1' is in normRecord, so should still be present in state
211+
expect(state['widget']).to.have.property('1')
212+
// '999' not in normRecord, so should no longer be present in state
213+
expect(state['widget']).to.not.have.property('999')
212214
})
213215
})
214216
}) // mutations

0 commit comments

Comments
 (0)