Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise has many notifications #4850

Merged
merged 7 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions addon/-private/system/diff-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
@namespace
@method diff-array
@for DS
@param {Array} oldArray the old array
@param {Array} newArray the new array
@return {hash} {
firstChangeIndex: <integer>, // null if no change
addedCount: <integer>, // 0 if no change
removedCount: <integer> // 0 if no change
}
*/
const diffArray = function (oldArray, newArray) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mind if we do export default function diffArray

  • This lets readers easily see (without scanning the full file) that this method is the default export

const oldLength = oldArray.length;
const newLength = newArray.length;

const shortestLength = Math.min(oldLength, newLength);
let firstChangeIndex = null; // null signifies no changes

// find the first change
for (let i=0; i<shortestLength; i++) {
// compare each item in the array
if (oldArray[i] !== newArray[i]) {
firstChangeIndex = i;
break;
}
}

if (firstChangeIndex === null && newLength !== oldLength) {
// no change found in the overlapping block
// and array lengths differ,
// so change starts at end of overlap
firstChangeIndex = shortestLength;
}

let addedCount = 0;
let removedCount = 0;
if (firstChangeIndex !== null) {
// we found a change, find the end of the change
let unchangedEndBlockLength = shortestLength - firstChangeIndex;
// walk back from the end of both arrays until we find a change
for (let i=1; i<=shortestLength; i++) {
// compare each item in the array
if (oldArray[oldLength-i] !== newArray[newLength-i]) {
unchangedEndBlockLength = i-1;
break;
}
}
addedCount = newLength - unchangedEndBlockLength - firstChangeIndex;
removedCount = oldLength - unchangedEndBlockLength - firstChangeIndex;
}

return {
firstChangeIndex,
addedCount,
removedCount
};
}

export default diffArray;
38 changes: 22 additions & 16 deletions addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Ember from 'ember';
import { assert } from "ember-data/-private/debug";
import { PromiseArray } from "./promise-proxies";
import { _objectIsAlive } from "./store/common";
import diffArray from './diff-array';

const { get, set } = Ember;

Expand Down Expand Up @@ -145,25 +146,31 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {

//a hack for not removing new records
//TODO remove once we have proper diffing
let newRecords = this.currentState.filter(
const newRecords = this.currentState.filter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets leave let here

// only add new records which are not yet in the canonical state of this
// relationship (a new record can be in the canonical state if it has
// been 'acknowleged' to be in the relationship via a store.push)
(internalModel) => internalModel.isNew() && toSet.indexOf(internalModel) === -1
);
toSet = toSet.concat(newRecords);
let oldLength = this.length;
this.arrayContentWillChange(0, this.length, toSet.length);
// It’s possible the parent side of the relationship may have been unloaded by this point
if (_objectIsAlive(this)) {
this.set('length', toSet.length);
}
this.currentState = toSet;
this.arrayContentDidChange(0, oldLength, this.length);

if (isInitialized) {
//TODO Figure out to notify only on additions and maybe only if unloaded
this.relationship.notifyHasManyChanged();
// diff to find changes
const diff = diffArray(this.currentState, toSet);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let


if (diff.firstChangeIndex !== null) { // it's null if no change found
// we found a change
this.arrayContentWillChange(diff.firstChangeIndex, diff.removedCount, diff.addedCount);
// It’s possible the parent side of the relationship may have been unloaded by this point
if (_objectIsAlive(this)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are not alive, i think we should just skip the whole function, exiting at line 145.

this.set('length', toSet.length);
}
this.currentState = toSet;
this.arrayContentDidChange(diff.firstChangeIndex, diff.removedCount, diff.addedCount);
if (isInitialized && diff.addedCount > 0) {
//notify only on additions
//TODO only notify if unloaded
this.relationship.notifyHasManyChanged();
}
}
},

Expand Down Expand Up @@ -289,12 +296,11 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
@return {DS.Model} record
*/
createRecord(hash) {
let store = get(this, 'store');
let type = get(this, 'type');
let record;
const store = get(this, 'store');
const type = get(this, 'type');

assert(`You cannot add '${type.modelName}' records to this polymorphic relationship.`, !get(this, 'isPolymorphic'));
record = store.createRecord(type.modelName, hash);
const record = store.createRecord(type.modelName, hash);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let

this.pushObject(record);

return record;
Expand Down
Loading