Skip to content

Commit

Permalink
[BUGFIX beta] mixin/arrays firstObject/lastObject notifications only …
Browse files Browse the repository at this point in the history
  • Loading branch information
opichals authored and stefanpenner committed Oct 19, 2016
1 parent e601285 commit 4fa699a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
25 changes: 15 additions & 10 deletions packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// HELPERS
//
import { symbol } from 'ember-utils';

import { peekMeta } from 'ember-metal';
import Ember, { // ES6TODO: Ember.A
get,
computed,
Expand Down Expand Up @@ -136,19 +138,22 @@ export function arrayContentDidChange(array, startIdx, removeAmt, addAmt) {
sendEvent(array, '@array:change', [array, startIdx, removeAmt, addAmt]);

let length = get(array, 'length');
let cachedFirst = cacheFor(array, 'firstObject');
let cachedLast = cacheFor(array, 'lastObject');

if (objectAt(array, 0) !== cachedFirst) {
propertyWillChange(array, 'firstObject');
propertyDidChange(array, 'firstObject');
}
let meta = peekMeta(array);
let cache = meta && meta.readableCache();

if (objectAt(array, length - 1) !== cachedLast) {
propertyWillChange(array, 'lastObject');
propertyDidChange(array, 'lastObject');
if (cache) {
if (cache.firstObject !== undefined &&
objectAt(array, 0) !== cacheFor.get(cache, 'firstObject')) {
propertyWillChange(array, 'firstObject');
propertyDidChange(array, 'firstObject');
}
if (cache.lastObject !== undefined &&
objectAt(array, length - 1) !== cacheFor.get(cache, 'lastObject')) {
propertyWillChange(array, 'lastObject');
propertyDidChange(array, 'lastObject');
}
}

return array;
}

Expand Down
17 changes: 17 additions & 0 deletions packages/ember-runtime/tests/suites/mutable_array/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ suite.test('[].replace(0,0,\'X\') => [\'X\'] + notify', function() {
equal(observer.timesCalled('lastObject'), 1, 'should have notified lastObject once');
});

suite.test('[].replace(0,0,"X") => ["X"] + avoid calling objectAt and notifying fistObject/lastObject when not in cache', function() {
var obj, exp, observer;
var called = 0;
exp = this.newFixture(1);
obj = this.newObject([]);
obj.objectAt = function() {
called++;
};
observer = this.newObserver(obj, 'firstObject', 'lastObject');

obj.replace(0, 0, exp);

equal(called, 0, 'should NOT have called objectAt upon replace when firstObject/lastObject are not cached');
equal(observer.validate('firstObject'), false, 'should NOT have notified firstObject since not cached');
equal(observer.validate('lastObject'), false, 'should NOT have notified lastObject since not cached');
});

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

0 comments on commit 4fa699a

Please sign in to comment.