For efficiency, use cached events in data system, for property and path changes.

This commit is contained in:
Steven Orvell 2015-11-05 13:08:33 -08:00
parent d8b78d4a7b
commit da71dfeb54
3 changed files with 33 additions and 18 deletions

View File

@ -21,17 +21,9 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
_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

View File

@ -380,10 +380,11 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
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) {
@ -602,6 +603,8 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
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,

View File

@ -198,6 +198,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
});
},
/**
* Dispatches a custom event with an optional detail value.
*
@ -214,18 +215,37 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
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.
*