Refactored messages Array on client to use a Map #341
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For issue #232
This PR refactors the messages state on the client (in
<ChatScreen />
) from an array (Message[]
) to a Map with message IDs as keys (Map<string, Message>
). To do this, I also had to add a message ID attribute using a UUID generated withexpo-crypto
. The messages map is passed to<MessageChannel />
as a map, which is then converted/sorted inside<MessageChannel />
on render.For future improvements, it may be a good idea to persist a sorted array on the client in addition to the map, so that it does not have to be created and sorted on every rerender. This is not a big problem for now but may introduce some performance costs when scaling to large and quickly updating chats. A hybrid approach may allow for the best of both data structures.
It should also be noted that the current broken state of socket server connection management made it difficult to test this refactor as it could not be run altogether. Instead, I injected some mock data (I left this commented out for others to use) at the broken points as a temporary solution.