Skip to content

Commit

Permalink
♻️ Refine session restore and disconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jun 26, 2023
1 parent 2b55d8f commit 947e544
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
21 changes: 12 additions & 9 deletions src/utils/cosmostation-mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ export async function initCosmostationMobile(
});
client = wcConnector;
// TODO: allow selecting sessions
const currentSessions = wcConnector.session.getAll();
const reuseSession = currentSessions
.reverse()
.find((session: SessionTypes.Struct) => {
return session.peer.metadata.name.includes('Cosmostation');
});
if (reuseSession) {
session = reuseSession;
if (!session) {
const currentSessions = wcConnector.session.getAll();
const reuseSession = currentSessions
.reverse()
.find((session: SessionTypes.Struct) => {
return session.peer.metadata.name.includes('Cosmostation');
});
if (reuseSession) {
session = reuseSession;
}
}
let accounts: AccountData[] = [];

Expand Down Expand Up @@ -152,12 +154,13 @@ export const checkIsInCosmostationMobileInAppBrowser = () =>
export async function onCosmostationMobileDisconnect(topic = session?.topic) {
if (topic) {
if (!client) return;
client.disconnect({
await client.disconnect({
topic,
reason: {
code: 6000,
message: 'USER_DISCONNECTED',
},
});
session = null;
}
}
21 changes: 12 additions & 9 deletions src/utils/keplr-mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ export async function initKeplrMobile(
});
client = wcConnector;
// TODO: allow selecting sessions
const currentSessions = wcConnector.session.getAll();
const reuseSession = currentSessions
.reverse()
.find((session: SessionTypes.Struct) => {
return session.peer.metadata.name.includes('Keplr');
});
if (reuseSession) {
session = reuseSession;
if (!session) {
const currentSessions = wcConnector.session.getAll();
const reuseSession = currentSessions
.reverse()
.find((session: SessionTypes.Struct) => {
return session.peer.metadata.name.includes('Keplr');
});
if (reuseSession) {
session = reuseSession;
}
}
let accounts: AccountData[] = [];

Expand Down Expand Up @@ -149,12 +151,13 @@ export async function initKeplrMobile(
export async function onKeplrMobileDisconnect(topic = session?.topic) {
if (topic) {
if (!client) return;
client.disconnect({
await client.disconnect({
topic,
reason: {
code: 6000,
message: 'USER_DISCONNECTED',
},
});
session = null;
}
}
15 changes: 9 additions & 6 deletions src/utils/wallet-connect-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ export async function initWalletConnectV2Connector(
projectId: options.walletConnectProjectId,
metadata: options.walletConnectMetadata,
});
const lastKeyIndex = wcConnector.session.getAll().length - 1;
// TODO: allow selecting sessions
if (lastKeyIndex > -1) {
const lastSession = wcConnector.session.getAll()[lastKeyIndex];
if (lastSession) session = lastSession;
if (!session) {
const lastKeyIndex = wcConnector.session.getAll().length - 1;
// TODO: allow selecting sessions
if (lastKeyIndex > -1) {
const lastSession = wcConnector.session.getAll()[lastKeyIndex];
if (lastSession) session = lastSession;
}
}
let accounts: AccountData[] = [];

Expand Down Expand Up @@ -237,12 +239,13 @@ export async function listenWalletConnectV2StoreChange(
export async function onWalletConnectV2Disconnect(topic = session?.topic) {
if (topic) {
if (!client) return;
client.disconnect({
await client.disconnect({
topic,
reason: {
code: 6000,
message: 'USER_DISCONNECTED',
},
});
session = null;
}
}

0 comments on commit 947e544

Please sign in to comment.