Closed brett-smith closed 2 years ago
Sounds good to me. Feel free to provide a PR for this feature.
Return the handler reference in addSigHandler()
Provide a new removeSigHandler() than only takes the handler instance.
While that works, another approach is to return a Closeable implementation token. Such as:
final var token = connection.addSigHandler( ... );
token.close();
It's a bit more flexible because the code that performs the closing doesn't need a handle to the connection
, only the token.
Having a Closeable
of some sort sounds good to me too. I'll get a PR together.
The change has been merged, can we close this issue?
Does any library usage documentation/example need to be updated as part of the PR?
I don't think so. But if you want to add some more samples or documentation, that would be great
This is not a bug, just a request for comments on possible API improvements to adding and removing signal handlers.
For example, say I have a GUI app. In this app, one of the windows I open will add a set of signal handlers when the window opens, and remove them when the windows. The app has various different types of windows that might listen, or stop listening to, various signals.
The code to achieve this a bit clunky, especially when used with lambda syntax, and may require keeping references to multiple objects to be able to remove signals.
This is not terrible, but it can get a bit excessive when you have many signals.
At the very least, it may be useful to do 2 things to reduce the typing a bit.
addSigHandler()
removeSigHandler()
than only takes the handler instance.So the above code becomes.