Skip to content

Commit

Permalink
For efficiency, use cached events in data system, for property and pa…
Browse files Browse the repository at this point in the history
…th changes.
  • Loading branch information
Steven Orvell committed Nov 5, 2015
1 parent d8b78d4 commit da71dfe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
14 changes: 3 additions & 11 deletions src/lib/bind/accessors.html
Expand Up @@ -21,17 +21,9 @@
_modelApi: {

_notifyChange: function(event, value) {
var cache = Polymer.Bind._dataEventCache;
var e = cache[event];
if (e) {
cache[event] = null;
} else {
e = new CustomEvent(event,
{bubbles: false, cancelable: false, detail: {}});
}
e.detail.value = value;
this.dispatchEvent(e);
cache[event] = e;
// use a cached event here (_useCache: true) for efficiency
this.fire(event, {value: value},
{bubbles: false, cancelable: false, _useCache: true});
},

// TODO(sjmiles): removing _notifyListener from here breaks accessors.html
Expand Down
5 changes: 4 additions & 1 deletion src/standard/notify-path.html
Expand Up @@ -380,10 +380,11 @@
var rootName = this._modelForPath(path);
var dashCaseName = Polymer.CaseMap.camelToDashCase(rootName);
var eventName = dashCaseName + this._EVENT_CHANGED;
// use a cached event here (_useCache: true) for efficiency
this.fire(eventName, {
path: path,
value: value
}, {bubbles: false});
}, {bubbles: false, _useCache: true});
},

_modelForPath: function(path) {
Expand Down Expand Up @@ -602,6 +603,8 @@
prepareModelNotifyPath: function(model) {
this.mixin(model, {
fire: Polymer.Base.fire,
_getEvent: Polymer.Base._getEvent,
__eventCache: Polymer.Base.__eventCache,
notifyPath: Polymer.Base.notifyPath,
_get: Polymer.Base._get,
_EVENT_CHANGED: Polymer.Base._EVENT_CHANGED,
Expand Down
32 changes: 26 additions & 6 deletions src/standard/utils.html
Expand Up @@ -198,6 +198,7 @@
});
},


/**
* Dispatches a custom event with an optional detail value.
*
Expand All @@ -214,18 +215,37 @@
fire: function(type, detail, options) {
options = options || Polymer.nob;
var node = options.node || this;
var detail = (detail === null || detail === undefined) ? Polymer.nob : detail;
var detail = (detail === null || detail === undefined) ? {} : detail;
var bubbles = options.bubbles === undefined ? true : options.bubbles;
var cancelable = Boolean(options.cancelable);
var event = new CustomEvent(type, {
bubbles: Boolean(bubbles),
cancelable: cancelable,
detail: detail
});
var useCache = options._useCache;
var event = this._getEvent(type, bubbles, cancelable, useCache);
event.detail = detail;
if (useCache) {
this.__eventCache[type] = null;
}
node.dispatchEvent(event);
if (useCache) {
this.__eventCache[type] = event;
}
return event;
},

__eventCache: {},

_getEvent: function(type, bubbles, cancelable, useCache) {
var event = useCache && this.__eventCache[type];
if (!event || ((event.bubbles != bubbles) ||
(event.cancelable != cancelable))) {
event = new Event(type, {
bubbles: Boolean(bubbles),
cancelable: cancelable
});
}
return event;
},


/**
* Runs a callback function asyncronously.
*
Expand Down

0 comments on commit da71dfe

Please sign in to comment.