Use useEffect for async start of waku

This commit is contained in:
Franck Royer 2021-04-21 16:11:07 +10:00
parent df5a4f8326
commit 3285d498c7
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 46 additions and 49 deletions

View File

@ -1,7 +1,7 @@
import { Paper } from '@material-ui/core'; import { Paper } from '@material-ui/core';
import { multiaddr } from 'multiaddr'; import { multiaddr } from 'multiaddr';
import PeerId from 'peer-id'; import PeerId from 'peer-id';
import React from 'react'; import React, { useEffect, useState } from 'react';
import './App.css'; import './App.css';
import { ChatMessage } from 'waku-chat/chat_message'; import { ChatMessage } from 'waku-chat/chat_message';
import { WakuMessage } from 'waku/waku_message'; import { WakuMessage } from 'waku/waku_message';
@ -20,22 +20,17 @@ interface State {
waku?: Waku waku?: Waku
} }
class App extends React.Component<Props, State> { export default function App () {
waku?: Waku; let [state, setState] = useState<State>({ messages: [] });
constructor(props: Props) {
super(props);
this.state = { useEffect(() => {
messages: [] async function initWaku() {
}; try {
const waku = await Waku.create({});
Waku.create({}).then((waku) => { setState(({ messages }) => (
this.state = { waku: waku, messages: this.state.messages }; { waku, messages }
));
waku.libp2p.peerStore.addressBook
.add(PeerId.createFromB58String('QmUJKveCpfwA4cY1zRybMEt5z64FRtMHLFFQwndWrSfMmf'),
[multiaddr('/ip4/127.0.0.1/tcp/7777/ws')]);
waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => { waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => {
const wakuMsg = WakuMessage.decode(event.data); const wakuMsg = WakuMessage.decode(event.data);
@ -43,43 +38,45 @@ class App extends React.Component<Props, State> {
const chatMsg = ChatMessage.decode(wakuMsg.payload); const chatMsg = ChatMessage.decode(wakuMsg.payload);
const msgStr = printMessage(chatMsg); const msgStr = printMessage(chatMsg);
const messages = this.state.messages.slice(); const messages = state.messages.slice();
messages.push(msgStr); messages.push(msgStr);
this.setState({ messages }); setState({ messages, waku });
} }
}); });
// Fire and forget try {
// waku.dial('/ip4/127.0.0.1/tcp/7777/ws/p2p/QmUJKveCpfwA4cY1zRybMEt5z64FRtMHLFFQwndWrSfMmf').then(() => { await waku.dial('/ip4/127.0.0.1/tcp/7777/ws/p2p/QmUJKveCpfwA4cY1zRybMEt5z64FRtMHLFFQwndWrSfMmf');
// console.log('Remote node dialed'); console.log('Remote node dialed');
// }).catch((e) => { } catch (e) {
// console.log('Error when dialing peer ', e); console.log('Error when dialing peer ', e);
// }); }
}).catch((e) => { } catch (e) {
console.log('Error starting waku ', e); console.log('Issue starting waku ', e);
}).then(()=> {
console.log("Waku is started");
});
} }
render() { }
if (!state.waku) {
initWaku()
.then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e));
}
}, [state.waku]);
return ( return (
<div className='App'> <div className='App'>
<div className='chat-room'> <div className='chat-room'>
<WakuContext.Provider value={{ waku: this.state.waku }}> <WakuContext.Provider value={{ waku: state.waku }}>
<Paper> <Paper>
<Room lines={this.state.messages} /> <Room lines={state.messages} />
</Paper> </Paper>
</WakuContext.Provider> </WakuContext.Provider>
</div> </div>
</div> </div>
); );
}
} }
export default App;
// TODO: Make it a proper component // TODO: Make it a proper component
function printMessage(chatMsg: ChatMessage) { function printMessage(chatMsg: ChatMessage) {
const timestamp = chatMsg.timestamp.toLocaleString([], { const timestamp = chatMsg.timestamp.toLocaleString([], {