Skip to content

Commit

Permalink
Merge pull request #11558 from stefanpenner/only-invoke-still-present…
Browse files Browse the repository at this point in the history
…-chain-entries

only invoke didChange if a node is still present.
  • Loading branch information
stefanpenner committed Jun 30, 2015
2 parents 5adf966 + 20b16a5 commit 0c8910d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/ember-metal/lib/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,13 @@ ChainNode.prototype = {

willChange(events) {
var chains = this._chains;
var node;
if (chains) {
for (var key in chains) {
chains[key].willChange(events);
node = chains[key];
if (node !== undefined) {
node.willChange(events);
}
}
}

Expand Down Expand Up @@ -389,7 +393,10 @@ export function finishChains(obj) {
chainNodes = chainWatchers[key];
if (chainNodes) {
for (var i = 0, l = chainNodes.length; i < l; i++) {
chainNodes[i].didChange(null);
var node = chainNodes[i];
if (node) {
node.didChange(null);
}
}
}
}
Expand Down
54 changes: 53 additions & 1 deletion packages/ember-metal/tests/chains_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { addObserver } from 'ember-metal/observer';
import { get } from 'ember-metal/property_get';
import { finishChains } from 'ember-metal/chains';

import { defineProperty } from 'ember-metal/properties';
import computed from 'ember-metal/computed';
import { propertyDidChange } from 'ember-metal/property_events';
QUnit.module('Chains');

QUnit.test('finishChains should properly copy chains from prototypes to instances', function() {
Expand All @@ -14,3 +17,52 @@ QUnit.test('finishChains should properly copy chains from prototypes to instance

ok(obj['__ember_meta__'].chains !== childObj['__ember_meta__'].chains, 'The chains object is copied');
});


QUnit.test('observer and CP chains', function() {
var obj = { };

defineProperty(obj, 'foo', computed('qux.[]', function() { }));
defineProperty(obj, 'qux', computed(function() { }));

// create DK chains
get(obj, 'foo');

// create observer chain
addObserver(obj, 'qux.length', function() { });

/*
+-----+
| qux | root CP
+-----+
^
+------+-----+
| |
+--------+ +----+
| length | | [] | chainWatchers
+--------+ +----+
observer CP(foo, 'qux.[]')
*/


// invalidate qux
propertyDidChange(obj, 'qux');

// CP chain is blown away

/*
+-----+
| qux | root CP
+-----+
^
+------+xxxxxx
| x
+--------+ xxxxxx
| length | x [] x chainWatchers
+--------+ xxxxxx
observer CP(foo, 'qux.[]')
*/

get(obj, 'qux'); // CP chain re-recreated
ok(true, 'no crash');
});

0 comments on commit 0c8910d

Please sign in to comment.