From 0d4ea895578fbc5d847605def466a3f90f729a18 Mon Sep 17 00:00:00 2001 From: sly7-7 Date: Thu, 10 Oct 2019 18:30:25 +0200 Subject: [PATCH 1/4] [BUGFIX links] relationship setup for link fetch should batch --- .../store/addon/-private/system/core-store.ts | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/store/addon/-private/system/core-store.ts b/packages/store/addon/-private/system/core-store.ts index 83fbb6666ec..122195d93b5 100644 --- a/packages/store/addon/-private/system/core-store.ts +++ b/packages/store/addon/-private/system/core-store.ts @@ -1634,14 +1634,16 @@ abstract class CoreStore extends Service { if (shouldFindViaLink) { return this.findHasMany(parentInternalModel, resource.links.related, relationshipMeta, options).then( internalModels => { - let payload: { data: any[]; meta?: any } = { - data: internalModels.map(im => (recordDataFor(im) as RelationshipRecordData).getResourceIdentifier()), - }; - if (internalModels.meta !== undefined) { - payload.meta = internalModels.meta; - } - parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, payload); - return internalModels; + this._backburner.join(() => { + let payload: { data: any[]; meta?: any } = { + data: internalModels.map(im => (recordDataFor(im) as RelationshipRecordData).getResourceIdentifier()), + }; + if (internalModels.meta !== undefined) { + payload.meta = internalModels.meta; + } + parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, payload); + return internalModels; + }); } ); } @@ -1713,14 +1715,16 @@ abstract class CoreStore extends Service { } return this.findBelongsTo(parentInternalModel, resource.links.related, relationshipMeta, options).then( internalModel => { - let response = - internalModel && (recordDataFor(internalModel) as RelationshipRecordData).getResourceIdentifier(); - parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, { data: response }); - if (internalModel === null) { - return null; - } - // TODO Igor this doesn't seem like the right boundary, probably the caller method should extract the record out - return internalModel.getRecord(); + return this._backburner.join(() => { + let response = internalModel && (recordDataFor(internalModel) as RelationshipRecordData).getResourceIdentifier(); + parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, { data: response }); + if (internalModel === null) { + return null; + } + // TODO Igor this doesn't seem like the right boundary, probably the caller method should extract the record out + return internalModel.getRecord(); + } + ); } ); } From d76caf435aa34aa41192559693553a8025d547a2 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Thu, 10 Oct 2019 09:37:48 -0700 Subject: [PATCH 2/4] fix lint --- packages/store/addon/-private/system/core-store.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/store/addon/-private/system/core-store.ts b/packages/store/addon/-private/system/core-store.ts index 122195d93b5..9fd79df3418 100644 --- a/packages/store/addon/-private/system/core-store.ts +++ b/packages/store/addon/-private/system/core-store.ts @@ -1716,15 +1716,15 @@ abstract class CoreStore extends Service { return this.findBelongsTo(parentInternalModel, resource.links.related, relationshipMeta, options).then( internalModel => { return this._backburner.join(() => { - let response = internalModel && (recordDataFor(internalModel) as RelationshipRecordData).getResourceIdentifier(); + let response = + internalModel && (recordDataFor(internalModel) as RelationshipRecordData).getResourceIdentifier(); parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, { data: response }); if (internalModel === null) { return null; } // TODO Igor this doesn't seem like the right boundary, probably the caller method should extract the record out return internalModel.getRecord(); - } - ); + }); } ); } From dbdc49b570c3da63e5d9532ec02d3448d37574bf Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Thu, 10 Oct 2019 10:34:44 -0700 Subject: [PATCH 3/4] remove unnecessary things --- .../store/addon/-private/system/core-store.ts | 26 ++----------------- .../-private/system/model/internal-model.ts | 10 ------- .../addon/-private/system/store/finders.js | 5 ++-- 3 files changed, 5 insertions(+), 36 deletions(-) diff --git a/packages/store/addon/-private/system/core-store.ts b/packages/store/addon/-private/system/core-store.ts index 9fd79df3418..590db4d4cbe 100644 --- a/packages/store/addon/-private/system/core-store.ts +++ b/packages/store/addon/-private/system/core-store.ts @@ -1632,20 +1632,7 @@ abstract class CoreStore extends Service { // fetch via link if (shouldFindViaLink) { - return this.findHasMany(parentInternalModel, resource.links.related, relationshipMeta, options).then( - internalModels => { - this._backburner.join(() => { - let payload: { data: any[]; meta?: any } = { - data: internalModels.map(im => (recordDataFor(im) as RelationshipRecordData).getResourceIdentifier()), - }; - if (internalModels.meta !== undefined) { - payload.meta = internalModels.meta; - } - parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, payload); - return internalModels; - }); - } - ); + return this.findHasMany(parentInternalModel, resource.links.related, relationshipMeta, options); } let preferLocalCache = hasAnyRelationshipData && !relationshipIsEmpty; @@ -1715,16 +1702,7 @@ abstract class CoreStore extends Service { } return this.findBelongsTo(parentInternalModel, resource.links.related, relationshipMeta, options).then( internalModel => { - return this._backburner.join(() => { - let response = - internalModel && (recordDataFor(internalModel) as RelationshipRecordData).getResourceIdentifier(); - parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, { data: response }); - if (internalModel === null) { - return null; - } - // TODO Igor this doesn't seem like the right boundary, probably the caller method should extract the record out - return internalModel.getRecord(); - }); + return internalModel ? internalModel.getRecord() : null; } ); } diff --git a/packages/store/addon/-private/system/model/internal-model.ts b/packages/store/addon/-private/system/model/internal-model.ts index d7828926511..a5187221888 100644 --- a/packages/store/addon/-private/system/model/internal-model.ts +++ b/packages/store/addon/-private/system/model/internal-model.ts @@ -515,16 +515,6 @@ export default class InternalModel { } } - linkWasLoadedForRelationship(key, data) { - let relationships = {}; - relationships[key] = data; - this._recordData.pushData({ - id: this.id, - type: this.modelName, - relationships, - }); - } - finishedReloading() { this.isReloading = false; if (this.hasRecord) { diff --git a/packages/store/addon/-private/system/store/finders.js b/packages/store/addon/-private/system/store/finders.js index ca693887e08..240e31bfd84 100644 --- a/packages/store/addon/-private/system/store/finders.js +++ b/packages/store/addon/-private/system/store/finders.js @@ -130,12 +130,14 @@ function syncRelationshipDataFromLink(store, payload, parentInternalModel, relat // now, push the left hand side (the parent record) to ensure things are in sync, since // the payload will be pushed with store._push - store.push({ + store._push({ data: { id: parentInternalModel.id, type: parentInternalModel.modelName, relationships: { [relationship.key]: { + meta: payload.meta, + links: payload.links, data: relationshipData, }, }, @@ -285,7 +287,6 @@ export function _findHasMany(adapter, store, internalModel, link, relationship, syncRelationshipDataFromLink(store, payload, internalModel, relationship); let internalModelArray = store._push(payload); - internalModelArray.meta = payload.meta; return internalModelArray; }, null, From 64ffde4239123a6339d8e28049b42267170df09d Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Thu, 10 Oct 2019 10:50:46 -0700 Subject: [PATCH 4/4] utilize a single store._push to avoid double flushing relationship changes --- .../addon/-private/system/store/finders.js | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/store/addon/-private/system/store/finders.js b/packages/store/addon/-private/system/store/finders.js index 240e31bfd84..5f91898f5cd 100644 --- a/packages/store/addon/-private/system/store/finders.js +++ b/packages/store/addon/-private/system/store/finders.js @@ -130,19 +130,24 @@ function syncRelationshipDataFromLink(store, payload, parentInternalModel, relat // now, push the left hand side (the parent record) to ensure things are in sync, since // the payload will be pushed with store._push - store._push({ - data: { - id: parentInternalModel.id, - type: parentInternalModel.modelName, - relationships: { - [relationship.key]: { - meta: payload.meta, - links: payload.links, - data: relationshipData, - }, + const parentPayload = { + id: parentInternalModel.id, + type: parentInternalModel.modelName, + relationships: { + [relationship.key]: { + meta: payload.meta, + links: payload.links, + data: relationshipData, }, }, - }); + }; + + if (!Array.isArray(payload.included)) { + payload.included = []; + } + payload.included.push(parentPayload); + + return payload; } function ensureRelationshipIsSetToParent(payload, parentInternalModel, store, parentRelationship, index) { @@ -284,7 +289,7 @@ export function _findHasMany(adapter, store, internalModel, link, relationship, let serializer = store.serializerFor(relationship.type); let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findHasMany'); - syncRelationshipDataFromLink(store, payload, internalModel, relationship); + payload = syncRelationshipDataFromLink(store, payload, internalModel, relationship); let internalModelArray = store._push(payload); return internalModelArray; @@ -312,7 +317,7 @@ export function _findBelongsTo(adapter, store, internalModel, link, relationship return null; } - syncRelationshipDataFromLink(store, payload, internalModel, relationship); + payload = syncRelationshipDataFromLink(store, payload, internalModel, relationship); return store._push(payload); },