diff --git a/src/util/evented.js b/src/util/evented.js index a1f10e40717..b596b57b66d 100644 --- a/src/util/evented.js +++ b/src/util/evented.js @@ -6,9 +6,11 @@ type Listener = (Object) => any; type Listeners = { [string]: Array }; function _addEventListener(type: string, listener: Listener, listenerList: Listeners) { - _removeEventListener(type, listener, listenerList); - listenerList[type] = listenerList[type] || []; - listenerList[type].push(listener); + const listenerExists = listenerList[type] && listenerList[type].indexOf(listener) !== -1; + if (!listenerExists) { + listenerList[type] = listenerList[type] || []; + listenerList[type].push(listener); + } } function _removeEventListener(type: string, listener: Listener, listenerList: Listeners) { diff --git a/test/unit/util/evented.test.js b/test/unit/util/evented.test.js index af79267c74c..8c2dc476de7 100644 --- a/test/unit/util/evented.test.js +++ b/test/unit/util/evented.test.js @@ -120,14 +120,16 @@ test('Evented', (t) => { }); t.test('on is idempotent', (t) => { - const evented = new Evented(); - const listener = t.spy(); - evented.on('a', listener); - evented.on('a', listener); - evented.on('a', listener); - evented.fire(new Event('a')); - t.ok(listener.calledOnce); - t.end(); + const evented = new Evented(); + const listenerA = t.spy(); + const listenerB = t.spy(); + evented.on('a', listenerA); + evented.on('a', listenerB); + evented.on('a', listenerA); + evented.fire(new Event('a')); + t.ok(listenerA.calledOnce); + t.ok(listenerA.calledBefore(listenerB)); + t.end(); }); t.test('evented parents', (t) => {