From 4e7f75868fb0ae4f8a197268c3d6e483ff542f16 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 16 Jun 2021 16:36:12 +1000 Subject: [PATCH] Clean up observer when component dismount --- examples/eth-dm/src/App.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/examples/eth-dm/src/App.tsx b/examples/eth-dm/src/App.tsx index 67be5f1436..830cd010d5 100644 --- a/examples/eth-dm/src/App.tsx +++ b/examples/eth-dm/src/App.tsx @@ -72,6 +72,11 @@ function App() { useEffect(() => { if (!waku) return; waku.relay.addObserver(observerPublicKeyMessage, [PublicKeyContentTopic]); + + return function cleanUp() { + if (!waku) return; + waku.relay.deleteObserver(observerPublicKeyMessage, [PublicKeyContentTopic]); + } }); useEffect(() => { @@ -81,6 +86,16 @@ function App() { observerDirectMessage.bind({}, ethDmKeyPair.privateKey), [DirectMessageContentTopic] ); + + return function cleanUp() { + if (!waku) return; + if (!ethDmKeyPair) return; + waku.relay.deleteObserver( + observerDirectMessage.bind({}, ethDmKeyPair.privateKey), + [DirectMessageContentTopic] + ); + + } }); const broadcastPublicKey = () => { @@ -109,6 +124,8 @@ function App() { }; const sendDummyMessage = () => { + if (!waku) return; + console.log(`Sending messages to ${publicKeys.size} peers`); publicKeys.forEach(async (publicKey, address) => { const msg = await encodeEncryptedWakuMessage(