mirror of
https://github.com/status-im/js-waku-examples.git
synced 2025-01-12 23:14:32 +00:00
fix: remove state for unsubscribe
This commit is contained in:
parent
b6296dd07f
commit
c82244c9e6
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user