From 8322d24ac162ce28a007d21d4e7ca4f3d098369c Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 5 Aug 2021 16:01:14 +1000 Subject: [PATCH] Always clean up listeners! --- examples/store-reactjs-chat/src/App.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/store-reactjs-chat/src/App.js b/examples/store-reactjs-chat/src/App.js index 564a4e4571..11edd149ac 100644 --- a/examples/store-reactjs-chat/src/App.js +++ b/examples/store-reactjs-chat/src/App.js @@ -65,18 +65,24 @@ function App() { // We do not handle disconnection/re-connection in this example 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 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. // Meaning if we are not connected to a store node, then it just fails and // we try again 10s later. - waku.libp2p.peerStore.once('change:protocols', ({ protocols }) => { - if (protocols.includes(StoreCodec)) { - // We are now connected to a store node - setConnectedToStore(true); - } - }); + waku.libp2p.peerStore.on('change:protocols', isStoreNode); + + return () => { + waku.libp2p.peerStore.removeListener('change:protocols', isStoreNode); + }; }, [waku, connectedToStore]); return (