Skip to content

Commit f162fbb

Browse files
committed
Added TableModel:updateNormalized() override to fix state mutation bug in redux-db
1 parent a4f37bc commit f162fbb

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-db-extras",
3-
"version": "1.0.34",
3+
"version": "1.0.35",
44
"description": "Collections, selectors, and top-level record fields for redux-db",
55
"main": "src/index.js",
66
"scripts": {
@@ -13,6 +13,7 @@
1313
"reselect": "^3.0.1"
1414
},
1515
"peerDependencies": {
16+
"lodash": "^4.17.4",
1617
"redux-db": "^0.9.11"
1718
},
1819
"repository": {

src/TableModel.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,43 @@ export default class TableModel extends DefaultTableModel {
106106
this.dirty = true
107107
}
108108

109+
/**
110+
* Fix state mutation bug
111+
*/
112+
updateNormalized(table) {
113+
let dirty
114+
let { state } = this
115+
116+
const records = Object.keys(table.byId).map(id => {
117+
if (!state.byId[id])
118+
throw new Error("Failed to apply update. No \"" + this.schema.name + "\" record with id: " + id + " exists.")
119+
120+
const oldRecord = state.byId[id]
121+
const newRecord = { ...oldRecord, ...table.byId[id] }
122+
123+
const isModified = this.schema.isModified(oldRecord, newRecord)
124+
125+
if (isModified) {
126+
if (!this.dirty) {
127+
state = { ...state, byId: { ...state.byId } }
128+
this.dirty = true
129+
}
130+
131+
dirty = true
132+
state.byId[id] = newRecord
133+
}
134+
135+
return this.schema.db.factory.newRecordModel(id, this)
136+
})
137+
138+
if (dirty) {
139+
this.state = state
140+
this._updateIndexes(table)
141+
}
142+
143+
return records
144+
}
145+
109146
_cleanIndexes(id, record, indexes) {
110147
super._cleanIndexes(id, record, indexes)
111148

0 commit comments

Comments
 (0)