Only add new listeners when waku is initialised

This commit is contained in:
Franck Royer 2021-05-05 14:29:00 +10:00
parent 9c65e590c4
commit f4663ab247
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 84 additions and 59 deletions

View File

@ -46,15 +46,16 @@ const themes = {
export const ChatContentTopic = 'dingpu'; export const ChatContentTopic = 'dingpu';
export default function App() { export default function App() {
let [stateMessages, setMessages] = useState<ChatMessage[]>([]); let [newMessages, setNewMessages] = useState<ChatMessage[]>([]);
let [archivedMessages, setArchivedMessages] = useState<ChatMessage[]>([]);
let [stateWaku, setWaku] = useState<Waku | undefined>(undefined); let [stateWaku, setWaku] = useState<Waku | undefined>(undefined);
let [nick, setNick] = useState<string>(generate()); let [nick, setNick] = useState<string>(generate());
useEffect(() => { useEffect(() => {
const handleNewMessages = (event: { data: Uint8Array }) => { const handleRelayMessage = (event: { data: Uint8Array }) => {
const chatMsg = decodeWakuMessage(event.data); const chatMsg = decodeWakuMessage(event.data);
if (chatMsg) { if (chatMsg) {
copyAppendReplace([chatMsg], stateMessages, setMessages); setNewMessages([chatMsg]);
} }
}; };
@ -78,7 +79,7 @@ export default function App() {
.map((wakuChatMessage) => .map((wakuChatMessage) =>
ChatMessage.fromWakuChatMessage(wakuChatMessage) ChatMessage.fromWakuChatMessage(wakuChatMessage)
); );
copyMergeUniqueReplace(messages, stateMessages, setMessages); setArchivedMessages(messages);
} }
} }
}; };
@ -88,7 +89,7 @@ export default function App() {
.then(() => console.log('Waku init done')) .then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e)); .catch((e) => console.log('Waku init failed ', e));
} else { } else {
stateWaku.libp2p.pubsub.on(RelayDefaultTopic, handleNewMessages); stateWaku.libp2p.pubsub.on(RelayDefaultTopic, handleRelayMessage);
stateWaku.libp2p.peerStore.on( stateWaku.libp2p.peerStore.on(
'change:protocols', 'change:protocols',
@ -99,7 +100,7 @@ export default function App() {
return () => { return () => {
stateWaku?.libp2p.pubsub.removeListener( stateWaku?.libp2p.pubsub.removeListener(
RelayDefaultTopic, RelayDefaultTopic,
handleNewMessages handleRelayMessage
); );
stateWaku?.libp2p.peerStore.removeListener( stateWaku?.libp2p.peerStore.removeListener(
'change:protocols', 'change:protocols',
@ -107,7 +108,7 @@ export default function App() {
); );
}; };
} }
}, [stateWaku, stateMessages]); }, [stateWaku]);
return ( return (
<div <div
@ -118,7 +119,8 @@ export default function App() {
<ThemeProvider theme={themes}> <ThemeProvider theme={themes}>
<Room <Room
nick={nick} nick={nick}
lines={stateMessages} newMessages={newMessages}
archivedMessages={archivedMessages}
commandHandler={(input: string) => { commandHandler={(input: string) => {
const { command, response } = handleCommand( const { command, response } = handleCommand(
input, input,
@ -128,7 +130,7 @@ export default function App() {
const commandMessages = response.map((msg) => { const commandMessages = response.map((msg) => {
return new ChatMessage(new Date(), new Date(), command, msg); return new ChatMessage(new Date(), new Date(), command, msg);
}); });
copyAppendReplace(commandMessages, stateMessages, setMessages); setNewMessages(commandMessages);
}} }}
/> />
</ThemeProvider> </ThemeProvider>
@ -170,35 +172,3 @@ function decodeWakuMessage(data: Uint8Array): null | ChatMessage {
WakuChatMessage.decode(wakuMsg.payload) WakuChatMessage.decode(wakuMsg.payload)
); );
} }
function copyAppendReplace<T>(
newValues: Array<T>,
currentValues: Array<T>,
setter: (val: Array<T>) => void
) {
const copy = currentValues.slice();
setter(copy.concat(newValues));
}
function copyMergeUniqueReplace(
newValues: ChatMessage[],
currentValues: ChatMessage[],
setter: (val: ChatMessage[]) => void
) {
const copy = currentValues.slice();
newValues.forEach((msg) => {
if (!copy.find(isEqual.bind({}, msg))) {
copy.push(msg);
}
});
copy.sort((a, b) => a.sentTimestamp.valueOf() - b.sentTimestamp.valueOf());
setter(copy);
}
function isEqual(lhs: ChatMessage, rhs: ChatMessage): boolean {
return (
lhs.nick === rhs.nick &&
lhs.message === rhs.message &&
lhs.sentTimestamp.toString() === rhs.sentTimestamp.toString()
);
}

View File

@ -1,4 +1,4 @@
import { useEffect, useRef } from 'react'; import { useEffect, useRef, useState } from 'react';
import { ChatMessage } from './ChatMessage'; import { ChatMessage } from './ChatMessage';
import { import {
Message, Message,
@ -8,18 +8,40 @@ import {
} from '@livechat/ui-kit'; } from '@livechat/ui-kit';
interface Props { interface Props {
messages: ChatMessage[]; archivedMessages: ChatMessage[];
newMessages: ChatMessage[];
} }
export default function ChatList(props: Props) { export default function ChatList(props: Props) {
const messages = props.messages; const [messages, setMessages] = useState<ChatMessage[]>([]);
let updatedMessages;
const messagesGroupedBySender = groupMessagesBySender(props.messages).map( if (IsThereNewMessages(props.newMessages, messages)) {
updatedMessages = messages.slice().concat(props.newMessages);
if (IsThereNewMessages(props.archivedMessages, updatedMessages)) {
updatedMessages = copyMergeUniqueReplace(
props.archivedMessages,
updatedMessages
);
}
} else {
if (IsThereNewMessages(props.archivedMessages, messages)) {
updatedMessages = copyMergeUniqueReplace(
props.archivedMessages,
messages
);
}
}
if (updatedMessages) {
setMessages(updatedMessages);
}
const messagesGroupedBySender = groupMessagesBySender(messages).map(
(currentMessageGroup) => ( (currentMessageGroup) => (
<MessageGroup onlyFirstWithMeta> <MessageGroup onlyFirstWithMeta>
{currentMessageGroup.map((currentMessage) => ( {currentMessageGroup.map((currentMessage) => (
<Message <Message
// We assume that the same user is not sending two messages in the same second
key={ key={
currentMessage.receivedTimestampMs.valueOf() + currentMessage.receivedTimestampMs.valueOf() +
currentMessage.nick + currentMessage.nick +
@ -38,7 +60,7 @@ export default function ChatList(props: Props) {
return ( return (
<MessageList active containScrollInSubtree> <MessageList active containScrollInSubtree>
{messagesGroupedBySender} {messagesGroupedBySender}
<AlwaysScrollToBottom messages={messages} /> <AlwaysScrollToBottom newMessages={props.newMessages} />
</MessageList> </MessageList>
); );
} }
@ -71,14 +93,48 @@ function formatDisplayDate(message: ChatMessage): string {
}); });
} }
const AlwaysScrollToBottom = (props: Props) => { const AlwaysScrollToBottom = (props: { newMessages: ChatMessage[] }) => {
const elementRef = useRef<HTMLDivElement>(); const elementRef = useRef<HTMLDivElement>();
useEffect(() => { useEffect(() => {
// @ts-ignore // @ts-ignore
elementRef.current.scrollIntoView(); elementRef.current.scrollIntoView();
}, [props.messages]); }, [props.newMessages]);
// @ts-ignore // @ts-ignore
return <div ref={elementRef} />; return <div ref={elementRef} />;
}; };
function IsThereNewMessages(
newValues: ChatMessage[],
currentValues: ChatMessage[]
): boolean {
if (newValues.length === 0) return false;
if (currentValues.length === 0) return true;
return !newValues.find((newMsg) =>
currentValues.find(isEqual.bind({}, newMsg))
);
}
function copyMergeUniqueReplace(
newValues: ChatMessage[],
currentValues: ChatMessage[]
) {
const copy = currentValues.slice();
newValues.forEach((msg) => {
if (!copy.find(isEqual.bind({}, msg))) {
copy.push(msg);
}
});
copy.sort((a, b) => a.sentTimestamp.valueOf() - b.sentTimestamp.valueOf());
return copy;
}
function isEqual(lhs: ChatMessage, rhs: ChatMessage): boolean {
return (
lhs.nick === rhs.nick &&
lhs.message === rhs.message &&
lhs.sentTimestamp.toString() === rhs.sentTimestamp.toString()
);
}

View File

@ -10,8 +10,7 @@ import {
} from '@livechat/ui-kit'; } from '@livechat/ui-kit';
interface Props { interface Props {
messageHandler: (msg: string) => void; sendMessage: ((msg: string) => Promise<void>) | undefined;
sendMessage: (() => Promise<void>) | undefined;
} }
export default function MessageInput(props: Props) { export default function MessageInput(props: Props) {
@ -20,14 +19,13 @@ export default function MessageInput(props: Props) {
const sendMessage = async () => { const sendMessage = async () => {
if (props.sendMessage) { if (props.sendMessage) {
await props.sendMessage(); await props.sendMessage(inputText);
setInputText(''); setInputText('');
} }
}; };
const messageHandler = (event: ChangeEvent<HTMLInputElement>) => { const messageHandler = (event: ChangeEvent<HTMLInputElement>) => {
setInputText(event.target.value); setInputText(event.target.value);
props.messageHandler(event.target.value);
}; };
const keyPressHandler = async (event: KeyboardEvent<HTMLInputElement>) => { const keyPressHandler = async (event: KeyboardEvent<HTMLInputElement>) => {

View File

@ -1,4 +1,3 @@
import { useState } from 'react';
import { ChatMessage } from './ChatMessage'; import { ChatMessage } from './ChatMessage';
import { ChatMessage as WakuChatMessage } from 'waku/chat_message'; import { ChatMessage as WakuChatMessage } from 'waku/chat_message';
import { WakuMessage } from 'waku/waku_message'; import { WakuMessage } from 'waku/waku_message';
@ -9,13 +8,13 @@ import { useWaku } from './WakuContext';
import { TitleBar } from '@livechat/ui-kit'; import { TitleBar } from '@livechat/ui-kit';
interface Props { interface Props {
lines: ChatMessage[]; newMessages: ChatMessage[];
archivedMessages: ChatMessage[];
commandHandler: (cmd: string) => void; commandHandler: (cmd: string) => void;
nick: string; nick: string;
} }
export default function Room(props: Props) { export default function Room(props: Props) {
let [messageToSend, setMessageToSend] = useState<string>('');
const { waku } = useWaku(); const { waku } = useWaku();
return ( return (
@ -24,12 +23,14 @@ export default function Room(props: Props) {
style={{ height: '98vh', display: 'flex', flexDirection: 'column' }} style={{ height: '98vh', display: 'flex', flexDirection: 'column' }}
> >
<TitleBar title="Waku v2 chat app" /> <TitleBar title="Waku v2 chat app" />
<ChatList messages={props.lines} /> <ChatList
newMessages={props.newMessages}
archivedMessages={props.archivedMessages}
/>
<MessageInput <MessageInput
messageHandler={setMessageToSend}
sendMessage={ sendMessage={
waku waku
? async () => { ? async (messageToSend) => {
return handleMessage( return handleMessage(
messageToSend, messageToSend,
props.nick, props.nick,