@@ -13,7 +13,23 @@ export function EnvironmentUI(props: EnvironmentProps) {
13
13
const vmStateName = useRef ( '' )
14
14
const providers = props . providers . providerList
15
15
const [ isSwitching , setIsSwitching ] = useState ( false )
16
- const watchdogTimer = useRef < NodeJS . Timeout | null > ( null )
16
+
17
+ const switchTimeoutRef = useRef < NodeJS . Timeout | null > ( null )
18
+
19
+ useEffect ( ( ) => {
20
+ if ( switchTimeoutRef . current ) {
21
+ clearTimeout ( switchTimeoutRef . current )
22
+ }
23
+ setIsSwitching ( false )
24
+ } , [ props . selectedEnv ] )
25
+
26
+ useEffect ( ( ) => {
27
+ return ( ) => {
28
+ if ( switchTimeoutRef . current ) {
29
+ clearTimeout ( switchTimeoutRef . current )
30
+ }
31
+ }
32
+ } , [ ] )
17
33
18
34
const remixVMs = providers . filter ( p => p . config . isVM )
19
35
const injectedProviders = providers . filter ( p => p . config . isInjected )
@@ -25,43 +41,22 @@ export function EnvironmentUI(props: EnvironmentProps) {
25
41
const walletConnect = providers . find ( p => p . name === 'walletconnect' || p . name === 'walletConnect' )
26
42
const httpProvider = providers . find ( p => p . name === 'basic-http-provider' || p . name === 'web3Provider' || p . name === 'basicHttpProvider' )
27
43
28
- const stopSwitching = ( ) => {
29
- if ( watchdogTimer . current ) {
30
- clearTimeout ( watchdogTimer . current )
31
- watchdogTimer . current = null
32
- }
33
- setIsSwitching ( false )
34
- }
44
+ const handleChangeExEnv = ( env : string ) => {
45
+ if ( props . selectedEnv === env || isSwitching ) return
35
46
36
- useEffect ( ( ) => {
37
- if ( isSwitching ) {
38
- stopSwitching ( )
47
+ if ( switchTimeoutRef . current ) {
48
+ clearTimeout ( switchTimeoutRef . current )
39
49
}
40
- } , [ props . selectedEnv ] )
41
-
42
- const handleChangeExEnv = async ( env : string ) => {
43
- if ( props . selectedEnv === env || isSwitching ) return
44
50
45
51
setIsSwitching ( true )
46
52
47
- watchdogTimer . current = setTimeout ( ( ) => {
48
- stopSwitching ( )
49
- } , 10000 )
50
-
51
- const provider = providers . find ( ( exEnv ) => exEnv . name === env )
53
+ switchTimeoutRef . current = setTimeout ( ( ) => {
54
+ setIsSwitching ( false )
55
+ } , 8000 )
52
56
53
- if ( provider && typeof provider . init === 'function' ) {
54
- try {
55
- await provider . init ( )
56
- props . setExecutionContext ( { context : env } )
57
- } catch ( e ) {
58
- stopSwitching ( )
59
- }
60
- } else {
61
- setTimeout ( ( ) => {
62
- props . setExecutionContext ( { context : env } )
63
- } , 0 )
64
- }
57
+ const provider = props . providers . providerList . find ( ( exEnv ) => exEnv . name === env )
58
+ const context = provider . name
59
+ props . setExecutionContext ( { context } )
65
60
}
66
61
67
62
const currentProvider = props . providers . providerList . find ( ( exEnv ) => exEnv . name === props . selectedEnv )
0 commit comments