Skip to content

Commit

Permalink
fix: disconnect eagerly (#3255)
Browse files Browse the repository at this point in the history
* fix: eager disconnect

* chore: update tests
  • Loading branch information
jxom authored Nov 28, 2023
1 parent 72c85fc commit b8d7563
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"typescript.preferences.importModuleSpecifier": "shortest",
"typescript.tsdk": "node_modules/typescript/lib",
"editor.codeActionsOnSave": {
"quickfix.biome": true,
"source.organizeImports.biome": true
"quickfix.biome": "explicit",
"source.organizeImports.biome": "explicit"
},
"[json]": {
"editor.defaultFormatter": "biomejs.biome"
Expand Down
22 changes: 0 additions & 22 deletions packages/core/src/actions/disconnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,6 @@ test('parameters: connector', async () => {
expect(config.state.status).toEqual('disconnected')
})

test('behavior: not connected to connector', async () => {
await expect(disconnect(config)).rejects.toMatchInlineSnapshot(
`
[ConnectorNotFoundError: Connector not found.
Version: @wagmi/core@x.y.z]
`,
)
})

test('behavior: connector passed not connected', async () => {
await connect(config, { connector })
const connector_ = config._internal.connectors.setup(mock({ accounts }))
await expect(
disconnect(config, { connector: connector_ }),
).rejects.toMatchInlineSnapshot(`
[ConnectorNotConnectedError: Connector not connected.
Version: @wagmi/core@x.y.z]
`)
})

test('behavior: uses next connector on disconnect', async () => {
const connector_ = config._internal.connectors.setup(mock({ accounts }))
await connect(config, { connector: connector_ })
Expand Down
28 changes: 11 additions & 17 deletions packages/core/src/actions/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {
type Config,
type Connection,
type Connector,
} from '../createConfig.js'
import type { Config, Connection, Connector } from '../createConfig.js'
import type { BaseErrorType, ErrorType } from '../errors/base.js'
import {
ConnectorNotConnectedError,
type ConnectorNotConnectedErrorType,
ConnectorNotFoundError,
type ConnectorNotFoundErrorType,
import type {
ConnectorNotConnectedErrorType,
ConnectorNotFoundErrorType,
} from '../errors/config.js'
import type { ConnectorParameter } from '../types/properties.js'

Expand Down Expand Up @@ -36,16 +30,16 @@ export async function disconnect(
connector = connection?.connector
}

if (!connector) throw new ConnectorNotFoundError()
const connections = config.state.connections
if (!connections.has(connector.uid)) throw new ConnectorNotConnectedError()

await connector.disconnect()
connector.emitter.off('change', config._internal.events.change)
connector.emitter.off('disconnect', config._internal.events.disconnect)
connector.emitter.on('connect', config._internal.events.connect)
if (connector) {
await connector.disconnect()
connector.emitter.off('change', config._internal.events.change)
connector.emitter.off('disconnect', config._internal.events.disconnect)
connector.emitter.on('connect', config._internal.events.connect)

connections.delete(connector.uid)
connections.delete(connector.uid)
}

config.setState((x) => {
// if no connections exist, move to disconnected state
Expand Down
12 changes: 12 additions & 0 deletions packages/react/src/hooks/useConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
type ConnectVariables,
connectMutationOptions,
} from '@wagmi/core/query'
import { useEffect } from 'react'

import type { ConfigParameter } from '../types/properties.js'
import type {
Expand Down Expand Up @@ -74,6 +75,17 @@ export function useConnect<
...mutationOptions,
})

// Reset mutation back to an idle state when the connector disconnects.
useEffect(() => {
return config.subscribe(
({ status }) => status,
(status, previousStatus) => {
if (previousStatus !== 'disconnected' && status === 'disconnected')
result.reset()
},
)
}, [config, result])

return {
...result,
connect: mutate,
Expand Down
2 changes: 1 addition & 1 deletion playgrounds/vite-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function Account() {
status: {account.status}
</div>

{account.status === 'connected' && (
{account.status !== 'disconnected' && (
<button type="button" onClick={() => disconnect()}>
Disconnect
</button>
Expand Down

0 comments on commit b8d7563

Please sign in to comment.