Skip to content

Commit

Permalink
handle negative index cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed Jul 17, 2017
1 parent b036634 commit 8c953db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,22 @@ export function arrayContentDidChange(array, startIdx, removeAmt, addAmt) {
let meta = peekMeta(array);
let cache = meta && meta.readableCache();
if (cache !== undefined) {
if (cache.firstObject !== undefined && startIdx === 0) {
let length = get(array, 'length');
let addedAmount = (addAmt === -1 ? 0 : addAmt);
let removedAmount = (removeAmt === -1 ? 0 : removeAmt);
let delta = addedAmount - removedAmount;
let previousLength = length - delta;

let normalStartIdx = startIdx < 0 ? previousLength + startIdx : startIdx;
if (cache.firstObject !== undefined && normalStartIdx === 0) {
propertyWillChange(array, 'firstObject');
propertyDidChange(array, 'firstObject');
}

if (cache.lastObject !== undefined) {
let addedAmount = (addAmt === -1 ? 0 : addAmt);
let removedAmount = (removeAmt === -1 ? 0 : removeAmt);
let delta = addedAmount - removedAmount;
let length = get(array, 'length');

let lastIndex = length - delta - 1;
let lastAffectedIndex = startIdx + removedAmount;
if (lastIndex < lastAffectedIndex) {
let previousLastIndex = previousLength - 1;
let lastAffectedIndex = normalStartIdx + removedAmount;
if (previousLastIndex < lastAffectedIndex) {
propertyWillChange(array, 'lastObject');
propertyDidChange(array, 'lastObject');
}
Expand Down
22 changes: 22 additions & 0 deletions packages/ember-runtime/tests/suites/mutable_array/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ suite.test('[A,B,C,D].replace(2,2) => [A,B] + notify', function() {
equal(observer.validate('firstObject'), false, 'should NOT have notified firstObject once');
});

suite.test('[A,B,C,D].replace(-1,1) => [A,B,C] + notify', function() {
let before = this.newFixture(4);
let after = [before[0], before[1], before[2]];

let obj = this.newObject(before);
let observer = this.newObserver(obj, '[]', '@each', 'length', 'firstObject', 'lastObject');

obj.getProperties('firstObject', 'lastObject'); /* Prime the cache */

obj.replace(-1, 1);

deepEqual(this.toArray(obj), after, 'post item results');

equal(observer.timesCalled('[]'), 1, 'should have notified [] once');
equal(observer.timesCalled('@each'), 0, 'should not have notified @each once');
equal(observer.timesCalled('length'), 1, 'should have notified length once');
equal(observer.timesCalled('lastObject'), 1, 'should have notified lastObject once');

equal(observer.validate('firstObject'), false, 'should NOT have notified firstObject once');
});


suite.test('Adding object should notify enumerable observer', function() {
let fixtures = this.newFixture(4);
let obj = this.newObject(fixtures);
Expand Down

0 comments on commit 8c953db

Please sign in to comment.