Skip to content

Commit 20f6eb5

Browse files
Merge pull request #243 from beejunk/issue-233
fix(GameScreen): Add more robust diff-ing of chat sections
2 parents f9aa7d1 + b6acc40 commit 20f6eb5

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/ui/game/GameScreen.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,36 @@ type State = {
4747
chatSections: Array<GameChatSection>,
4848
};
4949

50+
/**
51+
* Helper function that compares the current chat sections with the previous
52+
* chat sections and returns `true` if the chat has been updated.
53+
*/
54+
function chatHasUpdated(
55+
chatSections: Array<GameChatSection>,
56+
prevChatSections: Array<GameChatSection>
57+
) {
58+
let lastSection = chatSections.length
59+
? chatSections[chatSections.length - 1]
60+
: null;
61+
let prevLastSection = prevChatSections.length
62+
? prevChatSections[prevChatSections.length - 1]
63+
: null;
64+
return (
65+
chatSections.length > prevChatSections.length ||
66+
(lastSection &&
67+
prevLastSection &&
68+
(lastSection.messages.length > prevLastSection.messages.length ||
69+
lastSection.actions.length > prevLastSection.actions.length))
70+
);
71+
}
72+
5073
export default class GameScreen extends Component<Props, State> {
5174
static getDerivedStateFromProps(props: Props, state: State) {
5275
let { chatSections } = state;
5376
let { game } = props;
5477
let currentChatSections = getGameChatSections(game);
5578

56-
if (chatSections.length !== currentChatSections.length) {
79+
if (chatHasUpdated(currentChatSections, chatSections)) {
5780
return { ...state, chatSections: currentChatSections };
5881
}
5982

@@ -85,19 +108,7 @@ export default class GameScreen extends Component<Props, State> {
85108
componentDidUpdate(prevProps: Props, prevState: State) {
86109
let { chatSections } = this.state;
87110
let { chatSections: prevChatSections } = prevState;
88-
let lastSection = chatSections.length
89-
? chatSections[chatSections.length - 1]
90-
: null;
91-
let prevLastSection = prevChatSections.length
92-
? prevChatSections[prevChatSections.length - 1]
93-
: null;
94-
if (
95-
chatSections.length > prevChatSections.length ||
96-
(lastSection &&
97-
prevLastSection &&
98-
(lastSection.messages.length > prevLastSection.messages.length ||
99-
lastSection.actions.length > prevLastSection.actions.length))
100-
) {
111+
if (chatHasUpdated(chatSections, prevChatSections)) {
101112
this._setChatScroll();
102113
}
103114
}

0 commit comments

Comments
 (0)