Skip to content

Commit

Permalink
avoid Object.create(null) add some comments update some naming to be …
Browse files Browse the repository at this point in the history
…more consistent
  • Loading branch information
krisselden committed Jul 23, 2015
1 parent 9d29556 commit 95d3f3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
27 changes: 15 additions & 12 deletions packages/ember-metal/lib/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ function Chains() { }

Chains.prototype = Object.create(null);

// a map of nodes that reference a key
function ChainWatchers(src) {
this.src = src;
this.nodes = Object.create(null);
function ChainWatchers(obj) {
// this obj would be the referencing chain node's parent node's value
this.obj = obj;
// 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
this.chains = new Chains();
}

ChainWatchers.prototype = {
add(key, node) {
let nodes = this.nodes[key];
let nodes = this.chains[key];
if (nodes === undefined) {
this.nodes[key] = [node];
this.chains[key] = [node];
} else {
nodes.push(node);
}
},

remove(key, node) {
let nodes = this.nodes[key];
let nodes = this.chains[key];
if (nodes) {
for (var i = 0, l = nodes.length; i < l; i++) {
if (nodes[i] === node) {
Expand All @@ -50,7 +53,7 @@ ChainWatchers.prototype = {
},

has(key, node) {
let nodes = this.nodes[key];
let nodes = this.chains[key];
if (nodes) {
for (var i = 0, l = nodes.length; i < l; i++) {
if (nodes[i] === node) {
Expand All @@ -62,7 +65,7 @@ ChainWatchers.prototype = {
},

revalidateAll() {
for (let key in this.nodes) {
for (let key in this.chains) {
this.notify(key, true, undefined);
}
},
Expand All @@ -77,7 +80,7 @@ ChainWatchers.prototype = {
// will be/are invalidated by this key change depending on the
// whether the revalidate flag is passed
notify(key, revalidate, callback) {
let nodes = this.nodes[key];
let nodes = this.chains[key];
if (nodes === undefined || nodes.length === 0) {
return;
}
Expand Down Expand Up @@ -135,7 +138,7 @@ function addChainWatcher(obj, keyName, node) {

let m = metaFor(obj);

if (m.chainWatchers === undefined || m.chainWatchers.src !== obj) {
if (m.chainWatchers === undefined || m.chainWatchers.obj !== obj) {
m.chainWatchers = new ChainWatchers(obj);
}

Expand All @@ -152,7 +155,7 @@ function removeChainWatcher(obj, keyName, node) {
let m = obj.__ember_meta__;

if (!m ||
m.chainWatchers === undefined || m.chainWatchers.src !== obj) {
m.chainWatchers === undefined || m.chainWatchers.obj !== obj) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/ember-metal/lib/property_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,23 @@ function iterDeps(method, obj, deps, depKey, seen, meta) {
}

function chainsWillChange(obj, keyName, m) {
if (m.chainWatchers === undefined || m.chainWatchers.src !== obj) {
if (m.chainWatchers === undefined || m.chainWatchers.obj !== obj) {
return;
}

m.chainWatchers.notify(keyName, false, propertyWillChange);
}

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

m.chainWatchers.notify(keyName, true, propertyDidChange);
}

function overrideChains(obj, keyName, m) {
if (m.chainWatchers === undefined || m.chainWatchers.src !== obj) {
if (m.chainWatchers === undefined || m.chainWatchers.obj !== obj) {
return;
}
m.chainWatchers.revalidate(keyName);
Expand Down

0 comments on commit 95d3f3f

Please sign in to comment.