Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default function mitt(all: EventHandlerMap) {
* @memberOf mitt
*/
off(type: string, handler: EventHandler) {
if (all[type]) {
if (!type) {
all = Object.create(null);
} else if (all[type]) {
all[type].splice(all[type].indexOf(handler) >>> 0, 1);
}
},
Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ describe('mitt#', () => {
expect(events).to.have.property('foo').that.is.empty;
});

it('should remove all handlers when invoked without type', () => {
const onFoo = spy();
const onBar = spy();

inst.on('foo', onFoo);
inst.on('bar', onBar);

inst.emit('foo', 1);
inst.emit('foo', 2);
inst.emit('bar', 3);

inst.off();

inst.emit('foo', 4);
inst.emit('foo', 5);
inst.emit('bar', 6);
inst.emit('bar', 7);

expect(onFoo).to.have.callCount(2);
expect(onBar).to.have.callCount(1);
});

it('should NOT normalize case', () => {
let foo = () => {};
inst.on('FOO', foo);
Expand Down