This would have failed if the first node we connect to is NOT store

This commit is contained in:
Franck Royer 2021-08-05 16:28:05 +10:00
parent 951e2e296b
commit be771d6619
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 23 additions and 25 deletions

View File

@ -83,10 +83,8 @@ export default function App() {
const persistedNick = window.localStorage.getItem('nick'); const persistedNick = window.localStorage.getItem('nick');
return persistedNick !== null ? persistedNick : generate(); return persistedNick !== null ? persistedNick : generate();
}); });
const [ const [historicalMessagesRetrieved, setHistoricalMessagesRetrieved] =
historicalMessagesRetrieved, useState(false);
setHistoricalMessagesRetrieved,
] = useState(false);
useEffect(() => { useEffect(() => {
localStorage.setItem('nick', nick); localStorage.setItem('nick', nick);
@ -122,29 +120,29 @@ export default function App() {
if (!waku) return; if (!waku) return;
if (historicalMessagesRetrieved) return; if (historicalMessagesRetrieved) return;
const connectedToStorePeer = new Promise((resolve) => const checkAndRetrieve = ({ protocols }: { protocols: string[] }) => {
waku.libp2p.peerStore.once( if (protocols.includes(StoreCodec)) {
'change:protocols', console.log(`Retrieving archived messages}`);
({ peerId, protocols }) => { setHistoricalMessagesRetrieved(true);
if (protocols.includes(StoreCodec)) {
resolve(peerId); try {
} retrieveStoreMessages(waku, dispatchMessages).then((length) =>
console.log(`Messages retrieved:`, length)
);
} catch (e) {
console.log(`Error encountered when retrieving archived messages`, e);
} }
)
);
connectedToStorePeer.then(() => {
console.log(`Retrieving archived messages}`);
setHistoricalMessagesRetrieved(true);
try {
retrieveStoreMessages(waku, dispatchMessages).then((length) =>
console.log(`Messages retrieved:`, length)
);
} catch (e) {
console.log(`Error encountered when retrieving archived messages`, e);
} }
}); };
waku.libp2p.peerStore.on('change:protocols', checkAndRetrieve);
return () => {
waku.libp2p.peerStore.removeListener(
'change:protocols',
checkAndRetrieve
);
};
}, [waku, historicalMessagesRetrieved]); }, [waku, historicalMessagesRetrieved]);
return ( return (