fix: remove state for unsubscribe

This commit is contained in:
fryorcraken.eth 2022-08-20 18:49:48 +10:00
parent b6296dd07f
commit c82244c9e6
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -67,10 +67,6 @@ const useStyles = makeStyles({
function App() { function App() {
const [waku, setWaku] = useState<Waku>(); const [waku, setWaku] = useState<Waku>();
const [unsubscribePublicKeyMsg, setUnsubscribePublicKeyMsg] =
useState<() => Promise<void>>();
const [unsubscribePrivateMsg, setUnsubscribePrivateMsg] =
useState<() => Promise<void>>();
const [provider, setProvider] = useState<Web3Provider>(); const [provider, setProvider] = useState<Web3Provider>();
const [encryptionKeyPair, setEncryptionKeyPair] = useState< const [encryptionKeyPair, setEncryptionKeyPair] = useState<
KeyPair | undefined KeyPair | undefined
@ -112,12 +108,15 @@ function App() {
setPublicKeys setPublicKeys
); );
let unsubscribe: undefined | (() => Promise<void>);
waku.filter.addDecryptionKey(PublicKeyMessageEncryptionKey); waku.filter.addDecryptionKey(PublicKeyMessageEncryptionKey);
waku.filter waku.filter
.subscribe(observerPublicKeyMessage, [PublicKeyContentTopic]) .subscribe(observerPublicKeyMessage, [PublicKeyContentTopic])
.then( .then(
(unsubscribe) => { (_unsubscribe) => {
setUnsubscribePublicKeyMsg(unsubscribe); console.log("subscribed to ", PublicKeyContentTopic);
unsubscribe = _unsubscribe;
}, },
(e) => { (e) => {
console.error("Failed to subscribe", e); console.error("Failed to subscribe", e);
@ -126,14 +125,16 @@ function App() {
return function cleanUp() { return function cleanUp() {
if (!waku) return; if (!waku) return;
if (!unsubscribePublicKeyMsg) return;
waku.filter.deleteDecryptionKey(PublicKeyMessageEncryptionKey); waku.filter.deleteDecryptionKey(PublicKeyMessageEncryptionKey);
unsubscribePublicKeyMsg().catch((e) => if (typeof unsubscribe === "undefined") return;
console.error("Failed to unsubscribe", e) unsubscribe().then(
() => {
console.log("unsubscribed to ", PublicKeyContentTopic);
},
(e) => console.error("Failed to unsubscribe", e)
); );
}; };
}, [waku, address, unsubscribePublicKeyMsg]); }, [waku, address]);
useEffect(() => { useEffect(() => {
if (!waku) return; if (!waku) return;
@ -160,11 +161,13 @@ function App() {
address address
); );
let unsubscribe: undefined | (() => Promise<void>);
waku.filter waku.filter
.subscribe(observerPrivateMessage, [PrivateMessageContentTopic]) .subscribe(observerPrivateMessage, [PrivateMessageContentTopic])
.then( .then(
(unsubscribe) => { (_unsubscribe) => {
setUnsubscribePrivateMsg(unsubscribe); unsubscribe = _unsubscribe;
}, },
(e) => { (e) => {
console.error("Failed to subscribe", e); console.error("Failed to subscribe", e);
@ -173,12 +176,10 @@ function App() {
return function cleanUp() { return function cleanUp() {
if (!waku) return; if (!waku) return;
if (!unsubscribePrivateMsg) return; if (typeof unsubscribe === "undefined") return;
unsubscribePrivateMsg().catch((e) => unsubscribe().catch((e) => console.error("Failed to unsubscribe", e));
console.error("Failed to unsubscribe", e)
);
}; };
}, [waku, address, encryptionKeyPair, unsubscribePrivateMsg]); }, [waku, address, encryptionKeyPair]);
useEffect(() => { useEffect(() => {
if (!waku) return; if (!waku) return;