Use new observer interface in examples

This commit is contained in:
Franck Royer 2021-05-10 12:38:27 +10:00
parent 71a5b23bd2
commit 54f32bacad
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 12 additions and 27 deletions

View File

@ -6,7 +6,6 @@ import { multiaddr, Multiaddr } from 'multiaddr';
import { ChatMessage } from 'waku/chat_message';
import Waku from 'waku/waku';
import { WakuMessage } from 'waku/waku_message';
import { RelayDefaultTopic } from 'waku/waku_relay';
import { StoreCodec } from 'waku/waku_store';
const ChatContentTopic = 'dingpu';
@ -43,12 +42,9 @@ export default async function startChat(): Promise<void> {
console.log(`Hi, ${nick}!`);
// TODO: Bubble event to waku, infer topic, decode msg
// Tracked with https://github.com/status-im/js-waku/issues/19
waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => {
const wakuMsg = WakuMessage.decode(event.data);
if (wakuMsg.payload) {
const chatMsg = ChatMessage.decode(wakuMsg.payload);
waku.relay.addObserver((message) => {
if (message.payload) {
const chatMsg = ChatMessage.decode(message.payload);
console.log(formatMessage(chatMsg));
}
});

View File

@ -5,7 +5,6 @@ import './App.css';
import { ChatMessage } from './ChatMessage';
import { ChatMessage as WakuChatMessage } from 'waku/chat_message';
import { WakuMessage } from 'waku/waku_message';
import { RelayDefaultTopic } from 'waku/waku_relay';
import { StoreCodec } from 'waku/waku_store';
import handleCommand from './command';
import Room from './Room';
@ -52,10 +51,14 @@ export default function App() {
let [nick, setNick] = useState<string>(generate());
useEffect(() => {
const handleRelayMessage = (event: { data: Uint8Array }) => {
const chatMsg = decodeWakuMessage(event.data);
if (chatMsg) {
setNewMessages([chatMsg]);
const handleRelayMessage = (wakuMsg: WakuMessage) => {
if (wakuMsg.payload) {
const chatMsg = ChatMessage.fromWakuChatMessage(
WakuChatMessage.decode(wakuMsg.payload)
);
if (chatMsg) {
setNewMessages([chatMsg]);
}
}
};
@ -94,7 +97,7 @@ export default function App() {
.then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e));
} else {
stateWaku.libp2p.pubsub.on(RelayDefaultTopic, handleRelayMessage);
stateWaku.relay.addObserver(handleRelayMessage);
stateWaku.libp2p.peerStore.on(
'change:protocols',
@ -103,10 +106,6 @@ export default function App() {
// To clean up listener when component unmounts
return () => {
stateWaku?.libp2p.pubsub.removeListener(
RelayDefaultTopic,
handleRelayMessage
);
stateWaku?.libp2p.peerStore.removeListener(
'change:protocols',
handleProtocolChange.bind({}, stateWaku)
@ -173,13 +172,3 @@ async function initWaku(setter: (waku: Waku) => void) {
console.log('Issue starting waku ', e);
}
}
function decodeWakuMessage(data: Uint8Array): null | ChatMessage {
const wakuMsg = WakuMessage.decode(data);
if (!wakuMsg.payload) {
return null;
}
return ChatMessage.fromWakuChatMessage(
WakuChatMessage.decode(wakuMsg.payload)
);
}