Skip to content

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.

2 participants