Skip to content

Commit

Permalink
optimize array seek
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed May 4, 2021
1 parent ea7dbbb commit 9946278
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
7 changes: 0 additions & 7 deletions packages/model/addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,6 @@ export default EmberObject.extend(MutableArray, DeprecatedEvented, {
this.initialState = undefined;
},

// TODO: if(DEBUG)
anyUnloaded() {
// Use `filter[0]` as opposed to `find` because of IE11
let unloaded = this.currentState.filter((im) => im._isDematerializing || !im.currentState.isLoaded)[0];
return !!unloaded;
},

removeUnloadedInternalModel() {
for (let i = 0; i < this.currentState.length; ++i) {
let internalModel = this.currentState[i];
Expand Down
16 changes: 15 additions & 1 deletion packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ export default class InternalModel {
} else {
assert(
`You looked up the '${key}' relationship on a '${this.modelName}' with id ${this.id} but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async ('hasMany({ async: true })')`,
!manyArray.anyUnloaded()
!anyUnloaded(manyArray)
);

return manyArray;
Expand Down Expand Up @@ -1569,3 +1569,17 @@ export function extractRecordDataFromRecord(recordOrPromiseRecord) {

return recordDataFor(recordOrPromiseRecord);
}

function anyUnloaded(manyArray) {
// Can't use `find` because of IE11 and these arrays are potentially massive
let state = manyArray.currentState;
let unloaded = false;
for (let i = 0; i < this.currentState.length; i++) {
let im = state[i];
if (im._isDematerializing || !im.currentState.isLoaded) {
unloaded = true;
break;
}
}
return unloaded;
}
16 changes: 13 additions & 3 deletions packages/store/addon/-private/system/store/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,19 @@ function fixRelationshipData(relationshipData, relationshipKind, { id, modelName
if (relationshipKind === 'hasMany') {
payload = relationshipData || [];
if (relationshipData) {
if (
!relationshipData.filter((v) => v.type === parentRelationshipData.type && v.id === parentRelationshipData.id)[0]
) {
// IE11 does not support array.find
// these arrays could be massive so this is better than filter
// Note: this is potentially problematic if type/id are not in the
// same state of normalization.
let found = false;
for (let i = 0; i < relationshipData.length; i++) {
let v = relationshipData[i];
if (v.type === parentRelationshipData.type && v.id === parentRelationshipData.id) {
found = true;
break;
}
}
if (!found) {
payload.push(parentRelationshipData);
}
} else {
Expand Down

0 comments on commit 9946278

Please sign in to comment.