Rename `stateWaku` to `waku`

There should only be one.
This commit is contained in:
Franck Royer 2021-07-15 14:08:07 +10:00
parent f3738b1eac
commit 6e22897f74
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 16 additions and 20 deletions

View File

@ -76,7 +76,7 @@ async function retrieveStoreMessages(
export default function App() { export default function App() {
let [newMessages, setNewMessages] = useState<Message[]>([]); let [newMessages, setNewMessages] = useState<Message[]>([]);
let [archivedMessages, setArchivedMessages] = useState<Message[]>([]); let [archivedMessages, setArchivedMessages] = useState<Message[]>([]);
let [stateWaku, setWaku] = useState<Waku | undefined>(undefined); let [waku, setWaku] = useState<Waku | undefined>(undefined);
let [nick, setNick] = useState<string>(() => { let [nick, setNick] = useState<string>(() => {
const persistedNick = window.localStorage.getItem('nick'); const persistedNick = window.localStorage.getItem('nick');
return persistedNick !== null ? persistedNick : generate(); return persistedNick !== null ? persistedNick : generate();
@ -87,15 +87,15 @@ export default function App() {
}, [nick]); }, [nick]);
useEffect(() => { useEffect(() => {
if (stateWaku) return; if (waku) return;
initWaku(setWaku) initWaku(setWaku)
.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));
}, [stateWaku]); }, [waku]);
useEffect(() => { useEffect(() => {
if (!stateWaku) return; if (!waku) return;
const handleRelayMessage = (wakuMsg: WakuMessage) => { const handleRelayMessage = (wakuMsg: WakuMessage) => {
console.log('Message received: ', wakuMsg); console.log('Message received: ', wakuMsg);
@ -105,23 +105,23 @@ export default function App() {
} }
}; };
stateWaku.relay.addObserver(handleRelayMessage, [ChatContentTopic]); waku.relay.addObserver(handleRelayMessage, [ChatContentTopic]);
return; return;
}, [stateWaku]); }, [waku]);
useEffect(() => { useEffect(() => {
if (!stateWaku) return; if (!waku) return;
const handleProtocolChange = async ( const handleProtocolChange = async (
waku: Waku, _waku: Waku,
{ peerId, protocols }: { peerId: PeerId; protocols: string[] } { peerId, protocols }: { peerId: PeerId; protocols: string[] }
) => { ) => {
if (protocols.includes(StoreCodec)) { if (protocols.includes(StoreCodec)) {
console.log(`${peerId.toB58String()}: retrieving archived messages}`); console.log(`${peerId.toB58String()}: retrieving archived messages}`);
try { try {
const length = await retrieveStoreMessages( const length = await retrieveStoreMessages(
waku, _waku,
peerId, peerId,
setArchivedMessages setArchivedMessages
); );
@ -135,37 +135,33 @@ export default function App() {
} }
}; };
stateWaku.libp2p.peerStore.on( waku.libp2p.peerStore.on(
'change:protocols', 'change:protocols',
handleProtocolChange.bind({}, stateWaku) handleProtocolChange.bind({}, waku)
); );
// To clean up listener when component unmounts // To clean up listener when component unmounts
return () => { return () => {
stateWaku?.libp2p.peerStore.removeListener( waku?.libp2p.peerStore.removeListener(
'change:protocols', 'change:protocols',
handleProtocolChange.bind({}, stateWaku) handleProtocolChange.bind({}, waku)
); );
}; };
}, [stateWaku]); }, [waku]);
return ( return (
<div <div
className="chat-app" className="chat-app"
style={{ height: '100vh', width: '100vw', overflow: 'hidden' }} style={{ height: '100vh', width: '100vw', overflow: 'hidden' }}
> >
<WakuContext.Provider value={{ waku: stateWaku }}> <WakuContext.Provider value={{ waku: waku }}>
<ThemeProvider theme={themes}> <ThemeProvider theme={themes}>
<Room <Room
nick={nick} nick={nick}
newMessages={newMessages} newMessages={newMessages}
archivedMessages={archivedMessages} archivedMessages={archivedMessages}
commandHandler={(input: string) => { commandHandler={(input: string) => {
const { command, response } = handleCommand( const { command, response } = handleCommand(input, waku, setNick);
input,
stateWaku,
setNick
);
const commandMessages = response.map((msg) => { const commandMessages = response.map((msg) => {
return Message.fromUtf8String(command, msg); return Message.fromUtf8String(command, msg);
}); });