Events API that auto-removes handlers on destroy - #33
Open
domchristie wants to merge 1 commit into
Open
Conversation
Author
|
Alternatively, rather than a third argument, it might be nice for view functions to just accept an object e.g.: I reckon this could be made backwards-compatible by checking the arguments type/count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I have started migrating Stimulus controllers to Defo view functions in my podcast player app, and thought it might be useful to handle events with a nice API.
This pull request aims to do just that! After the
elandpropsarguments, this adds a third: an object containing function for registering events (on), and an abort signal (signal).onEvent handlers registered with
onare automatically removed when the view is destroyed.oncan register events that target the viewel:or a descendant of
elmatching a selector:or an explicit target, useful for events on the
window/document, or for events that don’t bubble, likefocusorblur:More than one listener can be bound to the same event:
signalAn abort signal is used to remove event listeners but it's also made available for use in view functions. For example, it could be passed into a
fetchto abort it when the view is destroyed:Disclosure
This code was largely produced by Claude Code. I have reviewed every line and it seems reasonable and good quality. The pull request was written up by me. However, if this isn't your thing (for this reason, or any other ones!), that's cool!
Lastly, thanks for defo! It's a joy :)