[Feature Request] React: Provide event name in useEcho callback #432
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the
useEcho
hook allows subscribing to multiple events with a single callback:While functional, this makes it impossible to determine which specific event was triggered, especially when handling multiple events that share similar payload structures.
This PR enhances the useEcho hook by passing the event name as a second argument to the callback:
Benefits to end users
Enables precise event handling when listening to multiple events.
Reduces the need for workarounds such as including the event name in the payload manually.
Backward compatibility
The change is backward-compatible: existing usage with a single payload parameter remains functional.
Additional data (event) is non-breaking and optional for the callback.
NOTE: I had to adapt the tests since the registered function is now anonymous. Verifying that the original mockCallback was passed directly no longer works as before. I'm not entirely sure how you'd prefer this to be tested—if you have a specific approach in mind, I'm happy to implement it accordingly. I can also add support for this feature in Vue if you think this is something you would accept as a feature.