Skip to content

Commit 09c744b

Browse files
committed
refactor code
1 parent 9869437 commit 09c744b

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

packages/modal/src/modalManager.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,18 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
267267
if (!connectorName) return;
268268
try {
269269
const connector = this.getConnector(connectorName);
270+
// skip if connector is already initialized
271+
if (connector.status !== CONNECTOR_STATUS.NOT_READY) return;
272+
270273
// only initialize a external connectors here if it is a cached connector.
271-
if (this.cachedConnector !== connectorName && connector.type === CONNECTOR_CATEGORY.EXTERNAL) {
272-
return;
273-
}
274+
if (this.cachedConnector !== connectorName && connector.type === CONNECTOR_CATEGORY.EXTERNAL) return;
275+
274276
// in-app wallets or cached wallet (being connected or already connected) are initialized first.
275277
// if connector is configured then only initialize in app or cached connector.
276278
// external wallets are initialized on INIT_EXTERNAL_WALLET event.
277-
if (connector.status !== CONNECTOR_STATUS.NOT_READY) return;
278279
this.subscribeToConnectorEvents(connector);
279280
await connector.init({ autoConnect: this.cachedConnector === connectorName, chainId: this.currentChain.chainId });
281+
280282
// note: not adding cachedWallet to modal if it is external wallet.
281283
// adding it later if no in-app wallets are available.
282284
if (connector.type === CONNECTOR_CATEGORY.IN_APP) {

packages/no-modal/src/noModal.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
5353

5454
protected commonJRPCProvider: CommonJRPCProvider | null = null;
5555

56-
protected multiInjectedProviderDiscovery: boolean = true;
57-
5856
protected connectorStore = createStore<{ connectors: IConnector<unknown>[]; setConnectors: (connectors: IConnector<unknown>[]) => void }>(
5957
(set) => ({
6058
connectors: [] as IConnector<unknown>[],
@@ -92,16 +90,12 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
9290
...chain,
9391
})),
9492
};
95-
this.multiInjectedProviderDiscovery = options.multiInjectedProviderDiscovery ?? true;
9693

9794
// handle cached current chain
9895
this.cachedCurrentChainId = storageAvailable(this.storage) ? window[this.storage].getItem(CURRENT_CHAIN_CACHE_KEY) : null;
9996
// use corrected chains from coreOptions
10097
const cachedChain = this.cachedCurrentChainId ? this.coreOptions.chains.find((chain) => chain.chainId === this.cachedCurrentChainId) : null;
10198
this.currentChain = cachedChain || this.coreOptions.chains[0]; // use first chain in list as default chain config
102-
103-
// TODO: do we need this ?
104-
this.subscribeToConnectorEvents = this.subscribeToConnectorEvents.bind(this);
10599
}
106100

107101
get connected(): boolean {
@@ -265,9 +259,11 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
265259
const connectorFns = [...(this.coreOptions.connectors || []), authConnector()];
266260
const config = { projectConfig, coreOptions: this.coreOptions };
267261

268-
// add injected wallets if multi injected provider discovery is enabled
269-
if (this.multiInjectedProviderDiscovery) {
262+
// add injected connectors
263+
const isMipdEnabled = this.coreOptions.multiInjectedProviderDiscovery ?? true;
264+
if (isMipdEnabled) {
270265
const chainNamespaces = new Set(this.coreOptions.chains.map((chain) => chain.chainNamespace));
266+
// Solana chains
271267
if (chainNamespaces.has(CHAIN_NAMESPACES.SOLANA)) {
272268
const { createSolanaMipd, hasSolanaWalletStandardFeatures, walletStandardConnector } = await import("@/core/default-solana-connector");
273269
const solanaMipd = createSolanaMipd();
@@ -283,6 +279,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
283279
.map(walletStandardConnector)
284280
);
285281
}
282+
// EVM chains
286283
if (chainNamespaces.has(CHAIN_NAMESPACES.EIP155)) {
287284
const { createMipd, injectedEvmConnector } = await import("@/core/default-evm-connector");
288285
const evmMipd = createMipd();
@@ -294,11 +291,10 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
294291
connectorFns.push(...evmMipd.getProviders().map(injectedEvmConnector));
295292
}
296293

297-
// add wallet connect v2 connector if enabled
298-
const { wallet_connect_enabled: walletConnectEnabled, wallet_connect_project_id: walletConnectProjectId } = projectConfig;
294+
// add WalletConnectV2 connector if enabled
299295
if (
300-
walletConnectEnabled &&
301-
walletConnectProjectId &&
296+
projectConfig.wallet_connect_enabled &&
297+
projectConfig.wallet_connect_project_id &&
302298
(chainNamespaces.has(CHAIN_NAMESPACES.SOLANA) || chainNamespaces.has(CHAIN_NAMESPACES.EIP155))
303299
) {
304300
const { walletConnectV2Connector } = await import("@/core/wallet-connect-v2-connector");

0 commit comments

Comments
 (0)