chat fixes and optimizations (#431)
* - format messages on didMount instead of didUpdate. will also prevent bad setSTate loops when message is blank; - convert message.js to functional comp - prevent extra rerenders in messages and chat with shouldComponentUpdate checks * revert chat test * more concise returns;
This commit is contained in:
@@ -11,26 +11,39 @@ import { convertToText } from '../../utils/chat.js';
|
|||||||
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
||||||
|
|
||||||
export default class ChatMessageView extends Component {
|
export default class ChatMessageView extends Component {
|
||||||
async componentDidUpdate(prevProps) {
|
constructor(props) {
|
||||||
const { message, username } = this.props;
|
super(props);
|
||||||
if (prevProps.message === message && this.state.formattedMessage) {
|
this.state = {
|
||||||
return;
|
formattedMessage: '',
|
||||||
|
};
|
||||||
}
|
}
|
||||||
const { body } = message;
|
|
||||||
|
|
||||||
|
shouldComponentUpdate(nextProps, nextState) {
|
||||||
|
const { formattedMessage } = this.state;
|
||||||
|
const { formattedMessage: nextFormattedMessage } = nextState;
|
||||||
|
|
||||||
|
return (formattedMessage !== nextFormattedMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
async componentDidMount() {
|
||||||
|
const { message, username } = this.props;
|
||||||
|
if (message && username) {
|
||||||
|
const { body } = message;
|
||||||
const formattedMessage = await formatMessageText(body, username);
|
const formattedMessage = await formatMessageText(body, username);
|
||||||
this.setState({
|
this.setState({
|
||||||
formattedMessage
|
formattedMessage,
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { message } = this.props;
|
const { message } = this.props;
|
||||||
const { author, timestamp } = message;
|
const { author, timestamp } = message;
|
||||||
|
|
||||||
const { formattedMessage } = this.state;
|
const { formattedMessage } = this.state;
|
||||||
if (!formattedMessage) {
|
if (!formattedMessage) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
const formattedTimestamp = formatTimestamp(timestamp);
|
const formattedTimestamp = formatTimestamp(timestamp);
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export default class Chat extends Component {
|
|||||||
webSocketConnected: true,
|
webSocketConnected: true,
|
||||||
messages: [],
|
messages: [],
|
||||||
chatUserNames: [],
|
chatUserNames: [],
|
||||||
|
newMessagesReceived: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.scrollableMessagesContainer = createRef();
|
this.scrollableMessagesContainer = createRef();
|
||||||
@@ -34,19 +35,37 @@ export default class Chat extends Component {
|
|||||||
this.submitChat = this.submitChat.bind(this);
|
this.submitChat = this.submitChat.bind(this);
|
||||||
this.scrollToBottom = this.scrollToBottom.bind(this);
|
this.scrollToBottom = this.scrollToBottom.bind(this);
|
||||||
this.handleWindowResize = debounce(this.handleWindowResize.bind(this), 500);
|
this.handleWindowResize = debounce(this.handleWindowResize.bind(this), 500);
|
||||||
|
this.handleNetworkingError = this.handleNetworkingError.bind(this);
|
||||||
this.messageListCallback = this.messageListCallback.bind(this);
|
this.messageListCallback = this.messageListCallback.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.setupWebSocketCallbacks();
|
this.setupWebSocketCallbacks();
|
||||||
this.getChatHistory();
|
this.getChatHistory();
|
||||||
|
|
||||||
window.addEventListener('resize', this.handleWindowResize);
|
window.addEventListener('resize', this.handleWindowResize);
|
||||||
|
|
||||||
this.messageListObserver = new MutationObserver(this.messageListCallback);
|
this.messageListObserver = new MutationObserver(this.messageListCallback);
|
||||||
this.messageListObserver.observe(this.scrollableMessagesContainer.current, { childList: true });
|
this.messageListObserver.observe(this.scrollableMessagesContainer.current, { childList: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shouldComponentUpdate(nextProps, nextState) {
|
||||||
|
const { username, chatInputEnabled } = this.props;
|
||||||
|
const { username: nextUserName, chatInputEnabled: nextChatEnabled } = nextProps;
|
||||||
|
|
||||||
|
const { webSocketConnected, messages, chatUserNames, newMessagesReceived } = this.state;
|
||||||
|
const {webSocketConnected: nextSocket, messages: nextMessages, chatUserNames: nextUserNames, newMessagesReceived: nextMessagesReceived } = nextState;
|
||||||
|
|
||||||
|
return (
|
||||||
|
username !== nextUserName ||
|
||||||
|
chatInputEnabled !== nextChatEnabled ||
|
||||||
|
webSocketConnected !== nextSocket ||
|
||||||
|
messages.length !== nextMessages.length ||
|
||||||
|
chatUserNames.length !== nextUserNames.length || newMessagesReceived !== nextMessagesReceived
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
componentDidUpdate(prevProps, prevState) {
|
componentDidUpdate(prevProps, prevState) {
|
||||||
const { username: prevName } = prevProps;
|
const { username: prevName } = prevProps;
|
||||||
const { username } = this.props;
|
const { username } = this.props;
|
||||||
@@ -61,7 +80,9 @@ export default class Chat extends Component {
|
|||||||
|
|
||||||
// scroll to bottom of messages list when new ones come in
|
// scroll to bottom of messages list when new ones come in
|
||||||
if (messages.length > prevMessages.length) {
|
if (messages.length > prevMessages.length) {
|
||||||
this.newMessagesReceived = true;
|
this.setState({
|
||||||
|
newMessagesReceived: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
@@ -96,7 +117,7 @@ export default class Chat extends Component {
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
// this.handleNetworkingError(`Fetch getChatHistory: ${error}`);
|
this.handleNetworkingError(`Fetch getChatHistory: ${error}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,6 +134,11 @@ export default class Chat extends Component {
|
|||||||
this.addMessage(message);
|
this.addMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleNetworkingError(error) {
|
||||||
|
// todo: something more useful
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
|
||||||
addMessage(message) {
|
addMessage(message) {
|
||||||
const { messages: curMessages } = this.state;
|
const { messages: curMessages } = this.state;
|
||||||
|
|
||||||
@@ -196,14 +222,16 @@ export default class Chat extends Component {
|
|||||||
if (numMutations) {
|
if (numMutations) {
|
||||||
const item = mutations[numMutations - 1];
|
const item = mutations[numMutations - 1];
|
||||||
if (item.type === 'childList' && item.addedNodes.length) {
|
if (item.type === 'childList' && item.addedNodes.length) {
|
||||||
if (this.newMessagesReceived) {
|
if (this.state.newMessagesReceived) {
|
||||||
if (!this.receivedFirstMessages) {
|
if (!this.receivedFirstMessages) {
|
||||||
this.scrollToBottom();
|
this.scrollToBottom();
|
||||||
this.receivedFirstMessages = true;
|
this.receivedFirstMessages = true;
|
||||||
} else if (this.checkShouldScroll()) {
|
} else if (this.checkShouldScroll()) {
|
||||||
this.scrollToBottom();
|
this.scrollToBottom();
|
||||||
}
|
}
|
||||||
this.newMessagesReceived = false;
|
this.setState({
|
||||||
|
newMessagesReceived: false,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
import { h, Component } from '/js/web_modules/preact.js';
|
import { h } from '/js/web_modules/preact.js';
|
||||||
import htm from '/js/web_modules/htm.js';
|
import htm from '/js/web_modules/htm.js';
|
||||||
const html = htm.bind(h);
|
const html = htm.bind(h);
|
||||||
|
|
||||||
import ChatMessageView from './chat-message-view.js';
|
import ChatMessageView from './chat-message-view.js';
|
||||||
|
|
||||||
import { messageBubbleColorForString } from '../../utils/user-colors.js';
|
|
||||||
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
||||||
|
|
||||||
export default class Message extends Component {
|
export default function Message(props) {
|
||||||
render(props) {
|
|
||||||
const { message } = props;
|
const { message } = props;
|
||||||
const { type } = message;
|
const { type } = message;
|
||||||
if (type === SOCKET_MESSAGE_TYPES.CHAT || type === SOCKET_MESSAGE_TYPES.SYSTEM) {
|
if (type === SOCKET_MESSAGE_TYPES.CHAT || type === SOCKET_MESSAGE_TYPES.SYSTEM) {
|
||||||
@@ -30,5 +28,4 @@ export default class Message extends Component {
|
|||||||
console.log("Unknown message type:", type);
|
console.log("Unknown message type:", type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user