Skip to content

Better event callbacks #3

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

Merged
merged 6 commits into from
Aug 28, 2022
Merged

Better event callbacks #3

merged 6 commits into from
Aug 28, 2022

Conversation

LK-Simon
Copy link
Contributor

Implemented a number of enhancements for Event Listeners and Callbacks

- Added a new `@EventMethod` Property Wrapper to enable in-line, automatically-registering Event-invoked Callback Methods on any class implementing `EventListening` (for automatic registration)
- Any Class implementing `EventListening` can now invoke `registerListeners()` at the end of its `init` method (or anywhere post-init you wish to do so, anyway) to automatically register all `@EventMethod`-decorated Event Callbacks.
- Added a version of `@EventMethod` specifically for use in `EventThread` types. You don't need to invoke `registerListeners()` for your `EventThread` types, because the base `init` will already do this for you.
- `EventThread` now supports multiple Callbacks for the same `Eventable` type. Invoking `addEventCallback` will now return a `UUID` containing the unique `token` of each Callback. This can be used with `removeEventCallback` to unregister the Callback at any time.
We cannot unregister or modify an `@EventMethod` once it is registered... so the code to try this has been removed.
To reflect the removed (impossible) Unregister code committed previously.
UUID tokens for `EventListener.addListener` have been replaced by a new `EventListenerHandling` protocol describing the `EventListenerHandler` class. This can be used to call `.remove()` and unregister the `Listener`.

References are `weak`, and perform `nil`-checks as appropriate, so calling `.remove()` against an `EventListenerHandler` for an `EventListenable` that has been garbage collected won't raise any errors.
Documentation for the `UUID` *token* has been superseded by the new `EventListenerHandling` solution.
I've implemented `EventCallbackContainer` in `EventThread` for internal Callback management. This matches the `EventListener` solution (which also has a bug fix now)
@LK-Simon LK-Simon merged commit c1600f1 into main Aug 28, 2022
@LK-Simon LK-Simon deleted the better-event-callbacks branch August 28, 2022 16:51
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.

1 participant