-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathhydrate.ts
62 lines (57 loc) · 2.14 KB
/
hydrate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { reconnect } from './actions/reconnect.js'
import type { Config, State } from './createConfig.js'
type HydrateParameters = {
initialState?: State | undefined
reconnectOnMount?: boolean | undefined
}
export function hydrate(config: Config, parameters: HydrateParameters) {
const { initialState, reconnectOnMount } = parameters
if (initialState && !config._internal.store.persist.hasHydrated())
config.setState({
...initialState,
chainId: config.chains.some((x) => x.id === initialState.chainId)
? initialState.chainId
: config.chains[0].id,
connections: reconnectOnMount ? initialState.connections : new Map(),
status: reconnectOnMount ? 'reconnecting' : 'disconnected',
})
return {
async onMount() {
if (config._internal.ssr) {
await config._internal.store.persist.rehydrate()
if (config._internal.mipd) {
config._internal.connectors.setState((connectors) => {
const rdnsSet = new Set<string>()
for (const connector of connectors ?? []) {
if (connector.rdns) {
const rdnsValues = Array.isArray(connector.rdns)
? connector.rdns
: [connector.rdns]
for (const rdns of rdnsValues) {
rdnsSet.add(rdns)
}
}
}
const mipdConnectors = []
const providers = config._internal.mipd?.getProviders() ?? []
for (const provider of providers) {
if (rdnsSet.has(provider.info.rdns)) continue
const connectorFn =
config._internal.connectors.providerDetailToConnector(provider)
const connector = config._internal.connectors.setup(connectorFn)
mipdConnectors.push(connector)
}
return [...connectors, ...mipdConnectors]
})
}
}
if (reconnectOnMount) reconnect(config)
else if (config.storage)
// Reset connections that may have been hydrated from storage.
config.setState((x) => ({
...x,
connections: new Map(),
}))
},
}
}