Skip to content

Commit

Permalink
Remove "pending flush" concept from chains
Browse files Browse the repository at this point in the history
  • Loading branch information
mmun committed Feb 9, 2016
1 parent 5164ff8 commit 419fc72
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 71 deletions.
41 changes: 6 additions & 35 deletions packages/ember-metal/lib/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ function isVolatile(obj) {
return !(isObject(obj) && obj.isDescriptor && obj._volatile === false);
}

function ChainWatchers(obj) {
// this obj would be the referencing chain node's parent node's value
this.obj = obj;
function ChainWatchers() {
// chain nodes that reference a key in this obj by key
// we only create ChainWatchers when we are going to add them
// so create this upfront
Expand Down Expand Up @@ -104,31 +102,8 @@ ChainWatchers.prototype = {
}
};

var pendingQueue = [];

// attempts to add the pendingQueue chains again. If some of them end up
// back in the queue and reschedule is true, schedules a timeout to try
// again.
export function flushPendingChains() {
if (pendingQueue.length === 0) {
return;
}

var queue = pendingQueue;
pendingQueue = [];

queue.forEach((q) => q[0].add(q[1]));

warn(
'Watching an undefined global, Ember expects watched globals to be ' +
'setup by the time the run loop is flushed, check for typos',
pendingQueue.length === 0,
{ id: 'ember-metal.chains-flush-pending-chains' }
);
}

function makeChainWatcher(obj) {
return new ChainWatchers(obj);
function makeChainWatcher() {
return new ChainWatchers();
}

function addChainWatcher(obj, keyName, node) {
Expand Down Expand Up @@ -340,25 +315,21 @@ ChainNode.prototype = {
}

if (affected && this._parent) {
this._parent.populateAffected(this, this._key, 1, affected);
this._parent.populateAffected(this._key, 1, affected);
}
},

populateAffected(chain, path, depth, affected) {
populateAffected(path, depth, affected) {
if (this._key) {
path = this._key + '.' + path;
}

if (this._parent) {
this._parent.populateAffected(this, path, depth + 1, affected);
this._parent.populateAffected(path, depth + 1, affected);
} else {
if (depth > 1) {
affected.push(this.value(), path);
}
path = 'this.' + path;
if (this._paths[path] > 0) {
affected.push(this.value(), path);
}
}
}
};
Expand Down
2 changes: 0 additions & 2 deletions packages/ember-metal/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ import {
import {
ChainNode,
finishChains,
flushPendingChains,
removeChainWatcher
} from 'ember-metal/chains';
import {
Expand Down Expand Up @@ -263,7 +262,6 @@ Ember.setProperties = setProperties;
Ember.watchKey = watchKey;
Ember.unwatchKey = unwatchKey;

Ember.flushPendingChains = flushPendingChains;
Ember.removeChainWatcher = removeChainWatcher;
Ember._ChainNode = ChainNode;
Ember.finishChains = finishChains;
Expand Down
5 changes: 1 addition & 4 deletions packages/ember-metal/lib/watching.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
*/

import {
removeChainWatcher,
flushPendingChains
removeChainWatcher
} from 'ember-metal/chains';
import {
watchKey,
Expand Down Expand Up @@ -58,8 +57,6 @@ export function watcherCount(obj, key) {
return (meta && meta.peekWatching(key)) || 0;
}

watch.flushPending = flushPendingChains;

export function unwatch(obj, _keyPath, m) {
// can't watch length on Array - it is special...
if (_keyPath === 'length' && Array.isArray(obj)) { return; }
Expand Down
31 changes: 1 addition & 30 deletions packages/ember-metal/tests/watching/watch_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {

var willCount, didCount,
willKeys, didKeys,
originalLookup, lookup, Global;
originalLookup, lookup;

QUnit.module('watch', {
setup() {
Expand Down Expand Up @@ -170,35 +170,6 @@ testBoth('watching an object value then unwatching should restore old value', fu
equal(get(get(get(foo, 'bar'), 'baz'), 'biff'), 'BIFF', 'biff should exist');
});

testBoth('watching a global object that does not yet exist should queue', function(get, set) {
lookup['Global'] = Global = null;

var obj = {};
addListeners(obj, 'Global.foo');

watch(obj, 'Global.foo'); // only works on global chained props

equal(willCount, 0, 'should not have fired yet');
equal(didCount, 0, 'should not have fired yet');

lookup['Global'] = Global = { foo: 'bar' };
addListeners(Global, 'foo');

watch.flushPending(); // this will also be invoked automatically on ready

equal(willCount, 0, 'should not have fired yet');
equal(didCount, 0, 'should not have fired yet');

set(Global, 'foo', 'baz');

// should fire twice because this is a chained property (once on key, once
// on path)
equal(willCount, 2, 'should be watching');
equal(didCount, 2, 'should be watching');

lookup['Global'] = Global = null; // reset
});

QUnit.test('when watching another object, destroy should remove chain watchers from the other object', function() {
var objA = {};
var objB = { foo: 'bar' };
Expand Down

0 comments on commit 419fc72

Please sign in to comment.