Skip to content

emmiter implementation#321

Open
adonch wants to merge 3 commits into
mate-academy:masterfrom
adonch:develop
Open

emmiter implementation#321
adonch wants to merge 3 commits into
mate-academy:masterfrom
adonch:develop

Conversation

@adonch

@adonch adonch commented Mar 28, 2026

Copy link
Copy Markdown

No description provided.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job on building the MyEventEmitter class! The structure is clean and most of the methods are implemented correctly. However, a couple of adjustments are needed to fully meet the project requirements.

I am requesting changes for the following reasons:

  1. removeAllListeners is incomplete: The current implementation only handles removing listeners for a specific event. According to the requirements, if no event is provided, it should remove all listeners for all events. Please add logic to handle this case.
  2. Incorrect argument handling in once methods: The wrapper functions for once and prependOnceListener only pass the first argument to the original listener. To ensure all data from emit is correctly forwarded, you should use rest parameters (...args) in the wrapper function and spread syntax (...args) when calling the listener.

Once these two issues are addressed, your solution will be complete. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/MyEventEmitter.js Outdated
Comment on lines +11 to +12
const onceListener = (data) => {
listener(data);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation of onceListener only captures the first argument passed from emit. To correctly forward all arguments, you should use rest parameters (...args) for the function definition and spread syntax (...args) when calling the original listener.

Comment thread src/MyEventEmitter.js Outdated
Comment on lines +34 to +35
const onceListener = (data) => {
listener(data);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the once method, this onceListener only passes the first argument to the original listener. It should be updated to handle multiple arguments using rest and spread syntax to ensure all data from emit is passed through.

Comment thread src/MyEventEmitter.js Outdated
Comment on lines +41 to +42
removeAllListeners(event) {
this.listeners = this.listeners.filter((l) => l.event !== event);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirement specifies that if no event is provided, all listeners for all events should be removed. This implementation only handles the case where an event is specified. Consider adding a condition to check if event is undefined, and if so, clear the entire listeners array.

- Modify `once` and `prependOnceListener` methods to accept multiple arguments.
- Ensure `removeAllListeners` correctly filters listeners based on the event.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Excellent progress on this task, and thank you for addressing the previous feedback! I am requesting a couple more adjustments to align your implementation perfectly with Node.js's EventEmitter behavior. First, the off method should only remove the first matching listener, not all of them; using findIndex and splice is a good way to handle this. Second, the emit method can behave unexpectedly when a listener is removed during the loop (like with once). Iterating over a copy of the listeners array, created with this.listeners.slice(), will solve this issue. After these fixes, your solution will be solid!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/MyEventEmitter.js Outdated
Comment on lines +19 to +21
this.listeners = this.listeners.filter(
(l) => l.event !== event || l.listener !== listener,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter method removes all elements that satisfy the condition. However, the standard EventEmitter.off() method should only remove the first matching listener if multiple identical listeners are registered for the same event.

Consider using a method like findIndex to locate the first match and then removing only that single element from the array, for example with splice.

Comment thread src/MyEventEmitter.js Outdated
);
}
emit(event, ...data) {
this.listeners.forEach((l) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a listener is removed from this.listeners during this forEach loop (e.g., a once listener calling off), it can cause unexpected behavior, such as skipping the next listener in the original array if the array is modified in place.

To ensure all listeners registered at the time of emit are called, it's safer to iterate over a copy of the listeners array. You can create a shallow copy by calling this.listeners.slice().forEach(...).

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Excellent work on this iteration! You've successfully addressed all the previous feedback, resulting in a robust and correct MyEventEmitter class. Your fixes for the off method to only remove the first matching listener and for the emit method to safely iterate over a copy of the listeners are perfect. The code now meets all task requirements, and I'm happy to approve your solution. Well done!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants