@@ -73,14 +73,16 @@ const _getProviderDropdownValue = (plugin: RunTab): string => {
7373}
7474
7575export const setExecutionContext = ( plugin : RunTab , dispatch : React . Dispatch < any > , executionContext : { context : string , fork : string } ) => {
76- if ( executionContext . context === 'walletconnect' ) {
77- setWalletConnectExecutionContext ( plugin , dispatch , executionContext )
78- } else {
79- plugin . blockchain . changeExecutionContext ( executionContext , null , ( alertMsg ) => {
80- plugin . call ( 'notification' , 'toast' , alertMsg )
81- } , async ( ) => {
82- setFinalContext ( plugin , dispatch )
83- } )
76+ if ( executionContext . context && executionContext . context !== plugin . REACT_API . selectExEnv ) {
77+ if ( executionContext . context === 'walletconnect' ) {
78+ setWalletConnectExecutionContext ( plugin , dispatch , executionContext )
79+ } else {
80+ plugin . blockchain . changeExecutionContext ( executionContext , null , ( alertMsg ) => {
81+ plugin . call ( 'notification' , 'toast' , alertMsg )
82+ } , async ( ) => {
83+ setFinalContext ( plugin , dispatch )
84+ } )
85+ }
8486 }
8587}
8688
@@ -236,33 +238,27 @@ export const addFileInternal = async (plugin: RunTab, path: string, content: str
236238}
237239
238240const setWalletConnectExecutionContext = ( plugin : RunTab , dispatch : React . Dispatch < any > , executionContext : { context : string , fork : string } ) => {
239- plugin . call ( 'walletconnect' , 'isWalletConnected' ) . then ( ( isConnected ) => {
240- if ( isConnected ) {
241- plugin . call ( 'walletconnect' , 'openModal' ) . then ( ( ) => {
242- plugin . blockchain . changeExecutionContext ( executionContext , null , ( alertMsg ) => {
243- plugin . call ( 'notification' , 'toast' , alertMsg )
244- } , async ( ) => {
245- setFinalContext ( plugin , dispatch )
246- } )
247- } )
248- } else {
249- plugin . call ( 'walletconnect' , 'openModal' ) . then ( ( ) => {
250- plugin . on ( 'walletconnect' , 'connectionSuccessful' , ( ) => {
251- plugin . blockchain . changeExecutionContext ( executionContext , null , ( alertMsg ) => {
252- plugin . call ( 'notification' , 'toast' , alertMsg )
253- } , async ( ) => {
254- setFinalContext ( plugin , dispatch )
255- } )
256- } )
257- plugin . on ( 'walletconnect' , 'connectionFailed' , ( msg ) => {
258- plugin . call ( 'notification' , 'toast' , msg )
259- } )
260- plugin . on ( 'walletconnect' , 'connectionDisconnected' , ( msg ) => {
261- plugin . call ( 'notification' , 'toast' , msg )
262- // reset to default provider if connection fails
263- setExecutionContext ( plugin , dispatch , { context : plugin . blockchain . defaultPinnedProviders [ 0 ] , fork : null } )
264- } )
241+ plugin . call ( 'walletconnect' , 'openModal' ) . then ( ( ) => {
242+ plugin . on ( 'walletconnect' , 'connectionSuccessful' , ( ) => {
243+ plugin . blockchain . changeExecutionContext ( executionContext , null , ( alertMsg ) => {
244+ plugin . call ( 'notification' , 'toast' , alertMsg )
245+ } , async ( ) => {
246+ setFinalContext ( plugin , dispatch )
265247 } )
266- }
248+ } )
249+ plugin . on ( 'walletconnect' , 'connectionFailed' , ( msg ) => {
250+ plugin . call ( 'notification' , 'toast' , msg )
251+ cleanupWalletConnectEvents ( plugin )
252+ } )
253+ plugin . on ( 'walletconnect' , 'connectionDisconnected' , ( msg ) => {
254+ plugin . call ( 'notification' , 'toast' , msg )
255+ cleanupWalletConnectEvents ( plugin )
256+ } )
267257 } )
268258}
259+
260+ const cleanupWalletConnectEvents = ( plugin : RunTab ) => {
261+ plugin . off ( 'walletconnect' , 'connectionFailed' )
262+ plugin . off ( 'walletconnect' , 'connectionDisconnected' )
263+ plugin . off ( 'walletconnect' , 'connectionSuccessful' )
264+ }
0 commit comments