mirror of
https://github.com/status-im/js-waku.git
synced 2025-02-24 02:48:11 +00:00
Remove InitWaku component
There was an issue where the observers are added/removed continously. This is due to using `useEffect` on props. By removing this component then `useEffect` ends only being called when waku changes, ie, at initialisation.
This commit is contained in:
parent
db6bb95a9b
commit
eec6de9f0c
@ -10,7 +10,6 @@ import { Message } from './messaging/Messages';
|
|||||||
import 'fontsource-roboto';
|
import 'fontsource-roboto';
|
||||||
import { AppBar, IconButton, Toolbar, Typography } from '@material-ui/core';
|
import { AppBar, IconButton, Toolbar, Typography } from '@material-ui/core';
|
||||||
import KeyPairHandling from './key_pair_handling/KeyPairHandling';
|
import KeyPairHandling from './key_pair_handling/KeyPairHandling';
|
||||||
import InitWaku from './InitWaku';
|
|
||||||
import {
|
import {
|
||||||
createMuiTheme,
|
createMuiTheme,
|
||||||
ThemeProvider,
|
ThemeProvider,
|
||||||
@ -20,6 +19,13 @@ import { teal, purple, green } from '@material-ui/core/colors';
|
|||||||
import WifiIcon from '@material-ui/icons/Wifi';
|
import WifiIcon from '@material-ui/icons/Wifi';
|
||||||
import BroadcastPublicKey from './BroadcastPublicKey';
|
import BroadcastPublicKey from './BroadcastPublicKey';
|
||||||
import Messaging from './messaging/Messaging';
|
import Messaging from './messaging/Messaging';
|
||||||
|
import {
|
||||||
|
DirectMessageContentTopic,
|
||||||
|
handleDirectMessage,
|
||||||
|
handlePublicKeyMessage,
|
||||||
|
initWaku,
|
||||||
|
PublicKeyContentTopic,
|
||||||
|
} from './waku';
|
||||||
|
|
||||||
declare let window: any;
|
declare let window: any;
|
||||||
|
|
||||||
@ -71,6 +77,61 @@ function App() {
|
|||||||
|
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
|
|
||||||
|
// Waku initialization
|
||||||
|
useEffect(() => {
|
||||||
|
if (waku) return;
|
||||||
|
initWaku()
|
||||||
|
.then((_waku) => {
|
||||||
|
console.log('waku: ready');
|
||||||
|
setWaku(_waku);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.error('Failed to initiate Waku', e);
|
||||||
|
});
|
||||||
|
}, [waku]);
|
||||||
|
|
||||||
|
const observerPublicKeyMessage = handlePublicKeyMessage.bind(
|
||||||
|
{},
|
||||||
|
ethDmKeyPair?.publicKey,
|
||||||
|
setPublicKeys
|
||||||
|
);
|
||||||
|
|
||||||
|
const observerDirectMessage =
|
||||||
|
ethDmKeyPair && address
|
||||||
|
? handleDirectMessage.bind(
|
||||||
|
{},
|
||||||
|
setMessages,
|
||||||
|
ethDmKeyPair.privateKey,
|
||||||
|
address
|
||||||
|
)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!waku) return;
|
||||||
|
waku.relay.addObserver(observerPublicKeyMessage, [PublicKeyContentTopic]);
|
||||||
|
|
||||||
|
return function cleanUp() {
|
||||||
|
if (!waku) return;
|
||||||
|
waku.relay.deleteObserver(observerPublicKeyMessage, [
|
||||||
|
PublicKeyContentTopic,
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
}, [waku]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!waku) return;
|
||||||
|
if (!observerDirectMessage) return;
|
||||||
|
waku.relay.addObserver(observerDirectMessage, [DirectMessageContentTopic]);
|
||||||
|
|
||||||
|
return function cleanUp() {
|
||||||
|
if (!waku) return;
|
||||||
|
if (!observerDirectMessage) return;
|
||||||
|
waku.relay.deleteObserver(observerDirectMessage, [
|
||||||
|
DirectMessageContentTopic,
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
}, [waku]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
try {
|
try {
|
||||||
window.ethereum
|
window.ethereum
|
||||||
@ -123,14 +184,6 @@ function App() {
|
|||||||
|
|
||||||
<div className={classes.container}>
|
<div className={classes.container}>
|
||||||
<main className={classes.main}>
|
<main className={classes.main}>
|
||||||
<InitWaku
|
|
||||||
ethDmKeyPair={ethDmKeyPair}
|
|
||||||
setMessages={setMessages}
|
|
||||||
setPublicKeys={setPublicKeys}
|
|
||||||
setWaku={setWaku}
|
|
||||||
waku={waku}
|
|
||||||
address={address}
|
|
||||||
/>
|
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend>Eth-DM Key Pair</legend>
|
<legend>Eth-DM Key Pair</legend>
|
||||||
<KeyPairHandling
|
<KeyPairHandling
|
||||||
|
@ -4,7 +4,7 @@ import { createPublicKeyMessage, KeyPair } from './crypto';
|
|||||||
import { PublicKeyMessage } from './messaging/wire';
|
import { PublicKeyMessage } from './messaging/wire';
|
||||||
import { WakuMessage, Waku } from 'js-waku';
|
import { WakuMessage, Waku } from 'js-waku';
|
||||||
import { Signer } from '@ethersproject/abstract-signer';
|
import { Signer } from '@ethersproject/abstract-signer';
|
||||||
import { PublicKeyContentTopic } from './InitWaku';
|
import { PublicKeyContentTopic } from './waku';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
ethDmKeyPair: KeyPair | undefined;
|
ethDmKeyPair: KeyPair | undefined;
|
||||||
|
@ -10,7 +10,7 @@ import React, { ChangeEvent, useState, KeyboardEvent } from 'react';
|
|||||||
import { Waku, WakuMessage } from 'js-waku';
|
import { Waku, WakuMessage } from 'js-waku';
|
||||||
import { DirectMessage, encode } from './wire';
|
import { DirectMessage, encode } from './wire';
|
||||||
import { encryptMessage } from '../crypto';
|
import { encryptMessage } from '../crypto';
|
||||||
import { DirectMessageContentTopic } from '../InitWaku';
|
import { DirectMessageContentTopic } from '../waku';
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles((theme) => ({
|
||||||
formControl: {
|
formControl: {
|
||||||
|
@ -8,87 +8,7 @@ import { byteArrayToHex } from './utils';
|
|||||||
export const PublicKeyContentTopic = '/eth-dm/1/public-key/proto';
|
export const PublicKeyContentTopic = '/eth-dm/1/public-key/proto';
|
||||||
export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json';
|
export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json';
|
||||||
|
|
||||||
interface Props {
|
export async function initWaku(): Promise<Waku> {
|
||||||
waku: Waku | undefined;
|
|
||||||
setWaku: (waku: Waku) => void;
|
|
||||||
ethDmKeyPair: KeyPair | undefined;
|
|
||||||
setPublicKeys: Dispatch<SetStateAction<Map<string, string>>>;
|
|
||||||
setMessages: Dispatch<SetStateAction<Message[]>>;
|
|
||||||
address: string | undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does all the waku initialization
|
|
||||||
*/
|
|
||||||
export default function InitWaku({
|
|
||||||
waku,
|
|
||||||
setWaku,
|
|
||||||
ethDmKeyPair,
|
|
||||||
setPublicKeys,
|
|
||||||
setMessages,
|
|
||||||
address,
|
|
||||||
}: Props) {
|
|
||||||
useEffect(() => {
|
|
||||||
if (waku) return;
|
|
||||||
initWaku()
|
|
||||||
.then((wakuNode) => {
|
|
||||||
console.log('waku: ready');
|
|
||||||
setWaku(wakuNode);
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
console.error('Failed to initiate Waku', e);
|
|
||||||
});
|
|
||||||
}, [waku, setWaku]);
|
|
||||||
|
|
||||||
const observerPublicKeyMessage = handlePublicKeyMessage.bind(
|
|
||||||
{},
|
|
||||||
ethDmKeyPair?.publicKey,
|
|
||||||
setPublicKeys
|
|
||||||
);
|
|
||||||
|
|
||||||
const observerDirectMessage =
|
|
||||||
ethDmKeyPair && address
|
|
||||||
? handleDirectMessage.bind(
|
|
||||||
{},
|
|
||||||
setMessages,
|
|
||||||
ethDmKeyPair.privateKey,
|
|
||||||
address
|
|
||||||
)
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!waku) return;
|
|
||||||
waku.relay.addObserver(observerPublicKeyMessage, [PublicKeyContentTopic]);
|
|
||||||
|
|
||||||
return function cleanUp() {
|
|
||||||
if (!waku) return;
|
|
||||||
waku.relay.deleteObserver(observerPublicKeyMessage, [
|
|
||||||
PublicKeyContentTopic,
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!waku) return;
|
|
||||||
if (!observerDirectMessage) return;
|
|
||||||
waku.relay.addObserver(observerDirectMessage, [DirectMessageContentTopic]);
|
|
||||||
|
|
||||||
return function cleanUp() {
|
|
||||||
if (!waku) return;
|
|
||||||
if (!observerDirectMessage) return;
|
|
||||||
waku.relay.deleteObserver(observerDirectMessage, [
|
|
||||||
DirectMessageContentTopic,
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// Returns an empty fragment.
|
|
||||||
// Taking advantages of React's state management and useEffect()
|
|
||||||
// Not sure it is best practice but it works.
|
|
||||||
return <></>;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function initWaku(): Promise<Waku> {
|
|
||||||
const waku = await Waku.create({});
|
const waku = await Waku.create({});
|
||||||
|
|
||||||
const nodes = await getNodes();
|
const nodes = await getNodes();
|
||||||
@ -105,7 +25,7 @@ function getNodes() {
|
|||||||
return getStatusFleetNodes(Environment.Prod);
|
return getStatusFleetNodes(Environment.Prod);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handlePublicKeyMessage(
|
export function handlePublicKeyMessage(
|
||||||
myPublicKey: string | undefined,
|
myPublicKey: string | undefined,
|
||||||
setter: Dispatch<SetStateAction<Map<string, string>>>,
|
setter: Dispatch<SetStateAction<Map<string, string>>>,
|
||||||
msg: WakuMessage
|
msg: WakuMessage
|
||||||
@ -128,7 +48,7 @@ function handlePublicKeyMessage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleDirectMessage(
|
export async function handleDirectMessage(
|
||||||
setter: Dispatch<SetStateAction<Message[]>>,
|
setter: Dispatch<SetStateAction<Message[]>>,
|
||||||
privateKey: string,
|
privateKey: string,
|
||||||
address: string,
|
address: string,
|
Loading…
x
Reference in New Issue
Block a user