Skip to content

Commit

Permalink
Fix a bug in event dispatch.
Browse files Browse the repository at this point in the history
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
Mike Bostock committed Jul 27, 2010
1 parent 2df0e89 commit da40e9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
31 changes: 17 additions & 14 deletions src/Dispatch.js
@@ -1,32 +1,35 @@
po.dispatch = function(that) { po.dispatch = function(that) {
var handlers = {}; var types = {};


that.on = function(type, handler) { that.on = function(type, handler) {
var array = handlers[type] || (handlers[type] = []); var listeners = types[type] || (types[type] = []);
for (var i = 0; i < array.length; i++) { for (var i = 0; i < listeners.length; i++) {
if (array[i] == handler) return that; // already registered if (listeners[i].handler == handler) return that; // already registered
} }
array.push(handler); listeners.push({handler: handler, on: true});
return that; return that;
}; };


that.off = function(type, handler) { that.off = function(type, handler) {
var array = handlers[type]; var listeners = types[type];
if (array) for (var i = 0; i < array.length; i++) { if (listeners) for (var i = 0; i < listeners.length; i++) {
if (array[i] == handler) { var l = listeners[i];
array.splice(i, 1); if (l.handler == handler) {
l.on = false;
listeners.splice(i, 1);
break; break;
} }
} }
return that; return that;
}; };


return function(event) { return function(event) {
var array = handlers[event.type]; var listeners = types[event.type];
if (!array) return; if (!listeners) return;
array = array.slice(); // defensive copy listeners = listeners.slice(); // defensive copy
for (var i = 0; i < array.length; i++) { for (var i = 0; i < listeners.length; i++) {
array[i].call(that, event); var l = listeners[i];
if (l.on) l.handler.call(that, event);
} }
}; };
}; };
2 changes: 1 addition & 1 deletion src/start.js
Expand Up @@ -2,4 +2,4 @@ if (!org) var org = {};
if (!org.polymaps) org.polymaps = {}; if (!org.polymaps) org.polymaps = {};
(function(po){ (function(po){


po.version = "1.0.7"; // semver.org po.version = "1.0.8"; // semver.org

0 comments on commit da40e9d

Please sign in to comment.