Skip to content

Commit

Permalink
Add a session_delete handler
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Sep 21, 2023
1 parent f05fc0b commit 3303802
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/components/walletconnect/SessionManager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ const SessionManager = () => {
const { safe, safeAddress } = useSafeInfo()
const { chainId } = safe
const { walletConnect, error: walletConnectError } = useContext(WalletConnectContext)
const [sessions, setSessions] = useState<Record<string, SessionTypes.Struct>>(walletConnect.getActiveSessions() || {})
const [sessions, setSessions] = useState<Record<string, SessionTypes.Struct>>(walletConnect.getActiveSessions())
const [proposal, setProposal] = useState<Web3WalletTypes.SessionProposal>()
const [error, setError] = useState<Error>()

// Subscribe to session proposals
useEffect(() => {
return walletConnect.addOnSessionPropose(async (event) => {
setProposal(event)
return walletConnect.onSessionPropose(setProposal)
}, [walletConnect])

// Subscribe to session deletes
useEffect(() => {
return walletConnect.onSessionDelete(() => {
setSessions(walletConnect.getActiveSessions())
})
}, [walletConnect])

Expand All @@ -37,8 +42,7 @@ const SessionManager = () => {
}

setProposal(undefined)
// Update sessions
setSessions(walletConnect.getActiveSessions() || {})
setSessions(walletConnect.getActiveSessions())
}, [proposal, walletConnect])

// On session reject
Expand All @@ -59,7 +63,6 @@ const SessionManager = () => {
const onDisconnect = async (session: SessionTypes.Struct) => {
try {
await walletConnect.disconnectSession(session)
setSessions(walletConnect.getActiveSessions() || {})
} catch (error) {
setError(asError(error))
}
Expand Down
13 changes: 12 additions & 1 deletion src/services/walletconnect/WalletConnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class WalletConnectWallet {
/**
* Subscribe to session proposals
*/
public addOnSessionPropose(onSessionPropose: (e: Web3WalletTypes.SessionProposal) => void) {
public onSessionPropose(onSessionPropose: (e: Web3WalletTypes.SessionProposal) => void) {
// Subscribe to the session proposal event
this.web3Wallet?.on('session_proposal', onSessionPropose)

Expand All @@ -154,6 +154,17 @@ class WalletConnectWallet {
}
}

/**
* Subscribe to session delete
*/
public onSessionDelete(handler: () => void) {
this.web3Wallet?.on('session_delete', handler)

return () => {
this.web3Wallet?.off('session_delete', handler)
}
}

/**
* Disconnect a session
*/
Expand Down

0 comments on commit 3303802

Please sign in to comment.