Session Lifecycle
Keep your application UI synchronized with D9 Wallet: display the active account, abort stale operations on account switches, and clean up cleanly on disconnect.
Prerequisites: The singleton walletRuntime from Quickstart, initialized once at the application root.
1. Subscribe to connection state
Section titled “1. Subscribe to connection state”Subscribe to connector.changes (or use the Promise-based /web facade in Connector API):
import { useEffect, useState } from "react";import { Effect, Fiber, Stream } from "effect";import { D9DAppConnector, type D9ConnectorSnapshot } from "@d9-network/dapp-connector";import { walletRuntime } from "./wallet";
export function useD9Wallet() { const [snapshot, setSnapshot] = useState<D9ConnectorSnapshot | null>(null);
useEffect(() => { const fiber = walletRuntime.runFork( Effect.gen(function* () { const connector = yield* D9DAppConnector; yield* Stream.runForEach(connector.changes, (next) => Effect.sync(() => setSnapshot(next)), ); }), );
return () => { void walletRuntime.runPromise(Fiber.interrupt(fiber)); }; }, []);
return { status: snapshot?.status ?? "disconnected", transport: snapshot?.transport ?? null, accounts: snapshot?.accounts ?? [], selectedAccount: snapshot?.accounts[0] ?? null, };}Each emitted snapshot contains:
| Field | Value |
|---|---|
status |
'disconnected' | 'connecting' | 'connected' | 'error' |
transport |
'papi' | 'walletconnect' | null |
accounts |
Read-only array of approved D9ConnectorAccount entries |
session |
Active session handle (WalletConnect only) |
error |
Populated when a connection error occurs |
2. React to account changes
Section titled “2. React to account changes”Whenever the user switches accounts or revokes authorization, a new snapshot is emitted (along with an accountsChanged event):
- Discard any pending transaction drafts immediately.
- Never broadcast a signature produced by account A after account B becomes active.
3. Re-verify authorization after every prompt
Section titled “3. Re-verify authorization after every prompt”A user can switch accounts while a wallet approval dialog is open. Verify account authorization both before and after awaiting the signature:
import { Effect } from "effect";import { D9DAppConnector } from "@d9-network/dapp-connector";import { walletRuntime } from "./wallet";
async function executeAuthorizedAction(selectedAddress: string) { return walletRuntime.runPromise( Effect.gen(function* () { const connector = yield* D9DAppConnector; const initialAccounts = yield* connector.getAccounts; const account = initialAccounts.find((a) => a.address === selectedAddress); if (!account) { throw new Error("Selected account is no longer authorized by the wallet"); }
const result = yield* connector.signMessage({ address: account.address, message: "Confirm action", });
// Re-check authorization in case the account changed during approval const activeAccounts = yield* connector.getAccounts; if (!activeAccounts.some((a) => a.address === account.address)) { throw new Error("Account authorization changed while awaiting signature"); }
return result; }), );}4. Handle cancellations and disconnects
Section titled “4. Handle cancellations and disconnects”- If the user declines a wallet prompt, return cleanly to the idle state—never re-prompt in a loop.
- On Log out or Disconnect, call
connector.disconnect. It clears persisted session data from browser storage and notifies the wallet. - Disconnecting only prevents new signing requests. Transactions already broadcast cannot be cancelled; continue tracking them by hash.
Verify
Section titled “Verify”- Switch accounts in D9 Wallet and confirm
accountsupdates immediately in your UI. - Click Disconnect and confirm
statustransitions to'disconnected'.
