fix: ensure session can be referenced when creating event handlers

This commit is contained in:
Ben Kremer 2022-02-04 12:26:22 +01:00
parent 004717fce1
commit dc58ed1266

View File

@ -26,7 +26,6 @@ import {
DEFAULT_PROJECT_ID, DEFAULT_PROJECT_ID,
DEFAULT_RELAY_URL, DEFAULT_RELAY_URL,
DEFAULT_TEST_CHAINS, DEFAULT_TEST_CHAINS,
DEFAULT_CHAINS,
} from "./constants"; } from "./constants";
import { import {
apiGetAccountAssets, apiGetAccountAssets,
@ -142,9 +141,8 @@ export default function App() {
const openRequestModal = () => setModal("request"); const openRequestModal = () => setModal("request");
const init = async () => { const init = async () => {
setLoading(true);
try { try {
setLoading(true);
await loadChainData(); await loadChainData();
const _client = await Client.init({ const _client = await Client.init({
@ -153,7 +151,7 @@ export default function App() {
projectId: DEFAULT_PROJECT_ID, projectId: DEFAULT_PROJECT_ID,
}); });
setClient(_client); setClient(_client);
subscribeToEvents(_client); await subscribeToEvents(_client);
await checkPersistedState(_client); await checkPersistedState(_client);
} catch (err) { } catch (err) {
throw err; throw err;
@ -166,11 +164,17 @@ export default function App() {
init(); init();
}, []); }, []);
const subscribeToEvents = (_client: Client) => { const subscribeToEvents = async (_client: Client) => {
if (typeof _client === "undefined") { if (typeof _client === "undefined") {
throw new Error("WalletConnect is not initialized"); throw new Error("WalletConnect is not initialized");
} }
let _session = {} as SessionTypes.Settled;
if (_client.session.topics.length) {
_session = await _client.session.get(_client.session.topics[0]);
}
_client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => { _client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => {
const { uri } = proposal.signal.params; const { uri } = proposal.signal.params;
setUri(uri); setUri(uri);
@ -186,10 +190,11 @@ export default function App() {
}); });
_client.on(CLIENT_EVENTS.session.deleted, (deletedSession: SessionTypes.Settled) => { _client.on(CLIENT_EVENTS.session.deleted, (deletedSession: SessionTypes.Settled) => {
if (deletedSession.topic !== session?.topic) return; if (deletedSession.topic !== _session?.topic) return;
console.log("EVENT", "session_deleted"); console.log("EVENT", "session_deleted");
// TODO: // TODO:
// this.resetApp(); // this.resetApp();
window.location.reload();
}); });
}; };