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

Refactored messages Array on client to use a Map #341

Merged
merged 4 commits into from
Feb 23, 2025

Conversation

connor-kress
Copy link
Contributor

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 with expo-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.

@h1divp
Copy link
Collaborator

h1divp commented Feb 23, 2025

This is a great change. I have started to research how to make it so we won't need to recreate the map on every render, and I think we should look into using a Reducer which will also make implementing reactions nicer. I will make a subsequent issue for this.

This post also seems like a good starting point for figuring out this problem.

Thank you for your work.

Copy link
Collaborator

@h1divp h1divp left a comment

Choose a reason for hiding this comment

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

tested, works, lgtm

@h1divp h1divp merged commit 5955203 into ufosc:main Feb 23, 2025
1 of 3 checks passed
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