-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 3 commits
0286fd0
661d49f
aeabb93
5610b11
ee2bcda
f898456
e68a3cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets leave |
||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
}, | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.pushObject(record); | ||
|
||
return record; | ||
|
There was a problem hiding this comment.
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