@@ -1558,8 +1558,8 @@ export class NetworkController extends BaseController<
1558
1558
}
1559
1559
1560
1560
/**
1561
- * Uses a request for the latest block to gather the following information on
1562
- * the given network:
1561
+ * Uses a request for the latest block to gather and record the following
1562
+ * information on the given network:
1563
1563
*
1564
1564
* - The connectivity status: whether it is available, geo-blocked (Infura
1565
1565
* only), unavailable, or unknown
@@ -1570,7 +1570,7 @@ export class NetworkController extends BaseController<
1570
1570
* If no ID is provided, uses the currently selected network.
1571
1571
* @returns The resulting metadata for the network.
1572
1572
*/
1573
- async #determineNetworkMetadata ( networkClientId : NetworkClientId ) {
1573
+ async #captureNetworkMetadata ( networkClientId : NetworkClientId ) {
1574
1574
// Force TypeScript to use one of the two overloads explicitly
1575
1575
const networkClient = isInfuraNetworkType ( networkClientId )
1576
1576
? this . getNetworkClientById ( networkClientId )
@@ -1631,6 +1631,22 @@ export class NetworkController extends BaseController<
1631
1631
}
1632
1632
}
1633
1633
1634
+ this . update ( ( state ) => {
1635
+ if ( state . networksMetadata [ networkClientId ] === undefined ) {
1636
+ state . networksMetadata [ networkClientId ] = {
1637
+ status : NetworkStatus . Unknown ,
1638
+ EIPS : { } ,
1639
+ } ;
1640
+ }
1641
+ const meta = state . networksMetadata [ networkClientId ] ;
1642
+ meta . status = networkStatus ;
1643
+ if ( isEIP1559Compatible === undefined ) {
1644
+ delete meta . EIPS [ 1559 ] ;
1645
+ } else {
1646
+ meta . EIPS [ 1559 ] = isEIP1559Compatible ;
1647
+ }
1648
+ } ) ;
1649
+
1634
1650
return { isInfura, networkStatus, isEIP1559Compatible } ;
1635
1651
}
1636
1652
@@ -1686,13 +1702,7 @@ export class NetworkController extends BaseController<
1686
1702
* @param networkClientId - The ID of the network client to inspect.
1687
1703
*/
1688
1704
async #lookupGivenNetwork( networkClientId : NetworkClientId ) {
1689
- const { networkStatus, isEIP1559Compatible } =
1690
- await this . #determineNetworkMetadata( networkClientId ) ;
1691
- this . #updateMetadataForNetwork(
1692
- networkClientId ,
1693
- networkStatus ,
1694
- isEIP1559Compatible ,
1695
- ) ;
1705
+ await this . #captureNetworkMetadata( networkClientId ) ;
1696
1706
}
1697
1707
1698
1708
/**
@@ -1713,113 +1723,30 @@ export class NetworkController extends BaseController<
1713
1723
return ;
1714
1724
}
1715
1725
1716
- let networkChanged = false ;
1717
- const listener = ( ) => {
1718
- networkChanged = true ;
1719
- try {
1720
- this . messagingSystem . unsubscribe (
1721
- 'NetworkController:networkDidChange' ,
1722
- listener ,
1723
- ) ;
1724
- } catch ( error ) {
1725
- // In theory, this `catch` should not be necessary given that this error
1726
- // would occur "inside" of the call to `#determineEIP1559Compatibility`
1727
- // below and so it should be caught by the `try`/`catch` below (it is
1728
- // impossible to reproduce in tests for that reason). However, somehow
1729
- // it occurs within Mobile and so we have to add our own `try`/`catch`
1730
- // here.
1731
- /* istanbul ignore next */
1732
- if (
1733
- ! ( error instanceof Error ) ||
1734
- error . message !==
1735
- 'Subscription not found for event: NetworkController:networkDidChange'
1736
- ) {
1737
- // Again, this error should not happen and is impossible to reproduce
1738
- // in tests.
1739
- /* istanbul ignore next */
1740
- throw error ;
1741
- }
1742
- }
1743
- } ;
1744
- this . messagingSystem . subscribe (
1745
- 'NetworkController:networkDidChange' ,
1746
- listener ,
1747
- ) ;
1748
-
1749
- const { isInfura, networkStatus, isEIP1559Compatible } =
1750
- await this . #determineNetworkMetadata( this . state . selectedNetworkClientId ) ;
1751
-
1752
- if ( networkChanged ) {
1753
- // If the network has changed, then `lookupNetwork` either has been or is
1754
- // in the process of being called, so we don't need to go further.
1755
- return ;
1756
- }
1726
+ // Capture up front in case the network is switched while awaiting the
1727
+ // network request
1728
+ const { selectedNetworkClientId } = this . state ;
1757
1729
1758
- try {
1759
- this . messagingSystem . unsubscribe (
1760
- 'NetworkController:networkDidChange' ,
1761
- listener ,
1762
- ) ;
1763
- } catch ( error ) {
1764
- if (
1765
- ! ( error instanceof Error ) ||
1766
- error . message !==
1767
- 'Subscription not found for event: NetworkController:networkDidChange'
1768
- ) {
1769
- throw error ;
1770
- }
1771
- }
1772
-
1773
- this . #updateMetadataForNetwork(
1730
+ const { isInfura, networkStatus } = await this . #captureNetworkMetadata(
1774
1731
this . state . selectedNetworkClientId ,
1775
- networkStatus ,
1776
- isEIP1559Compatible ,
1777
1732
) ;
1778
1733
1779
- if ( isInfura ) {
1780
- if ( networkStatus === NetworkStatus . Available ) {
1734
+ if ( selectedNetworkClientId === this . state . selectedNetworkClientId ) {
1735
+ if ( isInfura ) {
1736
+ if ( networkStatus === NetworkStatus . Available ) {
1737
+ this . messagingSystem . publish ( 'NetworkController:infuraIsUnblocked' ) ;
1738
+ } else if ( networkStatus === NetworkStatus . Blocked ) {
1739
+ this . messagingSystem . publish ( 'NetworkController:infuraIsBlocked' ) ;
1740
+ }
1741
+ } else {
1742
+ // Always publish infuraIsUnblocked regardless of network status to
1743
+ // prevent consumers from being stuck in a blocked state if they were
1744
+ // previously connected to an Infura network that was blocked
1781
1745
this . messagingSystem . publish ( 'NetworkController:infuraIsUnblocked' ) ;
1782
- } else if ( networkStatus === NetworkStatus . Blocked ) {
1783
- this . messagingSystem . publish ( 'NetworkController:infuraIsBlocked' ) ;
1784
1746
}
1785
- } else {
1786
- // Always publish infuraIsUnblocked regardless of network status to
1787
- // prevent consumers from being stuck in a blocked state if they were
1788
- // previously connected to an Infura network that was blocked
1789
- this . messagingSystem . publish ( 'NetworkController:infuraIsUnblocked' ) ;
1790
1747
}
1791
1748
}
1792
1749
1793
- /**
1794
- * Updates the metadata for the given network in state.
1795
- *
1796
- * @param networkClientId - The associated network client ID.
1797
- * @param networkStatus - The network status to store in state.
1798
- * @param isEIP1559Compatible - The EIP-1559 compatibility status to
1799
- * store in state.
1800
- */
1801
- #updateMetadataForNetwork(
1802
- networkClientId : NetworkClientId ,
1803
- networkStatus : NetworkStatus ,
1804
- isEIP1559Compatible : boolean | undefined ,
1805
- ) {
1806
- this . update ( ( state ) => {
1807
- if ( state . networksMetadata [ networkClientId ] === undefined ) {
1808
- state . networksMetadata [ networkClientId ] = {
1809
- status : NetworkStatus . Unknown ,
1810
- EIPS : { } ,
1811
- } ;
1812
- }
1813
- const meta = state . networksMetadata [ networkClientId ] ;
1814
- meta . status = networkStatus ;
1815
- if ( isEIP1559Compatible === undefined ) {
1816
- delete meta . EIPS [ 1559 ] ;
1817
- } else {
1818
- meta . EIPS [ 1559 ] = isEIP1559Compatible ;
1819
- }
1820
- } ) ;
1821
- }
1822
-
1823
1750
/**
1824
1751
* Convenience method to update provider network type settings.
1825
1752
*
0 commit comments