Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix a bug in event dispatch.

If an event listener is removed during an event dispatch, it shouldn't receive
the active event. So now in addition to storing a defensive copy, we use a
wrapper object for the event handler to store an `on` boolean. This is set to
false when the listener is removed, preventing that handler from receiving the current event.
  • Loading branch information...
commit da40e9dd84737a8f7137e4318a2b5b05a2cc0573 1 parent 2df0e89
@mbostock mbostock authored
Showing with 18 additions and 15 deletions.
  1. +17 −14 src/Dispatch.js
  2. +1 −1  src/start.js
View
31 src/Dispatch.js
@@ -1,20 +1,22 @@
po.dispatch = function(that) {
- var handlers = {};
+ var types = {};
that.on = function(type, handler) {
- var array = handlers[type] || (handlers[type] = []);
- for (var i = 0; i < array.length; i++) {
- if (array[i] == handler) return that; // already registered
+ var listeners = types[type] || (types[type] = []);
+ for (var i = 0; i < listeners.length; i++) {
+ if (listeners[i].handler == handler) return that; // already registered
}
- array.push(handler);
+ listeners.push({handler: handler, on: true});
return that;
};
that.off = function(type, handler) {
- var array = handlers[type];
- if (array) for (var i = 0; i < array.length; i++) {
- if (array[i] == handler) {
- array.splice(i, 1);
+ var listeners = types[type];
+ if (listeners) for (var i = 0; i < listeners.length; i++) {
+ var l = listeners[i];
+ if (l.handler == handler) {
+ l.on = false;
+ listeners.splice(i, 1);
break;
}
}
@@ -22,11 +24,12 @@ po.dispatch = function(that) {
};
return function(event) {
- var array = handlers[event.type];
- if (!array) return;
- array = array.slice(); // defensive copy
- for (var i = 0; i < array.length; i++) {
- array[i].call(that, event);
+ var listeners = types[event.type];
+ if (!listeners) return;
+ listeners = listeners.slice(); // defensive copy
+ for (var i = 0; i < listeners.length; i++) {
+ var l = listeners[i];
+ if (l.on) l.handler.call(that, event);
}
};
};
View
2  src/start.js
@@ -2,4 +2,4 @@ if (!org) var org = {};
if (!org.polymaps) org.polymaps = {};
(function(po){
- po.version = "1.0.7"; // semver.org
+ po.version = "1.0.8"; // semver.org
Please sign in to comment.
Something went wrong with that request. Please try again.