Skip to content

Commit

Permalink
[BUGFIX links] relationship setup for link fetch should batch (#6586)
Browse files Browse the repository at this point in the history
* [BUGFIX links] relationship setup for link fetch should batch

* fix lint

* remove unnecessary things

* utilize a single store._push to avoid double flushing relationship changes
  • Loading branch information
sly7-7 authored and runspired committed Oct 20, 2019
1 parent 73ebfba commit 8012b67
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
22 changes: 2 additions & 20 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1632,18 +1632,7 @@ abstract class CoreStore extends Service {

// fetch via link
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;
}
);
return this.findHasMany(parentInternalModel, resource.links.related, relationshipMeta, options);
}

let preferLocalCache = hasAnyRelationshipData && !relationshipIsEmpty;
Expand Down Expand Up @@ -1713,14 +1702,7 @@ 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 internalModel ? internalModel.getRecord() : null;
}
);
}
Expand Down
10 changes: 0 additions & 10 deletions packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 18 additions & 12 deletions packages/store/addon/-private/system/store/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +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]: {
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) {
Expand Down Expand Up @@ -282,10 +289,9 @@ 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);
internalModelArray.meta = payload.meta;
return internalModelArray;
},
null,
Expand All @@ -311,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);
},
Expand Down

0 comments on commit 8012b67

Please sign in to comment.