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');
return persistedNick !== null ? persistedNick : generate();
});
const [
historicalMessagesRetrieved,
setHistoricalMessagesRetrieved,
] = useState(false);
const [historicalMessagesRetrieved, setHistoricalMessagesRetrieved] =
useState(false);
useEffect(() => {
localStorage.setItem('nick', nick);
@ -122,29 +120,29 @@ export default function App() {
if (!waku) return;
if (historicalMessagesRetrieved) return;
const connectedToStorePeer = new Promise((resolve) =>
waku.libp2p.peerStore.once(
'change:protocols',
({ peerId, protocols }) => {
if (protocols.includes(StoreCodec)) {
resolve(peerId);
}
const checkAndRetrieve = ({ protocols }: { protocols: string[] }) => {
if (protocols.includes(StoreCodec)) {
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);
}
)
);
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]);
return (