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

Get firstObject/lastObject notification only when cached #5591

Closed
Show file tree
Hide file tree
Changes from all 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
27 changes: 16 additions & 11 deletions packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Ember from 'ember-metal/core'; // ES6TODO: Ember.A

import symbol from 'ember-metal/symbol';
import { get } from 'ember-metal/property_get';
import { peekMeta } from 'ember-metal/utils';
import {
computed,
cacheFor
Expand Down Expand Up @@ -517,18 +518,22 @@ export default Mixin.create(Enumerable, {

sendEvent(this, '@array:change', [this, startIdx, removeAmt, addAmt]);

// firstObject/lastObject change notifications
// The call to objectAt() should only occur if there is a cached value
var length = get(this, 'length');
var cachedFirst = cacheFor(this, 'firstObject');
var cachedLast = cacheFor(this, 'lastObject');

if (objectAt(this, 0) !== cachedFirst) {
propertyWillChange(this, 'firstObject');
propertyDidChange(this, 'firstObject');
}

if (objectAt(this, length - 1) !== cachedLast) {
propertyWillChange(this, 'lastObject');
propertyDidChange(this, 'lastObject');
var meta = peekMeta(this);
var cache = meta && meta.readableCache();
if (cache) {
if (cache['firstObject'] !== undefined &&
this.objectAt(0) !== cacheFor(this, 'firstObject')) {
propertyWillChange(this, 'firstObject');
propertyDidChange(this, 'firstObject');
}
if (cache['lastObject'] !== undefined &&
this.objectAt(length - 1) !== cacheFor(this, 'lastObject')) {
propertyWillChange(this, 'lastObject');
propertyDidChange(this, 'lastObject');
}
}

return this;
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() {
var obj, observer, before, replace, after;

Expand Down