Skip to content

Commit 6b07e2d

Browse files
committed
Minimise store changes to reduce reactive updates. More efficient merging.
1 parent 94db461 commit 6b07e2d

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/lib.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,10 @@ const Utils = class {
501501
* @param {object} results - JSONAPI record
502502
*/
503503
processIncludedRecords(context, results) {
504-
for (let item of get(results, ['data', 'included'], [])) {
505-
// Mark record as coming from included
506-
const includedItem = this.jsonapiToNormItem(item, 'isIncluded')
507-
context.commit('mergeRecords', includedItem)
508-
}
504+
context.commit(
505+
'mergeRecords',
506+
get(results, ['data', 'included'], []).map((item) => this.jsonapiToNormItem(item, 'isIncluded'))
507+
)
509508
}
510509

511510
/**
@@ -532,28 +531,34 @@ const Utils = class {
532531
* @param {object} records - Restructured records to be updated
533532
* @param {boolean} merging - Whether or not to merge or overwrite records
534533
*/
535-
updateRecords(state, records, merging = this.conf.mergeRecords) {
536-
let newState = {}
534+
updateRecords(state, records, mergeDefault = this.conf.mergeRecords) {
537535
const storeRecords = this.normToStore(records)
536+
let merging = {}
538537
for (let [type, item] of Object.entries(storeRecords)) {
539-
newState[type] = {}
540-
if (!this.hasProperty(state, type)) {
541-
state[type] = {}
542-
// If there's no type, then there are no existing records to merge
543-
merging = false
544-
}
545-
for (let [id, data] of Object.entries(item)) {
546-
if (merging) {
547-
const oldRecord = get(state, [type, id])
548-
if (oldRecord) {
549-
data = merge(oldRecord, data)
538+
let newRecords = item
539+
if (mergeDefault) {
540+
if (!(type in merging)) {
541+
merging[type] = true
542+
if (!this.hasProperty(state, type)) {
543+
// If there's no type, then there are no existing records to merge with
544+
merging[type] = false
550545
}
551546
}
552-
newState[type][id] = data
547+
if (merging[type]) {
548+
newRecords = Object.fromEntries(
549+
Object.entries(item).map(([id, data]) => {
550+
const oldRecord = get(state, [type, id])
551+
if (oldRecord) {
552+
data = merge(oldRecord, data)
553+
}
554+
return [id, data]
555+
})
556+
)
557+
}
553558
}
554559
// FIXME: Review with release of Vuex5 to see if there is a new ref()/reactive() approach
555560
// Maintain reactivity by 'touching' the 'root' state property
556-
state[type] = Object.assign({}, state[type], newState[type])
561+
state[type] = Object.assign({}, state[type], newRecords)
557562
}
558563
}
559564
}

tests/unit/actions/get.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ describe('get', function () {
119119
normMachine1._jv.isIncluded = true
120120
delete normWidget2._jv.isData
121121
delete normMachine1._jv.isData
122-
expect(stubContext.commit).to.have.been.calledWith('mergeRecords', normWidget2)
123-
expect(stubContext.commit).to.have.been.calledWith('mergeRecords', normMachine1)
122+
expect(stubContext.commit).to.have.been.calledWith('mergeRecords', [normWidget2, normMachine1])
124123
})
125124

126125
it('should return normalized data with expanded rels (single item)', async function () {

0 commit comments

Comments
 (0)