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 igorT committed Nov 6, 2019
1 parent e6e1ce8 commit be3f5fa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
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 @@ -477,16 +477,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
21 changes: 2 additions & 19 deletions packages/store/addon/-private/system/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1401,18 +1401,7 @@ class Store 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).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 @@ -1482,13 +1471,7 @@ class Store extends Service {
}
return this.findBelongsTo(parentInternalModel, resource.links.related, relationshipMeta, options).then(
internalModel => {
let response = internalModel && recordDataFor(internalModel).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
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 @@ -131,17 +131,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 @@ -283,10 +290,9 @@ export function _findHasMany(adapter, store, internalModel, link, relationship,
let serializer = serializerForAdapter(store, adapter, 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 @@ -312,7 +318,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 be3f5fa

Please sign in to comment.