Skip to content

Commit

Permalink
fix: resolves #3683
Browse files Browse the repository at this point in the history
Co-Authored-By: Magomed Khamidov <53529533+KosmosKey@users.noreply.github.com>
  • Loading branch information
jxom and magiziz committed Mar 26, 2024
1 parent a86aaab commit 83e3802
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { type Storage, createStorage, noopStorage } from './createStorage.js'
import { ChainNotConfiguredError } from './errors/config.js'
import type { Evaluate, ExactPartial, LooseOmit, OneOf } from './types/utils.js'
import { uid } from './utils/uid.js'
import { uniqueBy } from './utils/uniqueBy.js'
import { version } from './version.js'

export type CreateConfigParameters<
Expand Down Expand Up @@ -284,7 +285,8 @@ export function createConfig<
newConnectors.push(connector)
}

connectors.setState((x) => [...x, ...newConnectors], true)
if (!store.persist.hasHydrated()) return
connectors.setState((x) => uniqueBy([...x, ...newConnectors], 'id'), true)
})

/////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/hydrate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { reconnect } from './actions/reconnect.js'
import type { Config, State } from './createConfig.js'
import { uniqueBy } from './utils/uniqueBy.js'

type HydrateParameters = {
initialState?: State | undefined
Expand All @@ -24,10 +25,9 @@ export function hydrate(config: Config, parameters: HydrateParameters) {
?.getProviders()
.map(config._internal.connectors.providerDetailToConnector)
.map(config._internal.connectors.setup)
config._internal.connectors.setState((connectors) => [
...connectors,
...(mipdConnectors ?? []),
])
config._internal.connectors.setState((connectors) =>
uniqueBy([...connectors, ...(mipdConnectors ?? [])], 'id'),
)
}

if (reconnectOnMount) reconnect(config)
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/utils/uniqueBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function uniqueBy<
key extends string,
items extends { [_ in key]: string }[],
>(items: items, key: key): items {
const filtered = [] as unknown as items

for (const item of items)
if (!filtered.some((x) => x[key] === item[key])) filtered.push(item)

return filtered
}

0 comments on commit 83e3802

Please sign in to comment.