Prevent the 'Message' components from unexpectedly re-rendering while receiving tokens #1051
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.
While receiving tokens from the server, the 'updateMessageContentById' function will create new objects for all the messages having 'steps' member, which will modify the references values of the messages and cause all the 'Message' components based on those messages to rerender. If we have a long conversations, like 100 rounds, this may cause serious performance problem.
This patch tried to fix it by checking if the messages really changed and only creating new objects for changed ones.
Meanwhile, given that only one message need to be updated and for most of the cases the last one, this patch also make some performance optimization by iterating from the last element and break the loop when finding matched one.