Skip to content

Commit

Permalink
Adopt d3.map for d3.dispatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Feb 21, 2012
1 parent a528559 commit c6c4f38
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
15 changes: 7 additions & 8 deletions d3.v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ d3_dispatch.prototype.on = function(type, listener) {

function d3_dispatch_event(dispatch) {
var listeners = [],
listenerByName = {};
listenerByName = new d3_Map;

function event() {
var z = listeners, // defensive reference
Expand All @@ -596,22 +596,21 @@ function d3_dispatch_event(dispatch) {
}

event.on = function(name, listener) {
var l, i;
var l = listenerByName.get(name),
i;

// return the current listener, if any
if (arguments.length < 2) return (l = listenerByName[name]) && l.on;
if (arguments.length < 2) return l && l.on;

// remove the old listener, if any (with copy-on-write)
if (l = listenerByName[name]) {
if (l) {
l.on = null;
listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
delete listenerByName[name];
listenerByName.delete(name);
}

// add the new listener, if any
if (listener) {
listeners.push(listenerByName[name] = {on: listener});
}
if (listener) listeners.push(listenerByName.set(name, {on: listener}));

return dispatch;
};
Expand Down
Loading

0 comments on commit c6c4f38

Please sign in to comment.