Skip to content

Commit b56ae1c

Browse files
committed
feat: subscribe network list from neuron-wallet in neuron-ui
1 parent 4788f4a commit b56ae1c

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

packages/neuron-ui/src/containers/Main/hooks.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import UILayer, {
1515
networksCall,
1616
} from 'services/UILayer'
1717
import { initWindow } from 'services/remote'
18-
import { SystemScript as SystemScriptSubject, DataUpdate as DataUpdateSubject } from 'services/subjects'
18+
import {
19+
SystemScript as SystemScriptSubject,
20+
DataUpdate as DataUpdateSubject,
21+
NetworkList as NetworkListSubject,
22+
} from 'services/subjects'
1923
import { ckbCore, getTipBlockNumber, getBlockchainInfo } from 'services/chain'
2024
import { Routes, Channel, ConnectionStatus } from 'utils/const'
2125
import {
@@ -262,14 +266,6 @@ export const useChannelListeners = ({
262266
UILayer.on(Channel.Networks, (_e: Event, method: NetworksMethod, args: ChannelResponse<any>) => {
263267
if (args.status) {
264268
switch (method) {
265-
case NetworksMethod.GetAll: {
266-
dispatch({
267-
type: NeuronWalletActions.Settings,
268-
payload: { networks: args.result || [] },
269-
})
270-
networksCache.save(args.result || [])
271-
break
272-
}
273269
case NetworksMethod.CurrentID: {
274270
dispatch({
275271
type: NeuronWalletActions.Chain,
@@ -455,9 +451,17 @@ export const useSubscription = ({
455451
}
456452
}
457453
})
454+
const networkListSubscription = NetworkListSubject.subscribe(({ currentNetworkList = [] }) => {
455+
dispatch({
456+
type: NeuronWalletActions.UpdateNetworkList,
457+
payload: currentNetworkList,
458+
})
459+
networksCache.save(currentNetworkList)
460+
})
458461
return () => {
459462
systemScriptSubscription.unsubscribe()
460463
dataUpdateSubscription.unsubscribe()
464+
networkListSubscription.unsubscribe()
461465
}
462466
}, [walletID, pageNo, pageSize, keywords, txHash, dispatch])
463467
}

packages/neuron-ui/src/services/UILayer.ts

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export enum WalletsMethod {
3636
}
3737

3838
export enum NetworksMethod {
39-
GetAll = 'getAll',
4039
Get = 'get',
4140
Create = 'create',
4241
Update = 'update',

packages/neuron-ui/src/services/subjects.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const SUBJECT_PATH = `./models/subjects`
2+
13
const FallbackSubject = {
24
subscribe: (args: any) => {
35
console.warn('remote is not supported')
@@ -13,18 +15,25 @@ const FallbackSubject = {
1315
},
1416
}
1517
export const SystemScript = window.remote
16-
? (window.remote.require('./models/subjects/system-script').default as NeuronWalletSubject<{ codeHash: string }>)
18+
? (window.remote.require(`${SUBJECT_PATH}/system-script`).default as NeuronWalletSubject<{ codeHash: string }>)
1719
: FallbackSubject
1820

1921
export const DataUpdate = window.remote
20-
? (window.remote.require('./models/subjects/data-update').default as NeuronWalletSubject<{
22+
? (window.remote.require(`${SUBJECT_PATH}/data-update`).default as NeuronWalletSubject<{
2123
dataType: 'address' | 'transaction' | 'wallet' | 'network'
2224
actionType: 'create' | 'update' | 'delete'
2325
walletID?: string
2426
}>)
2527
: FallbackSubject
2628

29+
export const NetworkList = window.remote
30+
? (window.remote.require(`${SUBJECT_PATH}/networks`).NetworkListSubject as NeuronWalletSubject<{
31+
currentNetworkList: State.Network[]
32+
}>)
33+
: FallbackSubject
34+
2735
export default {
2836
SystemScript,
2937
DataUpdate,
38+
NetworkList,
3039
}

packages/neuron-ui/src/states/stateProvider/reducer.ts

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export enum NeuronWalletActions {
77
Wallet = 'wallet',
88
Settings = 'settings',
99
UpdateCodeHash = 'updateCodeHash',
10+
UpdateNetworkList = 'updateNetworkList',
1011
}
1112
export enum AppActions {
1213
UpdateTransactionID = 'updateTransactionID',
@@ -137,6 +138,15 @@ export const reducer = (
137138
},
138139
}
139140
}
141+
case NeuronWalletActions.UpdateNetworkList: {
142+
return {
143+
...state,
144+
settings: {
145+
...settings,
146+
networks: payload,
147+
},
148+
}
149+
}
140150
// Actions of App
141151
case AppActions.UpdateTipBlockNumber: {
142152
/**

packages/neuron-ui/src/utils/initializeApp.ts

+8-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import initStates from 'states/initStates'
33

44
import {
55
wallets as walletsCache,
6-
networks as networksCache,
76
addresses as addressesCache,
87
currentNetworkID as currentNetworkIDCache,
98
currentWallet as currentWalletCache,
@@ -27,8 +26,6 @@ const intializeApp = ({
2726
}) => {
2827
const {
2928
locale = '',
30-
networks = [],
31-
currentNetworkID: networkID = '',
3229
wallets = [],
3330
currentWallet: wallet = initStates.wallet,
3431
addresses = [],
@@ -47,17 +44,14 @@ const intializeApp = ({
4744
} else {
4845
history.push(`${Routes.WalletWizard}${WalletWizardPath.Welcome}`)
4946
}
50-
if (networks.length) {
51-
dispatch({
52-
type: NeuronWalletActions.Initiate,
53-
payload: {
54-
networks,
55-
networkID,
56-
wallet: { ...wallet, balance: addressesToBalance(addresses), addresses },
57-
wallets,
58-
},
59-
})
60-
}
47+
dispatch({
48+
type: NeuronWalletActions.Initiate,
49+
payload: {
50+
networkID,
51+
wallet: { ...wallet, balance: addressesToBalance(addresses), addresses },
52+
wallets,
53+
},
54+
})
6155
dispatch({
6256
type: NeuronWalletActions.Chain,
6357
payload: {
@@ -69,10 +63,8 @@ const intializeApp = ({
6963
})
7064

7165
currentWalletCache.save(wallet)
72-
currentNetworkIDCache.save(networkID)
7366
walletsCache.save(wallets)
7467
addressesCache.save(addresses)
75-
networksCache.save(networks)
7668
systemScriptCache.save({ codeHash })
7769
}
7870
export default intializeApp

packages/neuron-wallet/src/controllers/app/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ export default class AppController {
2929
currentWallet = null,
3030
wallets = [],
3131
currentNetworkID = '',
32-
networks = [],
3332
tipNumber = '0',
3433
connectionStatus = false,
3534
codeHash = '',
3635
] = await Promise.all([
3736
walletsService.getCurrent(),
3837
walletsService.getAll(),
3938
networksService.getCurrentID(),
40-
networksService.getAll(),
4139
SyncInfoController.currentBlockNumber()
4240
.then(res => {
4341
if (res.status) {
@@ -80,7 +78,6 @@ export default class AppController {
8078
wallets: [...wallets.map(({ name, id }) => ({ id, name }))],
8179
addresses,
8280
currentNetworkID,
83-
networks,
8481
transactions,
8582
locale,
8683
tipNumber,

0 commit comments

Comments
 (0)