Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mitt.off('*') doesn't work #150

Closed
feliperaul opened this issue Oct 4, 2021 · 2 comments
Closed

mitt.off('*') doesn't work #150

feliperaul opened this issue Oct 4, 2021 · 2 comments

Comments

@feliperaul
Copy link

The docs state that the .off method accepts a star string to remove all event handlers:

type (string | symbol) Type of event to unregister handler from, or '*'

However, in my test, this does not work, and I can't understand how it could, since the source code is very simple and Map.get('*') will not return all handlers.

Is this a never-implemented feature and the docs are wrong?

@feliperaul
Copy link
Author

feliperaul commented Oct 4, 2021

Just to be extra clear, aren't we missing something like this in the .off method?

if (!type || type === '*') { all.clear(); return }

@developit
Copy link
Owner

developit commented Jan 28, 2022

The docs are unclear here: .off('*') doesn't remove all listeners, it removes all wildcard (*) listeners:

const events = mitt();

// add two wildcard listeners:
events.on('*', (type, e) => console.log(type, e));
let count = 0;
function countEvents(type) { count++; }
events.on('*', countEvents);

// Remove a specific wildcard listener:
events.on('*', countEvents);

// remove all wildcard listeners:
events.off('*');

Mitt does not provide a way to remove all listeners by default. To add it, you can pass in a custom Map and clear it when necessary:

const all = new Map();
const events = mitt(all);

events.on('foo', console.log);
events.on('bar', console.info);

all.clear(); // remove all event listeners

developit added a commit that referenced this issue Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants