Skip to content

Commit 4c2cc1f

Browse files
committed
More cleanup, dry up some stuff, try to give better names
1 parent 9c41d3b commit 4c2cc1f

File tree

3 files changed

+56
-84
lines changed

3 files changed

+56
-84
lines changed

packages/ember-metal/lib/chains.js

+52-70
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,46 @@ ChainWatchers.prototype = {
6161
return false;
6262
},
6363

64-
finishAllChains() {
64+
revalidateAll() {
6565
for (let key in this.nodes) {
66-
this.finishChains(key);
66+
this.notify(key, true, undefined);
6767
}
6868
},
6969

70-
finishChains(key) {
71-
let nodes = this.nodes[key];
72-
if (nodes && nodes.length) {
73-
for (var i = 0, l = nodes.length; i < l; i++) {
74-
nodes[i].didChange(null);
75-
}
76-
}
70+
revalidate(key) {
71+
this.notify(key, true, undefined);
7772
},
7873

79-
willChange(key) {
74+
// key: the string key that is part of a path changed
75+
// revalidate: boolean the chains that are watching this value should revalidate
76+
// callback: function that will be called with the the object and path that
77+
// will be/are invalidated by this key change depending on the
78+
// whether the revalidate flag is passed
79+
notify(key, revalidate, callback) {
8080
let nodes = this.nodes[key];
81-
if (nodes && nodes.length) {
82-
let events = [];
83-
for (var i = 0, l = nodes.length; i < l; i++) {
84-
nodes[i].willChange(events);
85-
}
86-
return events;
81+
if (nodes === undefined || nodes.length === 0) {
82+
return;
8783
}
88-
},
8984

90-
didChange(key) {
91-
let nodes = this.nodes[key];
92-
if (nodes && nodes.length) {
93-
let events = [];
94-
for (var i = 0, l = nodes.length; i < l; i++) {
95-
nodes[i].didChange(events);
96-
}
97-
return events;
85+
let affected;
86+
87+
if (callback) {
88+
affected = [];
89+
}
90+
91+
for (let i = 0, l = nodes.length; i < l; i++) {
92+
nodes[i].notify(revalidate, affected);
93+
}
94+
95+
if (callback === undefined) {
96+
return;
97+
}
98+
99+
// we gather callbacks so we don't notify them during revalidation
100+
for (let i = 0, l = affected.length; i < l; i += 2) {
101+
let obj = affected[i];
102+
let path = affected[i + 1];
103+
callback(obj, path);
98104
}
99105
}
100106
};
@@ -356,44 +362,8 @@ ChainNode.prototype = {
356362
}
357363
},
358364

359-
willChange(events) {
360-
var chains = this._chains;
361-
var node;
362-
if (chains) {
363-
for (var key in chains) {
364-
node = chains[key];
365-
if (node !== undefined) {
366-
node.willChange(events);
367-
}
368-
}
369-
}
370-
371-
if (this._parent) {
372-
this._parent.notifyChainChange(this, this._key, 1, events);
373-
}
374-
},
375-
376-
notifyChainChange(chain, path, depth, events) {
377-
if (this._key) {
378-
path = this._key + '.' + path;
379-
}
380-
381-
if (this._parent) {
382-
this._parent.notifyChainChange(this, path, depth + 1, events);
383-
} else {
384-
if (depth > 1) {
385-
events.push(this.value(), path);
386-
}
387-
path = 'this.' + path;
388-
if (this._paths[path] > 0) {
389-
events.push(this.value(), path);
390-
}
391-
}
392-
},
393-
394-
didChange(events) {
395-
// invalidate my own value first.
396-
if (this._watching) {
365+
notify(revalidate, affected) {
366+
if (revalidate && this._watching) {
397367
var obj = this._parent.value();
398368
if (obj !== this._object) {
399369
removeChainWatcher(this._object, this._key, this);
@@ -416,19 +386,31 @@ ChainNode.prototype = {
416386
for (var key in chains) {
417387
node = chains[key];
418388
if (node !== undefined) {
419-
node.didChange(events);
389+
node.notify(revalidate, affected);
420390
}
421391
}
422392
}
423393

424-
// if no events are passed in then we only care about the above wiring update
425-
if (events === null) {
426-
return;
394+
if (affected && this._parent) {
395+
this._parent.populateAffected(this, this._key, 1, affected);
396+
}
397+
},
398+
399+
populateAffected(chain, path, depth, affected) {
400+
if (this._key) {
401+
path = this._key + '.' + path;
427402
}
428403

429-
// and finally tell parent about my path changing...
430404
if (this._parent) {
431-
this._parent.notifyChainChange(this, this._key, 1, events);
405+
this._parent.populateAffected(this, path, depth + 1, affected);
406+
} else {
407+
if (depth > 1) {
408+
affected.push(this.value(), path);
409+
}
410+
path = 'this.' + path;
411+
if (this._paths[path] > 0) {
412+
affected.push(this.value(), path);
413+
}
432414
}
433415
}
434416
};
@@ -440,7 +422,7 @@ export function finishChains(obj) {
440422
// finish any current chains node watchers that reference obj
441423
let chainWatchers = m.chainWatchers;
442424
if (chainWatchers) {
443-
chainWatchers.finishAllChains();
425+
chainWatchers.revalidateAll();
444426
}
445427
// copy chains from prototype
446428
let chains = m.chains;

packages/ember-metal/lib/computed.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ ComputedPropertyPrototype.get = function(obj, keyName) {
327327
}
328328

329329
if (meta.chainWatchers) {
330-
meta.chainWatchers.finishChains(keyName);
330+
meta.chainWatchers.revalidate(keyName);
331331
}
332332
addDependentKeys(this, obj, keyName, meta);
333333
} else {

packages/ember-metal/lib/property_events.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,22 @@ function chainsWillChange(obj, keyName, m) {
195195
return;
196196
}
197197

198-
let events = m.chainWatchers.willChange(keyName);
199-
if (events) {
200-
for (let i = 0, l = events.length; i < l; i += 2) {
201-
propertyWillChange(events[i], events[i + 1]);
202-
}
203-
}
198+
m.chainWatchers.notify(keyName, false, propertyWillChange);
204199
}
205200

206201
function chainsDidChange(obj, keyName, m) {
207202
if (m.chainWatchers === undefined || m.chainWatchers.src !== obj) {
208203
return;
209204
}
210205

211-
let events = m.chainWatchers.didChange(keyName);
212-
if (events) {
213-
for (let i = 0, l = events.length; i < l; i += 2) {
214-
propertyDidChange(events[i], events[i + 1]);
215-
}
216-
}
206+
m.chainWatchers.notify(keyName, true, propertyDidChange);
217207
}
218208

219209
function overrideChains(obj, keyName, m) {
220210
if (m.chainWatchers === undefined || m.chainWatchers.src !== obj) {
221211
return;
222212
}
223-
m.chainWatchers.finishChains(keyName);
213+
m.chainWatchers.revalidate(keyName);
224214
}
225215

226216
/**

0 commit comments

Comments
 (0)