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

Replace Observable and ModelChangeListener with EventBus #3749

Conversation

kwvanderlinde
Copy link
Collaborator

@kwvanderlinde kwvanderlinde commented Nov 24, 2022

Identify the Bug or Feature request

Relates to #1827

Description of the Change

This change moves the remaining from legacy event models to EventBus. The two legacy event models are:

  • Observable/Observer
  • BaseModel/ModelChangeListener

One use of Observable/Observer was removed as being unnecessary (ChatTyperObserver), while the remaining uses are now covered by these new events:

  • ChatMessageAdded
  • PlayerConnected
  • PlayerDisconnected
  • ServerStopped

With that gone, our custom ObservableList or the related PlayerListModel could be deleted in favour of plain lists.

The remainder and bulk of the change is in removing ModelChangeListener, which amounts to replacing each Zone.Event tag with a corresponding event class. There were some existing event classes (TokensAddedEvent, TokensRemovedEvent, TokensChangedEvent) that TokenEventBusBridge translated to, but these have been replaced with new ones that are easier to build and consume, and which are consistent with how the other events works.

With all the model events replaced, we could remove these types:

  • ModelChangeListener
  • BaseModel
  • TokenEventBusBridge

Possible Drawbacks

If done correctly, should be nothing. It just requires a bunch of testing

Documentation Notes

N/A (refactoring)

Release Notes

N/A


This change is Reviewable

kwvanderlinde and others added 4 commits November 23, 2022 16:09
Instead of directly listening to `ObservableList`, we instead send out events for subscribers to consume.

- ChatMessageAdded
- PlayerConnected
- PlayerDisconnected
- ServerStopped

This also means `MapTool` no longer needs to maintain a list of messages since the panel can directly listen for and
maintain the list.

`ObservableList` is no longer necessary and has been replaced with `List` where used.
The use of the `Observable`/`Observer` pattern was unnecessary here since the objects are created together anyways, and
the observer was very simple.
With this change, subscribers no longer need to subscribe to a particular model object but can generically listen for
event types that come from anywhere. In particular, this frees several components from having to ensure that their
current zone is the one they are actually listening to. For any components that relied on a particular zone, these
include a check that the event's zone matches the one in the component.

For the most part, this change does not change behaviour beyond handling events in the new way. There are a few
exceptions where components unconditionally responded to every model event whether or not it was actually
applicable. These components are now more constrained in what they respond to:
- `UndoPerZone` no longer subscribes to anything as it did nothing but log all of a zone's events.
- `DrawPanelTreeModel` only subscribes to the `DrawableChanged` event.
- `TokenPanelTreeModel` only subscribes to the various `Token*` events.
- `ZoneRenderer` only subscribes to the events that it already handled explicitly. But it no longer calls
  `updateTokenTree()` and `repaintDebouncer.dispatch()` for all the other possible events.

All the events that were fired this way were in the `Zone.Event` enum (which no longer exists). Here is the mapping from
old to new:
- `Zone.Event.TOKEN_ADDED`: `TokenAdded`
- `Zone.Event.TOKEN_REMOVED`: `TokenRemoved`
- `Zone.Event.TOKEN_CHANGED`: `TokenChanged`
- `Zone.Event.GRID_CHANGED`: `GridChanged`
- `Zone.Event.DRAWABLE_ADDED`: `DrawableAdded`
- `Zone.Event.DRAWABLE_REMOVED`: `DrawableRemoved`
- `Zone.Event.FOG_CHANGED`: `FogChanged`
- `Zone.Event.LABEL_ADDED`: `LabelAdded`
- `Zone.Event.LABEL_REMOVED`: `LabelRemoved`
- `Zone.Event.LABEL_CHANGED`: `LabelChanged`
- `Zone.Event.TOPOLOGY_CHANGED`: `TopologyChanged`
- `Zone.Event.INITIATIVE_LIST_CHANGED`: `InitiativeListChanged`
- `Zone.Event.BOARD_CHANGED`: `BoardChanged`
- `Zone.Event.TOKEN_EDITED`: `TokenEdited`
- `Zone.Event.TOKEN_MACRO_CHANGED`: `TokenMacroChanged`
- `Zone.Event.TOKEN_PANEL_CHANGED`: `TokenPanelChanged`

There were some existing events for EventBus that have been replaced with easier-to-use alternatives that are consistent
with the other events:
- `TokensAddedEvent`: `TokensAdded`
- `TokensRemovedEvent`: `TokensRemoved`
- `TokensChangedEvent`: `TokensChanged`

In conjunction with that, the `TokenEventBusBridge` is no more. There was some important functionality in there to post
token events in response to corresponding zone events, but that is now handled by posting the token events at the some
point that the zone events are posted. This functionality was also added to `ServeMessageHandler` for good measure.

Finally, since `BaseModel` only offered functionality for model events, it has been removed and existing uses (`Zone`,
`Token`) no longer have a base class. Of course, `ModelChangeListener` is also gone.
Copy link
Contributor

@Phergus Phergus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 52 of 52 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @kwvanderlinde)

@Phergus
Copy link
Contributor

Phergus commented Nov 25, 2022

@kwvanderlinde need a spotless check

@kwvanderlinde
Copy link
Collaborator Author

Formatting is fine (or so spotless says on my end). The JAR download just failed on Github:

Downloading Google Java Format
Downloading executable to /home/runner/google-java-format.jar
/opt/hostedtoolcache/jdk/17.0.5/x64/bin/java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED -jar /home/runner/google-java-format.jar --version
Error: Invalid or corrupt jarfile /home/runner/google-java-format.jar

Reran and everything checked out.

@Phergus Phergus merged commit d02f3ab into RPTools:develop Nov 25, 2022
@kwvanderlinde kwvanderlinde deleted the refactor/1827-replace-observables-and-model-listeners branch November 25, 2022 20:26
@cwisniew cwisniew added the feature Adding functionality that adds value label Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Adding functionality that adds value
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

3 participants