Skip to content

Commit

Permalink
Make evented#addEventListener idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhamley committed May 17, 2018
1 parent 9d9ec49 commit a5da36c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/util/evented.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Listener = (Object) => any;
type Listeners = { [string]: Array<Listener> };

function _addEventListener(type: string, listener: Listener, listenerList: Listeners) {
_removeEventListener(type, listener, listenerList);
listenerList[type] = listenerList[type] || [];
listenerList[type].push(listener);
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/util/evented.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ test('Evented', (t) => {
t.end();
});

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();
});

t.test('evented parents', (t) => {

t.test('adds parents with "setEventedParent"', (t) => {
Expand Down

0 comments on commit a5da36c

Please sign in to comment.