-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
listenerCount()
and Emittery.listenerAdded
event not working as expected
#64
Comments
// @Richienb |
@sindresorhus The problem involves the asynchronous nature of promises. They are only executed during idle time which is why sindresorhus/make-synchronous#3 is impossible (since synchronously sleeping is not counted as idle). However, before idle time is reached, all of the synchronous Lines 256 to 268 in 1afed6a
emitter.on(Emitter.listenerAdded) . As expected, these proof-of-concept changes within .emit() fix the problem at hand:
- await resolvedPromise;
- await Promise.all([
- ...staticListeners.map(async listener => {
- if (listeners.has(listener)) {
- return listener(eventData);
- }
- }),
- ...staticAnyListeners.map(async listener => {
- if (anyListeners.has(listener)) {
- return listener(eventName, eventData);
- }
- })
- ]);
+ staticListeners.map(listener => {
+ if (listeners.has(listener)) {
+ return listener(eventData);
+ }
+ });
+ staticAnyListeners.map(listener => {
+ if (anyListeners.has(listener)) {
+ return listener(eventName, eventData);
+ }
+ }); This means that the scope of this problem actually extends to any code using const Emittery = require('.');
const emitter = new Emittery();
emitter.on('unicorn', () => {
console.count(`Event received`);
});
emitter.emit('unicorn');
console.count('Event sent');
emitter.emit('unicorn');
console.count('Event sent'); The above code outputs:
Instead of:
|
After some additional investigation, it appears that the only way to fix this problem is to wait for Line 207 in 1afed6a
and here: Line 223 in 1afed6a
However, this will require us to make |
Is there really no other way? |
🤷♂️ |
I guess the only other way is to make |
If I have the following snippet
Then I get printed:
While I would expect:
The text was updated successfully, but these errors were encountered: