Skip to content

Commit

Permalink
Separate public & private get, flip conditions, add notifyPath API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Oct 9, 2015
1 parent e59dbef commit 97503ec
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/lib/bind/effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

_annotationEffect: function(source, value, effect) {
if (source != effect.value) {
value = this.get(effect.value);
value = this._get(effect.value);
this.__data__[effect.value] = value;
}
var calc = effect.negate ? !value : value;
Expand Down Expand Up @@ -115,7 +115,7 @@
if (arg.literal) {
v = arg.value;
} else if (arg.structured) {
v = Polymer.Base.get(name, model);
v = Polymer.Base._get(name, model);
} else {
v = model[name];
}
Expand Down
2 changes: 1 addition & 1 deletion src/standard/configure.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
// seeding configuration only
if (node._configValue) {
var value = (p === x.effect.value) ? config[p] :
this.get(x.effect.value, config);
this._get(x.effect.value, config);
node._configValue(x.effect.name, value);
}
}
Expand Down
71 changes: 43 additions & 28 deletions src/standard/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
notifyPath: function(path, value, fromAbove) {
// Convert any array indices to keys before notifying path
var info = {};
path = this.get(path, this, info);
path = this._get(path, this, info);
// Notify change to key-based path
this._notifyPath(info.path, value, fromAbove);
},
Expand Down Expand Up @@ -181,22 +181,22 @@
// item in collection associated with key for that item
if (array) {
var coll = Polymer.Collection.get(array);
if (parseInt(last) == last) {
// Dereference index & lookup collection key
var old = prop[last];
var key = coll.getKey(old);
// Translate array indices to collection keys for path notificaiton
parts[i] = key;
// Replace item associated with key in collection
coll.setItem(key, value);
} else if (last[0] == '#') {
if (last[0] == '#') {
// Part was key; lookup item in collection
var key = last;
var old = coll.getItem(key);
// Update last part from key to index: O(n) lookup unavoidable
last = array.indexOf(old);
// Replace item associated with key in collection
coll.setItem(key, value);
} else if (parseInt(last) == last) {
// Dereference index & lookup collection key
var old = prop[last];
var key = coll.getKey(old);
// Translate array indices to collection keys for path notificaiton
parts[i] = key;
// Replace item associated with key in collection
coll.setItem(key, value);
}
}
// Set value to object at end of path
Expand Down Expand Up @@ -227,12 +227,17 @@
* indices, the index may be used as a dotted part directly
* (e.g. `users.12.name` or `['users', 12, 'name']`).
* @param {Object=} root Root object from which the path is evaluated.
* @param {Object=} info Info object; if supplied, a `path` property
* will be added containing path with array indices converted to keys
* @return {*} Value at the path, or `undefined` if any part of the path
* is undefined.
*/
get: function(path, root, info) {
get: function(path, root) {
return this._get(path, root);
},

// If `info` object is supplied, a `path` property will be added to it
// containing the path with array indices converted to keys, for use
// by the private _notifyPath / _notifySplice implementations
_get: function(path, root, info) {
var prop = root || this;
var parts = this._getPathParts(path);
var array;
Expand All @@ -246,7 +251,7 @@
// Part was key; lookup item in collection
prop = Polymer.Collection.get(array).getItem(part);
} else {
// Part was index; simple dereference
// Get item from simple property dereference
prop = prop[part];
if (info && array && (parseInt(part) == part)) {
// Translate array indices to collection keys for path notificaiton
Expand Down Expand Up @@ -333,7 +338,7 @@
this._boundPaths = this._boundPaths || {};
if (from) {
this._boundPaths[to] = from;
// this.set(to, this.get(from));
// this.set(to, this._get(from));
} else {
this.unlinkPaths(to);
// this.set(to, from);
Expand Down Expand Up @@ -417,11 +422,12 @@
*/
notifySplices: function(path, splices) {
var info = {};
var array = this.get(path, this, info);
var array = this._get(path, this, info);
// Notify change to key-based path
this._notifySplices(array, info.path, splices);
},

// Note: this implemetation only accepts key-based array paths
_notifySplices: function(array, path, splices) {
var change = {
keySplices: Polymer.Collection.applySplices(array, splices),
Expand All @@ -431,7 +437,8 @@
Object.defineProperty(array, 'splices',
{configurable: true, writable: true});
}
this.set(path + '.splices', change);
array.splices = change;
this._notifyPath(path + '.splices', change);
this._notifyPath(path + '.length', array.length);
// Null here to allow potentially large splice records to be GC'ed
change.keySplices = null;
Expand Down Expand Up @@ -463,12 +470,13 @@
* @return {number} New length of the array.
*/
push: function(path) {
var array = this.get(path);
var info = {};
var array = this._get(path, this, info);
var args = Array.prototype.slice.call(arguments, 1);
var len = array.length;
var ret = array.push.apply(array, args);
if (args.length) {
this._notifySplice(array, path, len, args.length, []);
this._notifySplice(array, info.path, len, args.length, []);
}
return ret;
},
Expand All @@ -487,12 +495,13 @@
* @return {any} Item that was removed.
*/
pop: function(path) {
var array = this.get(path);
var info = {};
var array = this._get(path, this, info);
var hadLength = Boolean(array.length);
var args = Array.prototype.slice.call(arguments, 1);
var ret = array.pop.apply(array, args);
if (hadLength) {
this._notifySplice(array, path, array.length, 0, [ret]);
this._notifySplice(array, info.path, array.length, 0, [ret]);
}
return ret;
},
Expand All @@ -515,7 +524,8 @@
* @return {Array} Array of removed items.
*/
splice: function(path, start, deleteCount) {
var array = this.get(path);
var info = {};
var array = this._get(path, this, info);
// Normalize fancy native splice handling of crazy start values
if (start < 0) {
start = array.length - Math.floor(-start);
Expand All @@ -529,7 +539,7 @@
var ret = array.splice.apply(array, args);
var addedCount = Math.max(args.length - 2, 0);
if (addedCount || ret.length) {
this._notifySplice(array, path, start, addedCount, ret);
this._notifySplice(array, info.path, start, addedCount, ret);
}
return ret;
},
Expand All @@ -548,12 +558,13 @@
* @return {any} Item that was removed.
*/
shift: function(path) {
var array = this.get(path);
var info = {};
var array = this._get(path, this, info);
var hadLength = Boolean(array.length);
var args = Array.prototype.slice.call(arguments, 1);
var ret = array.shift.apply(array, args);
if (hadLength) {
this._notifySplice(array, path, 0, 0, [ret]);
this._notifySplice(array, info.path, 0, 0, [ret]);
}
return ret;
},
Expand All @@ -573,11 +584,12 @@
* @return {number} New length of the array.
*/
unshift: function(path) {
var array = this.get(path);
var info = {};
var array = this._get(path, this, info);
var args = Array.prototype.slice.call(arguments, 1);
var ret = array.unshift.apply(array, args);
if (args.length) {
this._notifySplice(array, path, 0, args.length, []);
this._notifySplice(array, info.path, 0, args.length, []);
}
return ret;
},
Expand All @@ -590,16 +602,19 @@
this.mixin(model, {
fire: Polymer.Base.fire,
notifyPath: Polymer.Base.notifyPath,
_get: Polymer.Base._get,
_EVENT_CHANGED: Polymer.Base._EVENT_CHANGED,
_notifyPath: Polymer.Base._notifyPath,
_notifyPathUp: Polymer.Base._notifyPathUp,
_pathEffector: Polymer.Base._pathEffector,
_annotationPathEffect: Polymer.Base._annotationPathEffect,
_complexObserverPathEffect: Polymer.Base._complexObserverPathEffect,
_annotatedComputationPathEffect: Polymer.Base._annotatedComputationPathEffect,
_computePathEffect: Polymer.Base._computePathEffect,
_modelForPath: Polymer.Base._modelForPath,
_pathMatchesEffect: Polymer.Base._pathMatchesEffect,
_notifyBoundPaths: Polymer.Base._notifyBoundPaths
_notifyBoundPaths: Polymer.Base._notifyBoundPaths,
_getPathParts: Polymer.Base._getPathParts
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/standard/utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
return path.splice(index, 1);
}
} else {
var arr = this.get(path);
var arr = this._get(path);
index = arr.indexOf(item);
if (index >= 0) {
return this.splice(path, index, 1);
Expand Down

0 comments on commit 97503ec

Please sign in to comment.