comment the Events code better
This commit is contained in:
parent
dcb6f74ef5
commit
d0051500c3
@ -9,6 +9,8 @@ L.Evented = L.Class.extend({
|
|||||||
// types can be a map of types/handlers
|
// types can be a map of types/handlers
|
||||||
if (typeof types === 'object') {
|
if (typeof types === 'object') {
|
||||||
for (var type in types) {
|
for (var type in types) {
|
||||||
|
// we don't process space-separated events here for performance;
|
||||||
|
// it's a hot path since Layer uses the on(obj) syntax
|
||||||
this._on(type, types[type], fn);
|
this._on(type, types[type], fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,8 +55,8 @@ L.Evented = L.Class.extend({
|
|||||||
contextId = context && context !== this && L.stamp(context);
|
contextId = context && context !== this && L.stamp(context);
|
||||||
|
|
||||||
if (contextId) {
|
if (contextId) {
|
||||||
// store listeners of a particular context in a separate hash (if it has an id)
|
// store listeners with custom context in a separate hash (if it has an id);
|
||||||
// gives a major performance boost when removing thousands of map layers
|
// gives a major performance boost when firing and removing events (e.g. on map object)
|
||||||
|
|
||||||
var indexKey = type + '_idx',
|
var indexKey = type + '_idx',
|
||||||
indexLenKey = type + '_len',
|
indexLenKey = type + '_len',
|
||||||
@ -69,6 +71,9 @@ L.Evented = L.Class.extend({
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
// individual layers mostly use "this" for context and don't fire listeners too often
|
||||||
|
// so simple array makes the memory footprint better while not degrading performance
|
||||||
|
|
||||||
events[type] = events[type] || [];
|
events[type] = events[type] || [];
|
||||||
events[type].push({fn: fn});
|
events[type].push({fn: fn});
|
||||||
}
|
}
|
||||||
@ -97,8 +102,6 @@ L.Evented = L.Class.extend({
|
|||||||
listeners = events[indexKey];
|
listeners = events[indexKey];
|
||||||
|
|
||||||
if (listeners && listeners[id]) {
|
if (listeners && listeners[id]) {
|
||||||
// set the old action to a no-op, because it is possible
|
|
||||||
// that the listener is being iterated over as part of a dispatch
|
|
||||||
listener = listeners[id];
|
listener = listeners[id];
|
||||||
delete listeners[id];
|
delete listeners[id];
|
||||||
events[indexLenKey]--;
|
events[indexLenKey]--;
|
||||||
@ -148,6 +151,7 @@ L.Evented = L.Class.extend({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (propagate) {
|
if (propagate) {
|
||||||
|
// propagate the event to parents (set with addEventParent)
|
||||||
this._propagateEvent(event);
|
this._propagateEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,6 +164,7 @@ L.Evented = L.Class.extend({
|
|||||||
if (events && (events[type] || events[type + '_len'])) { return true; }
|
if (events && (events[type] || events[type + '_len'])) { return true; }
|
||||||
|
|
||||||
if (propagate) {
|
if (propagate) {
|
||||||
|
// also check parents for listeners if event propagates
|
||||||
for (var id in this._eventParents) {
|
for (var id in this._eventParents) {
|
||||||
if (this._eventParents[id].listens(type)) { return true; }
|
if (this._eventParents[id].listens(type)) { return true; }
|
||||||
}
|
}
|
||||||
@ -182,11 +187,13 @@ L.Evented = L.Class.extend({
|
|||||||
.off(types, handler, context);
|
.off(types, handler, context);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
// add a listener that's executed once and removed after that
|
||||||
return this
|
return this
|
||||||
.on(types, fn, context)
|
.on(types, fn, context)
|
||||||
.on(types, handler, context);
|
.on(types, handler, context);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// adds a parent to propagate events to (when you fire with true as a 3rd argument)
|
||||||
addEventParent: function (obj) {
|
addEventParent: function (obj) {
|
||||||
this._eventParents = this._eventParents || {};
|
this._eventParents = this._eventParents || {};
|
||||||
this._eventParents[L.stamp(obj)] = obj;
|
this._eventParents[L.stamp(obj)] = obj;
|
||||||
@ -209,7 +216,7 @@ L.Evented = L.Class.extend({
|
|||||||
|
|
||||||
var proto = L.Evented.prototype;
|
var proto = L.Evented.prototype;
|
||||||
|
|
||||||
// aliases
|
// aliases; we should ditch those eventually
|
||||||
proto.addEventListener = proto.on;
|
proto.addEventListener = proto.on;
|
||||||
proto.removeEventListener = proto.clearAllEventListeners = proto.off;
|
proto.removeEventListener = proto.clearAllEventListeners = proto.off;
|
||||||
proto.addOneTimeEventListener = proto.once;
|
proto.addOneTimeEventListener = proto.once;
|
||||||
|
Loading…
Reference in New Issue
Block a user