Skip to content
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

core: use immutable event results #29

Draft
wants to merge 1 commit into
base: events
Choose a base branch
from
Draft

Conversation

F0Xde
Copy link
Member

@F0Xde F0Xde commented Jul 23, 2022

Currently mutable events are implemented via subclasses of EventScope which need to use EventScopeProperty in order to make mutation impossible without access to a MutableEventScope.

This PR explores a different solution using immutable objects instead. To make this possible event listeners now only have access to an EventScope<E, R> in their context, which has been repurposed to hold both the event instance of type E and the event result of type R. Non-mutating listeners are registered via listen like before, while mutating listeners need to use listenMut. An example usage could be:

Events.myEvent.listen {
    if (result is Active) {
        doStuff(event)
    }
}

Events.myEvent.listenMut {
    if (event.someProperty) {
        doStuff(event)
        Cancelled // cancel the event
    } else result // return the current result
}

As seen above, the included Cancellable type was changed to a sealed interface with two subclasses, Active and Cancelled, instead of having a mutable boolean property to indicate cancellation.
I have also changed the generic parameter T for the type of event instance to E so it better reflects the meaning together with R, and have made immutable events use Unit as their result type which is now possible without a bound on the result type.

TODO:

  • Add utility methods for creating cancellable events without having to pass a result supplier every time

Note that all names are bikesheddable and I have not yet adjusted the documentation comments because that this is only a draft and I wanted to get some general opinions first.

Restructure event listeners to access both the event instance and
result from a context object. Mutating listeners need to return the
desired result, while non-mutating listeners return nothing.
@F0Xde F0Xde requested a review from jakobkmar July 23, 2022 21:50
@F0Xde F0Xde changed the title core: use immutable events core: use immutable event results Jul 23, 2022
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