Skip to content

Commit 95cd041

Browse files
author
Daan van der Kallen
committed
Do not parse model again if it came from cache
1 parent 7d6a09a commit 95cd041

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

dist/mobx-spine.cjs.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
11561156
this.__parseRelations(options.relations, options.relsFromCache);
11571157
}
11581158
if (data) {
1159-
this.parse(data);
1159+
this.parse(data, options.relsFromCache);
11601160
}
11611161
this.initialize();
11621162

@@ -1573,17 +1573,26 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
15731573
value: function parse(data) {
15741574
var _this9 = this;
15751575

1576+
var relsFromCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1577+
15761578
invariant(lodash.isPlainObject(data), 'Parameter supplied to `parse()` is not an object, got: ' + JSON.stringify(data));
15771579

15781580
lodash.forIn(data, function (value, key) {
15791581
var attr = _this9.constructor.fromBackendAttrKey(key);
15801582
if (_this9.__attributes.includes(attr)) {
15811583
_this9[attr] = _this9.__parseAttr(attr, value);
15821584
} else if (_this9.__activeCurrentRelations.includes(attr)) {
1585+
var cacheData = relsFromCache[attr];
1586+
1587+
// Model came from cache so we do not have to parse it again
1588+
if (cacheData && cacheData.model) {
1589+
return;
1590+
}
1591+
15831592
// In Binder, a relation property is an `int` or `[int]`, referring to its ID.
15841593
// However, it can also be an object if there are nested relations (non flattened).
15851594
if (lodash.isPlainObject(value) || Array.isArray(value) && value.every(lodash.isPlainObject)) {
1586-
_this9[attr].parse(value);
1595+
_this9[attr].parse(value, cacheData && cacheData.rels);
15871596
} else if (value === null) {
15881597
// The relation is cleared.
15891598
_this9[attr].clear();

dist/mobx-spine.es.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
11501150
this.__parseRelations(options.relations, options.relsFromCache);
11511151
}
11521152
if (data) {
1153-
this.parse(data);
1153+
this.parse(data, options.relsFromCache);
11541154
}
11551155
this.initialize();
11561156

@@ -1567,17 +1567,26 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
15671567
value: function parse(data) {
15681568
var _this9 = this;
15691569

1570+
var relsFromCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1571+
15701572
invariant(isPlainObject(data), 'Parameter supplied to `parse()` is not an object, got: ' + JSON.stringify(data));
15711573

15721574
forIn(data, function (value, key) {
15731575
var attr = _this9.constructor.fromBackendAttrKey(key);
15741576
if (_this9.__attributes.includes(attr)) {
15751577
_this9[attr] = _this9.__parseAttr(attr, value);
15761578
} else if (_this9.__activeCurrentRelations.includes(attr)) {
1579+
var cacheData = relsFromCache[attr];
1580+
1581+
// Model came from cache so we do not have to parse it again
1582+
if (cacheData && cacheData.model) {
1583+
return;
1584+
}
1585+
15771586
// In Binder, a relation property is an `int` or `[int]`, referring to its ID.
15781587
// However, it can also be an object if there are nested relations (non flattened).
15791588
if (isPlainObject(value) || Array.isArray(value) && value.every(isPlainObject)) {
1580-
_this9[attr].parse(value);
1589+
_this9[attr].parse(value, cacheData && cacheData.rels);
15811590
} else if (value === null) {
15821591
// The relation is cleared.
15831592
_this9[attr].clear();

src/Model.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export default class Model {
195195
this.__parseRelations(options.relations, options.relsFromCache);
196196
}
197197
if (data) {
198-
this.parse(data);
198+
this.parse(data, options.relsFromCache);
199199
}
200200
this.initialize();
201201

@@ -599,7 +599,7 @@ export default class Model {
599599
}
600600

601601
@action
602-
parse(data) {
602+
parse(data, relsFromCache = {}) {
603603
invariant(
604604
isPlainObject(data),
605605
`Parameter supplied to \`parse()\` is not an object, got: ${JSON.stringify(
@@ -612,10 +612,17 @@ export default class Model {
612612
if (this.__attributes.includes(attr)) {
613613
this[attr] = this.__parseAttr(attr, value);
614614
} else if (this.__activeCurrentRelations.includes(attr)) {
615+
const cacheData = relsFromCache[attr];
616+
617+
// Model came from cache so we do not have to parse it again
618+
if (cacheData && cacheData.model) {
619+
return
620+
}
621+
615622
// In Binder, a relation property is an `int` or `[int]`, referring to its ID.
616623
// However, it can also be an object if there are nested relations (non flattened).
617624
if (isPlainObject(value) || (Array.isArray(value) && value.every(isPlainObject))) {
618-
this[attr].parse(value);
625+
this[attr].parse(value, cacheData && cacheData.rels);
619626
} else if (value === null) {
620627
// The relation is cleared.
621628
this[attr].clear();

0 commit comments

Comments
 (0)