mirror of https://github.com/waku-org/js-waku.git
This would have failed if the first node we connect to is NOT store
This commit is contained in:
parent
951e2e296b
commit
be771d6619
|
@ -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,18 +120,8 @@ 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(
|
|
||||||
'change:protocols',
|
|
||||||
({ peerId, protocols }) => {
|
|
||||||
if (protocols.includes(StoreCodec)) {
|
if (protocols.includes(StoreCodec)) {
|
||||||
resolve(peerId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
connectedToStorePeer.then(() => {
|
|
||||||
console.log(`Retrieving archived messages}`);
|
console.log(`Retrieving archived messages}`);
|
||||||
setHistoricalMessagesRetrieved(true);
|
setHistoricalMessagesRetrieved(true);
|
||||||
|
|
||||||
|
@ -144,7 +132,17 @@ export default function App() {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(`Error encountered when retrieving archived messages`, 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 (
|
||||||
|
|
Loading…
Reference in New Issue