From 83e38020743f41f3912dc40393316f3edc8b503f Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Wed, 27 Mar 2024 08:15:34 +1100 Subject: [PATCH] fix: resolves #3683 Co-Authored-By: Magomed Khamidov <53529533+KosmosKey@users.noreply.github.com> --- packages/core/src/createConfig.ts | 4 +++- packages/core/src/hydrate.ts | 8 ++++---- packages/core/src/utils/uniqueBy.ts | 11 +++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 packages/core/src/utils/uniqueBy.ts diff --git a/packages/core/src/createConfig.ts b/packages/core/src/createConfig.ts index c9fede57fe..cacf5372eb 100644 --- a/packages/core/src/createConfig.ts +++ b/packages/core/src/createConfig.ts @@ -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< @@ -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) }) ///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/packages/core/src/hydrate.ts b/packages/core/src/hydrate.ts index 7de4a41177..5a86dfa21f 100644 --- a/packages/core/src/hydrate.ts +++ b/packages/core/src/hydrate.ts @@ -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 @@ -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) diff --git a/packages/core/src/utils/uniqueBy.ts b/packages/core/src/utils/uniqueBy.ts new file mode 100644 index 0000000000..6c84827d7c --- /dev/null +++ b/packages/core/src/utils/uniqueBy.ts @@ -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 +}