Skip to content

Commit

Permalink
Don't use a mobx-observed property to cache relationships (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Delmont authored Feb 5, 2021
1 parent f593ec0 commit fa7a50f
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 40 deletions.
12 changes: 9 additions & 3 deletions dist/mobx-async-store.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2652,9 +2652,15 @@ function relatedToOne(targetOrModelKlass, property, descriptor) {

function getRelatedRecords(record, property) {
var modelType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var relationships = record.relationships;
var relationships = record.relationships,
cachedRelationships = record.cachedRelationships;
var relationType = modelType || property;
var references = relationships && relationships[relationType];

if (!references || !references.data) {
references = cachedRelationships && cachedRelationships[relationType];
}

var relatedRecords = []; // NOTE: If the record doesn't have a matching references for the relation type
// fall back to looking up records by a foreign id i.e record.related_record_id

Expand Down Expand Up @@ -2689,14 +2695,14 @@ function getRelatedRecords(record, property) {
}
}

record.relationships = _objectSpread$3(_objectSpread$3({}, relationships), {}, _defineProperty({}, relationType, _objectSpread$3(_objectSpread$3({}, references), {}, {
record.cachedRelationships = _objectSpread$3(_objectSpread$3({}, cachedRelationships), {}, _defineProperty({}, relationType, {
data: relatedRecords.map(function (r) {
return {
type: r.type,
id: r.id
};
})
})));
}));
}

return new RelatedRecordsArray(relatedRecords, record, relationType);
Expand Down
12 changes: 9 additions & 3 deletions dist/mobx-async-store.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2646,9 +2646,15 @@ function relatedToOne(targetOrModelKlass, property, descriptor) {

function getRelatedRecords(record, property) {
var modelType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var relationships = record.relationships;
var relationships = record.relationships,
cachedRelationships = record.cachedRelationships;
var relationType = modelType || property;
var references = relationships && relationships[relationType];

if (!references || !references.data) {
references = cachedRelationships && cachedRelationships[relationType];
}

var relatedRecords = []; // NOTE: If the record doesn't have a matching references for the relation type
// fall back to looking up records by a foreign id i.e record.related_record_id

Expand Down Expand Up @@ -2683,14 +2689,14 @@ function getRelatedRecords(record, property) {
}
}

record.relationships = _objectSpread$3(_objectSpread$3({}, relationships), {}, _defineProperty({}, relationType, _objectSpread$3(_objectSpread$3({}, references), {}, {
record.cachedRelationships = _objectSpread$3(_objectSpread$3({}, cachedRelationships), {}, _defineProperty({}, relationType, {
data: relatedRecords.map(function (r) {
return {
type: r.type,
id: r.id
};
})
})));
}));
}

return new RelatedRecordsArray(relatedRecords, record, relationType);
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/Model.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
14 changes: 7 additions & 7 deletions docs/classes/RelatedRecordsArray.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down Expand Up @@ -84,7 +84,7 @@ <h1>RelatedRecordsArray Class</h1>


<div class="foundat">
Defined in: <a href="../files/src_decorators_relationships.js.html#l221"><code>src&#x2F;decorators&#x2F;relationships.js:221</code></a>
Defined in: <a href="../files/src_decorators_relationships.js.html#l224"><code>src&#x2F;decorators&#x2F;relationships.js:224</code></a>
</div>


Expand Down Expand Up @@ -125,7 +125,7 @@ <h3 class="name"><code>RelatedRecordsArray</code></h3>
<div class="meta">
<p>
Defined in
<a href="../files/src_decorators_relationships.js.html#l221"><code>src&#x2F;decorators&#x2F;relationships.js:221</code></a>
<a href="../files/src_decorators_relationships.js.html#l224"><code>src&#x2F;decorators&#x2F;relationships.js:224</code></a>
</p>


Expand Down Expand Up @@ -274,7 +274,7 @@ <h3 class="name"><code>add</code></h3>
<div class="meta">
<p>
Defined in
<a href="../files/src_decorators_relationships.js.html#l252"><code>src&#x2F;decorators&#x2F;relationships.js:252</code></a>
<a href="../files/src_decorators_relationships.js.html#l255"><code>src&#x2F;decorators&#x2F;relationships.js:255</code></a>
</p>


Expand Down Expand Up @@ -396,7 +396,7 @@ <h3 class="name"><code>getRelatedRecord</code></h3>
<div class="meta">
<p>
Defined in
<a href="../files/src_decorators_relationships.js.html#l147"><code>src&#x2F;decorators&#x2F;relationships.js:147</code></a>
<a href="../files/src_decorators_relationships.js.html#l150"><code>src&#x2F;decorators&#x2F;relationships.js:150</code></a>
</p>


Expand Down Expand Up @@ -614,7 +614,7 @@ <h3 class="name"><code>remove</code></h3>
<div class="meta">
<p>
Defined in
<a href="../files/src_decorators_relationships.js.html#l294"><code>src&#x2F;decorators&#x2F;relationships.js:294</code></a>
<a href="../files/src_decorators_relationships.js.html#l297"><code>src&#x2F;decorators&#x2F;relationships.js:297</code></a>
</p>


Expand Down Expand Up @@ -686,7 +686,7 @@ <h3 class="name"><code>setRelatedRecord</code></h3>
<div class="meta">
<p>
Defined in
<a href="../files/src_decorators_relationships.js.html#l172"><code>src&#x2F;decorators&#x2F;relationships.js:172</code></a>
<a href="../files/src_decorators_relationships.js.html#l175"><code>src&#x2F;decorators&#x2F;relationships.js:175</code></a>
</p>


Expand Down
2 changes: 1 addition & 1 deletion docs/classes/Schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/Store.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
12 changes: 6 additions & 6 deletions docs/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "mobx-async-store",
"description": "Asyc Data Store for mobx",
"url": "https://github.com/artemis-ag/mobx-async-store",
"version": "3.2.5"
"version": "3.2.7"
},
"files": {
"src/decorators/attributes.js": {
Expand Down Expand Up @@ -68,7 +68,7 @@
"plugin_for": [],
"extension_for": [],
"file": "src/decorators/relationships.js",
"line": 221,
"line": 224,
"description": "An array that allows for updating store references and relationships",
"is_constructor": 1,
"params": [
Expand Down Expand Up @@ -219,15 +219,15 @@
},
{
"file": "src/decorators/relationships.js",
"line": 147,
"line": 150,
"description": "Handles getting polymorphic has_one/belong_to.",
"itemtype": "method",
"name": "getRelatedRecord",
"class": "RelatedRecordsArray"
},
{
"file": "src/decorators/relationships.js",
"line": 172,
"line": 175,
"description": "Handles setting polymorphic has_one/belong_to.\n- Validates the related record to make sure it inherits from `Model` class\n- Sets the relationship\n- Attempts to find an inverse relationship, and if successful adds it as well",
"itemtype": "method",
"name": "setRelatedRecord",
Expand Down Expand Up @@ -257,7 +257,7 @@
},
{
"file": "src/decorators/relationships.js",
"line": 252,
"line": 255,
"description": "Adds a record to the array, and updates references in the store, as well as inverse references",
"itemtype": "method",
"name": "add",
Expand All @@ -276,7 +276,7 @@
},
{
"file": "src/decorators/relationships.js",
"line": 294,
"line": 297,
"description": "Removes a record from the array, and updates references in the store, as well as inverse references",
"itemtype": "method",
"name": "remove",
Expand Down
2 changes: 1 addition & 1 deletion docs/files/src_Model.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
2 changes: 1 addition & 1 deletion docs/files/src_Store.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
2 changes: 1 addition & 1 deletion docs/files/src_decorators_attributes.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
15 changes: 9 additions & 6 deletions docs/files/src_decorators_relationships.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down Expand Up @@ -186,11 +186,15 @@ <h1 class="file-heading">File: src/decorators/relationships.js</h1>
* @param {String} modelType an override of the modelType
*/
export function getRelatedRecords (record, property, modelType = null) {
let { relationships } = record
let { relationships, cachedRelationships } = record

const relationType = modelType || property

const references = relationships &amp;&amp; relationships[relationType]
let references = relationships &amp;&amp; relationships[relationType]
if (!references || !references.data) {
references = cachedRelationships &amp;&amp; cachedRelationships[relationType]
}

let relatedRecords = []

// NOTE: If the record doesn&#x27;t have a matching references for the relation type
Expand All @@ -217,10 +221,9 @@ <h1 class="file-heading">File: src/decorators/relationships.js</h1>
}
}

record.relationships = {
...relationships,
record.cachedRelationships = {
...cachedRelationships,
[relationType]: {
...references,
data: relatedRecords.map(r =&gt; ({ type: r.type, id: r.id }))
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/files/src_schema.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
2 changes: 1 addition & 1 deletion docs/files/src_utils.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="../assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<h1><img src="./assets/css/logo.png" title="mobx-async-store" width="117" height="52"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 3.2.5</em>
<em>API Docs for: 3.2.7</em>
</div>
</div>
<div id="bd" class="yui3-g">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@artemisag/mobx-async-store",
"version": "3.2.5",
"version": "3.2.7",
"module": "dist/mobx-async-store.esm.js",
"browser": "dist/mobx-async-store.cjs.js",
"main": "dist/mobx-async-store.cjs.js",
Expand Down
13 changes: 8 additions & 5 deletions src/decorators/relationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ export function relatedToOne (targetOrModelKlass, property, descriptor) {
* @param {String} modelType an override of the modelType
*/
export function getRelatedRecords (record, property, modelType = null) {
let { relationships } = record
let { relationships, cachedRelationships } = record

const relationType = modelType || property

const references = relationships && relationships[relationType]
let references = relationships && relationships[relationType]
if (!references || !references.data) {
references = cachedRelationships && cachedRelationships[relationType]
}

let relatedRecords = []

// NOTE: If the record doesn't have a matching references for the relation type
Expand All @@ -132,10 +136,9 @@ export function getRelatedRecords (record, property, modelType = null) {
}
}

record.relationships = {
...relationships,
record.cachedRelationships = {
...cachedRelationships,
[relationType]: {
...references,
data: relatedRecords.map(r => ({ type: r.type, id: r.id }))
}
}
Expand Down

0 comments on commit fa7a50f

Please sign in to comment.