Skip to content

Commit

Permalink
use plugins async
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Jul 9, 2024
1 parent 5124efb commit 265ad68
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions packages/no-modal/src/noModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
IWeb3AuthCoreOptions,
log,
PLUGIN_NAMESPACES,
PLUGIN_STATUS,
PROJECT_CONFIG_RESPONSE,
storageAvailable,
UserAuthInfo,
Expand Down Expand Up @@ -284,6 +285,10 @@ export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth {
);

this.plugins[plugin.name] = plugin;
if (this.status === ADAPTER_STATUS.CONNECTED && this.connectedAdapterName) {
// web3auth is already connected. can initialize plugins
this.connectToPlugins({ adapter: this.connectedAdapterName });
}
return this;
}

Expand All @@ -300,24 +305,7 @@ export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth {
this.connectedAdapterName = data.adapter;
this.cacheWallet(data.adapter);
log.debug("connected", this.status, this.connectedAdapterName);
Object.values(this.plugins).map(async (plugin) => {
try {
if (!plugin.SUPPORTED_ADAPTERS.includes(data.adapter)) {
return;
}
const { openloginInstance } = this.walletAdapters[this.connectedAdapterName] as OpenloginAdapter;
const { options, sessionId, sessionNamespace } = openloginInstance || {};
await plugin.initWithWeb3Auth(this, options.whiteLabel);
await plugin.connect({ sessionId, sessionNamespace });
} catch (error: unknown) {
// swallow error if connector adapter doesn't supports this plugin.
if ((error as Web3AuthError).code === 5211) {
return;
}
log.error(error);
}
});

this.connectToPlugins(data);
this.emit(ADAPTER_EVENTS.CONNECTED, { ...data } as CONNECTED_EVENT_DATA);
});

Expand Down Expand Up @@ -383,4 +371,25 @@ export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth {
window[this.storage].setItem(ADAPTER_CACHE_KEY, walletName);
this.cachedAdapter = walletName;
}

private connectToPlugins(data: { adapter: WALLET_ADAPTER_TYPE }) {
Object.values(this.plugins).map(async (plugin) => {
try {
if (!plugin.SUPPORTED_ADAPTERS.includes(data.adapter)) {
return;
}
if (plugin.status === PLUGIN_STATUS.CONNECTED) return;
const { openloginInstance } = this.walletAdapters[this.connectedAdapterName] as OpenloginAdapter;
const { options, sessionId, sessionNamespace } = openloginInstance || {};
await plugin.initWithWeb3Auth(this, options.whiteLabel);
await plugin.connect({ sessionId, sessionNamespace });
} catch (error: unknown) {
// swallow error if connector adapter doesn't supports this plugin.
if ((error as Web3AuthError).code === 5211) {
return;
}
log.error(error);
}
});
}
}

0 comments on commit 265ad68

Please sign in to comment.