Skip to content

Commit 1173622

Browse files
committed
feat(neuron-ui): subscribe current network id from neuron-wallet in neuron-ui
1 parent b56ae1c commit 1173622

File tree

10 files changed

+52
-44
lines changed

10 files changed

+52
-44
lines changed

packages/neuron-ui/src/components/ErrorBoundary/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ErrorBoundary extends Component<{ children: React.ReactChild }, { hasError
1616
}
1717

1818
static getDerivedStateFromError(error: Error) {
19+
window.alert(error.stack)
1920
return handleError(error)
2021
}
2122

packages/neuron-ui/src/containers/Footer/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ const Footer = ({
6767
location: { pathname },
6868
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
6969
const {
70-
app: { tipBlockNumber },
71-
chain: { networkID, connectionStatus, tipBlockNumber: syncedBlockNumber },
72-
settings: { networks },
70+
app: { tipBlockNumber = '0' },
71+
chain: { networkID = '', connectionStatus = ConnectionStatus.Offline, tipBlockNumber: syncedBlockNumber = '0' },
72+
settings: { networks = [] },
7373
} = useContext(NeuronWalletContext)
7474
const [t] = useTranslation()
7575

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
SystemScript as SystemScriptSubject,
2020
DataUpdate as DataUpdateSubject,
2121
NetworkList as NetworkListSubject,
22+
CurrentNetworkID as CurrentNetworkIDSubject,
2223
} from 'services/subjects'
2324
import { ckbCore, getTipBlockNumber, getBlockchainInfo } from 'services/chain'
2425
import { Routes, Channel, ConnectionStatus } from 'utils/const'
@@ -266,14 +267,6 @@ export const useChannelListeners = ({
266267
UILayer.on(Channel.Networks, (_e: Event, method: NetworksMethod, args: ChannelResponse<any>) => {
267268
if (args.status) {
268269
switch (method) {
269-
case NetworksMethod.CurrentID: {
270-
dispatch({
271-
type: NeuronWalletActions.Chain,
272-
payload: { networkID: args.result },
273-
})
274-
currentNetworkIDCache.save(args.result)
275-
break
276-
}
277270
case NetworksMethod.Create:
278271
case NetworksMethod.Update: {
279272
history.push(Routes.SettingsNetworks)
@@ -458,10 +451,18 @@ export const useSubscription = ({
458451
})
459452
networksCache.save(currentNetworkList)
460453
})
454+
const currentNetworkIDSubscription = CurrentNetworkIDSubject.subscribe(({ currentNetworkID = '' }) => {
455+
dispatch({
456+
type: NeuronWalletActions.UpdateCurrentNetworkID,
457+
payload: currentNetworkID,
458+
})
459+
currentNetworkIDCache.save(currentNetworkID)
460+
})
461461
return () => {
462462
systemScriptSubscription.unsubscribe()
463463
dataUpdateSubscription.unsubscribe()
464464
networkListSubscription.unsubscribe()
465+
currentNetworkIDSubscription.unsubscribe()
465466
}
466467
}, [walletID, pageNo, pageSize, keywords, txHash, dispatch])
467468
}

packages/neuron-ui/src/containers/Main/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ const MainContent = ({
108108
}: React.PropsWithoutRef<{ dispatch: StateDispatch } & RouteComponentProps>) => {
109109
const neuronWalletState = useState()
110110
const {
111-
wallet: { id: walletID },
111+
wallet: { id: walletID = '' },
112112
chain,
113-
settings: { networks },
113+
settings: { networks = [] },
114114
} = neuronWalletState
115115
const { networkID } = chain
116116
const [, i18n] = useTranslation()

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

+7
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ export const NetworkList = window.remote
3232
}>)
3333
: FallbackSubject
3434

35+
export const CurrentNetworkID = window.remote
36+
? (window.remote.require(`${SUBJECT_PATH}/networks`).CurrentNetworkIDSubject as NeuronWalletSubject<{
37+
currentNetworkID: string
38+
}>)
39+
: FallbackSubject
40+
3541
export default {
3642
SystemScript,
3743
DataUpdate,
3844
NetworkList,
45+
CurrentNetworkID,
3946
}

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export enum NeuronWalletActions {
88
Settings = 'settings',
99
UpdateCodeHash = 'updateCodeHash',
1010
UpdateNetworkList = 'updateNetworkList',
11+
UpdateCurrentNetworkID = 'updateCurrentNetworkID',
1112
}
1213
export enum AppActions {
1314
UpdateTransactionID = 'updateTransactionID',
@@ -53,18 +54,16 @@ export const reducer = (
5354
switch (type) {
5455
// Actions of Neuron Wallet
5556
case NeuronWalletActions.Initiate: {
56-
const { networks, networkID, wallets, wallet: incomingWallet } = payload
57+
const { wallets, wallet: incomingWallet } = payload
5758
return {
5859
...state,
5960
wallet: incomingWallet || wallet,
6061
chain: {
6162
...state.chain,
62-
networkID,
6363
},
6464
settings: {
6565
...state.settings,
6666
wallets,
67-
networks,
6867
},
6968
}
7069
}
@@ -147,6 +146,15 @@ export const reducer = (
147146
},
148147
}
149148
}
149+
case NeuronWalletActions.UpdateCurrentNetworkID: {
150+
return {
151+
...state,
152+
chain: {
153+
...chain,
154+
networkID: payload,
155+
},
156+
}
157+
}
150158
// Actions of App
151159
case AppActions.UpdateTipBlockNumber: {
152160
/**

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

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import initStates from 'states/initStates'
44
import {
55
wallets as walletsCache,
66
addresses as addressesCache,
7-
currentNetworkID as currentNetworkIDCache,
87
currentWallet as currentWalletCache,
98
systemScript as systemScriptCache,
109
language as languageCache,
@@ -47,7 +46,6 @@ const intializeApp = ({
4746
dispatch({
4847
type: NeuronWalletActions.Initiate,
4948
payload: {
50-
networkID,
5149
wallet: { ...wallet, balance: addressesToBalance(addresses), addresses },
5250
wallets,
5351
},

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

-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import app from '../../app'
66
import { URL, contextMenuTemplate } from './options'
77

88
import TransactionsController from '../transactions'
9-
import NetworksService from '../../services/networks'
109
import WalletsService from '../../services/wallets'
1110
import NodeService from '../../services/node'
1211
import WalletsController from '../wallets'
@@ -18,7 +17,6 @@ import WindowManager from '../../models/window-manager'
1817
import i18n from '../../utils/i18n'
1918
import env from '../../env'
2019

21-
const networksService = NetworksService.getInstance()
2220
const nodeService = NodeService.getInstance()
2321

2422
@ControllerDecorator(Channel.App)
@@ -28,14 +26,12 @@ export default class AppController {
2826
const [
2927
currentWallet = null,
3028
wallets = [],
31-
currentNetworkID = '',
3229
tipNumber = '0',
3330
connectionStatus = false,
3431
codeHash = '',
3532
] = await Promise.all([
3633
walletsService.getCurrent(),
3734
walletsService.getAll(),
38-
networksService.getCurrentID(),
3935
SyncInfoController.currentBlockNumber()
4036
.then(res => {
4137
if (res.status) {
@@ -77,7 +73,6 @@ export default class AppController {
7773
},
7874
wallets: [...wallets.map(({ name, id }) => ({ id, name }))],
7975
addresses,
80-
currentNetworkID,
8176
transactions,
8277
locale,
8378
tipNumber,

packages/neuron-wallet/src/models/subjects/networks.ts

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
import { Subject } from 'rxjs'
2-
import { debounceTime } from 'rxjs/operators'
3-
import DataUpdateSubject from './data-update'
1+
import { BehaviorSubject } from 'rxjs'
42

5-
const DEBOUNCE_TIME = 50
6-
7-
export const NetworkListSubject = new Subject<{
3+
export const NetworkListSubject = new BehaviorSubject<{
84
currentNetworkList: Controller.Network[]
9-
}>()
10-
export const CurrentNetworkIDSubject = new Subject<{ currentNetworkID: Controller.NetworkID }>()
11-
12-
NetworkListSubject.pipe(debounceTime(DEBOUNCE_TIME)).subscribe(() => {
13-
DataUpdateSubject.next({ dataType: 'network', actionType: 'update' })
14-
})
15-
16-
CurrentNetworkIDSubject.pipe(debounceTime(DEBOUNCE_TIME)).subscribe(() => {
17-
DataUpdateSubject.next({ dataType: 'network', actionType: 'update' })
5+
}>({ currentNetworkList: [] })
6+
export const CurrentNetworkIDSubject = new BehaviorSubject<{ currentNetworkID: Controller.NetworkID }>({
7+
currentNetworkID: '',
188
})
199

2010
export default {

packages/neuron-wallet/src/services/networks.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ export default class NetworksService extends Store {
4545
constructor() {
4646
super('networks', 'index.json', JSON.stringify(env.presetNetworks))
4747

48+
this.getAll().then(currentNetworkList => {
49+
if (currentNetworkList) {
50+
NetworkListSubject.next({
51+
currentNetworkList,
52+
})
53+
}
54+
})
55+
56+
this.getCurrentID().then(currentNetworkID => {
57+
if (currentNetworkID) {
58+
CurrentNetworkIDSubject.next({ currentNetworkID })
59+
}
60+
})
61+
4862
this.on(NetworksKey.List, async (_, currentNetworkList: NetworkWithID[] = []) => {
4963
NetworkListSubject.next({ currentNetworkList })
5064

@@ -69,12 +83,6 @@ export default class NetworksService extends Store {
6983
NodeService.getInstance().setNetwork(currentNetwork.remote)
7084
networkSwitchSubject.next(currentNetwork)
7185
})
72-
73-
this.getCurrentID().then(currentID => {
74-
if (currentID) {
75-
this.emit(NetworksKey.Current, null, currentID)
76-
}
77-
})
7886
}
7987

8088
public getAll = async () => {

0 commit comments

Comments
 (0)