Clean up observer when component dismount

This commit is contained in:
Franck Royer 2021-06-16 16:36:12 +10:00
parent 1d3f0e4c8a
commit 4e7f75868f
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 17 additions and 0 deletions

View File

@ -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(