mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-05-05 04:13:20 +00:00
Always clean up listeners!
This commit is contained in:
parent
cecbaad4f2
commit
8322d24ac1
@ -65,18 +65,24 @@ function App() {
|
|||||||
// We do not handle disconnection/re-connection in this example
|
// We do not handle disconnection/re-connection in this example
|
||||||
if (connectedToStore) return;
|
if (connectedToStore) return;
|
||||||
|
|
||||||
|
const isStoreNode = ({ protocols }) => {
|
||||||
|
if (protocols.includes(StoreCodec)) {
|
||||||
|
// We are now connected to a store node
|
||||||
|
setConnectedToStore(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// This demonstrates one way to wait to be connected to a store node.
|
// This demonstrates one way to wait to be connected to a store node.
|
||||||
//
|
//
|
||||||
// This is only for demonstration purposes as it is not needed in this
|
// This is only for demonstration purposes as it is not needed in this
|
||||||
// example app as we query the store node every 10s and catch if it fails.
|
// example app as we query the store node every 10s and catch if it fails.
|
||||||
// Meaning if we are not connected to a store node, then it just fails and
|
// Meaning if we are not connected to a store node, then it just fails and
|
||||||
// we try again 10s later.
|
// we try again 10s later.
|
||||||
waku.libp2p.peerStore.once('change:protocols', ({ protocols }) => {
|
waku.libp2p.peerStore.on('change:protocols', isStoreNode);
|
||||||
if (protocols.includes(StoreCodec)) {
|
|
||||||
// We are now connected to a store node
|
return () => {
|
||||||
setConnectedToStore(true);
|
waku.libp2p.peerStore.removeListener('change:protocols', isStoreNode);
|
||||||
}
|
};
|
||||||
});
|
|
||||||
}, [waku, connectedToStore]);
|
}, [waku, connectedToStore]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user