@@ -40,61 +40,52 @@ export default class NetworksService extends Store {
4040 constructor ( ) {
4141 super ( 'networks' , 'index.json' , JSON . stringify ( presetNetworks ) )
4242
43- this . getAll ( ) . then ( currentNetworkList => {
44- if ( currentNetworkList ) {
45- NetworkListSubject . next ( {
46- currentNetworkList,
47- } )
48- Promise . all ( currentNetworkList . map ( n => {
49- if ( n . type == NetworkType . Default ) {
50- return n
51- } else {
52- const core = new Core ( n . remote )
53- return Promise . all ( [
54- core . rpc . getBlockchainInfo ( ) ,
55- core . rpc . getBlockHash ( '0x0' )
56- ] ) . then ( ( [ info , genesisHash ] ) => ( {
57- ...n ,
58- chain : info . chain ,
59- genesisHash
60- } ) )
61- }
62- } ) ) . then ( networkList => {
63- this . updateAll ( networkList )
64- } ) . catch ( ( err : Error ) => {
65- logger . error ( err )
66- } )
43+ const currentNetworkList = this . getAll ( )
44+ NetworkListSubject . next ( { currentNetworkList } )
45+
46+ Promise . all ( currentNetworkList . map ( n => {
47+ if ( n . type == NetworkType . Default ) {
48+ return n
49+ } else {
50+ const core = new Core ( n . remote )
51+ return Promise . all ( [
52+ core . rpc . getBlockchainInfo ( ) ,
53+ core . rpc . getBlockHash ( '0x0' )
54+ ] ) . then ( ( [ info , genesisHash ] ) => ( {
55+ ...n ,
56+ chain : info . chain ,
57+ genesisHash
58+ } ) )
6759 }
60+ } ) ) . then ( networkList => {
61+ this . updateAll ( networkList )
62+ } ) . catch ( ( err : Error ) => {
63+ logger . error ( err )
6864 } )
6965
70- this . getCurrentID ( ) . then ( currentNetworkID => {
71- if ( currentNetworkID ) {
72- CurrentNetworkIDSubject . next ( { currentNetworkID } )
73- this . get ( currentNetworkID ) . then ( network => {
74- if ( network ) {
75- networkSwitchSubject . next ( network )
76- }
77- } )
78- }
79- } )
66+ const currentNetwork = this . getCurrent ( )
67+ if ( currentNetwork ) {
68+ CurrentNetworkIDSubject . next ( { currentNetworkID : currentNetwork . id } )
69+ networkSwitchSubject . next ( currentNetwork )
70+ }
8071
8172 this . on ( NetworksKey . List , async ( _ , currentNetworkList : NetworkWithID [ ] = [ ] ) => {
8273 NetworkListSubject . next ( { currentNetworkList } )
8374
84- const currentID = await this . getCurrentID ( )
75+ const currentID = this . getCurrentID ( )
8576 if ( currentNetworkList . find ( network => network . id === currentID ) ) {
8677 return
8778 }
8879
89- const defaultNetwork = await this . defaultOne ( )
80+ const defaultNetwork = this . defaultOne ( )
9081 if ( ! defaultNetwork ) {
9182 throw new LackOfDefaultNetwork ( )
9283 }
9384 this . activate ( defaultNetwork . id )
9485 } )
9586
9687 this . on ( NetworksKey . Current , async ( _ , currentNetworkID : NetworkID ) => {
97- const currentNetwork = await this . get ( currentNetworkID )
88+ const currentNetwork = this . get ( currentNetworkID )
9889 if ( ! currentNetwork ) {
9990 throw new NetworkNotFound ( currentNetworkID )
10091 }
@@ -103,19 +94,22 @@ export default class NetworksService extends Store {
10394 } )
10495 }
10596
106- public getAll = async ( ) => {
107- const list = await this . read < NetworkWithID [ ] > ( NetworksKey . List )
97+ public getAll = ( ) => {
98+ const list = this . readSync < NetworkWithID [ ] > ( NetworksKey . List )
10899 return list || presetNetworks . networks
109100 }
110101
111- @Validate
112- public async get ( @Required id : NetworkID ) {
113- const list = await this . getAll ( )
102+ public getCurrent ( ) : NetworkWithID {
103+ const currentID = this . getCurrentID ( )
104+ return this . get ( currentID ! ) ! // Should always have at least one network
105+ }
106+
107+ public get ( @Required id : NetworkID ) {
108+ const list = this . getAll ( )
114109 return list . find ( item => item . id === id ) || null
115110 }
116111
117- @Validate
118- public async updateAll ( @Required networks : NetworkWithID [ ] ) {
112+ public updateAll ( @Required networks : NetworkWithID [ ] ) {
119113 if ( ! Array . isArray ( networks ) ) {
120114 throw new InvalidFormat ( 'Networks' )
121115 }
@@ -124,7 +118,7 @@ export default class NetworksService extends Store {
124118
125119 @Validate
126120 public async create ( @Required name : NetworkName , @Required remote : NetworkRemote , type : NetworkType = NetworkType . Normal ) {
127- const list = await this . getAll ( )
121+ const list = this . getAll ( )
128122 if ( list . some ( item => item . name === name ) ) {
129123 throw new UsedName ( 'Network' )
130124 }
@@ -148,13 +142,13 @@ export default class NetworksService extends Store {
148142 chain,
149143 }
150144
151- await this . updateAll ( [ ...list , newOne ] )
145+ this . updateAll ( [ ...list , newOne ] )
152146 return newOne
153147 }
154148
155149 @Validate
156150 public async update ( @Required id : NetworkID , @Required options : Partial < Network > ) {
157- const list = await this . getAll ( )
151+ const list = this . getAll ( )
158152 const network = list . find ( item => item . id === id )
159153 if ( ! network ) {
160154 throw new NetworkNotFound ( id )
@@ -177,30 +171,30 @@ export default class NetworksService extends Store {
177171 }
178172
179173 this . updateAll ( list )
180- const currentID = await this . getCurrentID ( )
174+ const currentID = this . getCurrentID ( )
181175 if ( currentID === id ) {
182176 await this . activate ( id )
183177 }
184178 }
185179
186180 @Validate
187181 public async delete ( @Required id : NetworkID ) {
188- const networkToDelete = await this . get ( id )
182+ const networkToDelete = this . get ( id )
189183 if ( ! networkToDelete ) {
190184 throw new NetworkNotFound ( id )
191185 }
192186 if ( networkToDelete . type === NetworkType . Default ) {
193187 throw new DefaultNetworkUnremovable ( )
194188 }
195189
196- const prevNetworkList = await this . getAll ( )
190+ const prevNetworkList = this . getAll ( )
197191 const currentNetworkList = prevNetworkList . filter ( item => item . id !== id )
198192 this . updateAll ( currentNetworkList )
199193 }
200194
201195 @Validate
202196 public async activate ( @Required id : NetworkID ) {
203- const network = await this . get ( id )
197+ const network = this . get ( id )
204198 if ( ! network ) {
205199 throw new NetworkNotFound ( id )
206200 }
@@ -227,12 +221,12 @@ export default class NetworksService extends Store {
227221 }
228222 }
229223
230- public getCurrentID = async ( ) => {
231- return ( await this . read < string > ( NetworksKey . Current ) ) || null
224+ public getCurrentID = ( ) => {
225+ return this . readSync < string > ( NetworksKey . Current ) || null
232226 }
233227
234- public defaultOne = async ( ) => {
235- const list = await this . getAll ( )
228+ public defaultOne = ( ) => {
229+ const list = this . getAll ( )
236230 return list . find ( item => item . type === NetworkType . Default ) || null
237231 }
238232}
0 commit comments