Skip to content

Commit

Permalink
feat: subscribe network list from neuron-wallet in neuron-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Jul 25, 2019
1 parent 4788f4a commit b56ae1c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
22 changes: 13 additions & 9 deletions packages/neuron-ui/src/containers/Main/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import UILayer, {
networksCall,
} from 'services/UILayer'
import { initWindow } from 'services/remote'
import { SystemScript as SystemScriptSubject, DataUpdate as DataUpdateSubject } from 'services/subjects'
import {
SystemScript as SystemScriptSubject,
DataUpdate as DataUpdateSubject,
NetworkList as NetworkListSubject,
} from 'services/subjects'
import { ckbCore, getTipBlockNumber, getBlockchainInfo } from 'services/chain'
import { Routes, Channel, ConnectionStatus } from 'utils/const'
import {
Expand Down Expand Up @@ -262,14 +266,6 @@ export const useChannelListeners = ({
UILayer.on(Channel.Networks, (_e: Event, method: NetworksMethod, args: ChannelResponse<any>) => {
if (args.status) {
switch (method) {
case NetworksMethod.GetAll: {
dispatch({
type: NeuronWalletActions.Settings,
payload: { networks: args.result || [] },
})
networksCache.save(args.result || [])
break
}
case NetworksMethod.CurrentID: {
dispatch({
type: NeuronWalletActions.Chain,
Expand Down Expand Up @@ -455,9 +451,17 @@ export const useSubscription = ({
}
}
})
const networkListSubscription = NetworkListSubject.subscribe(({ currentNetworkList = [] }) => {
dispatch({
type: NeuronWalletActions.UpdateNetworkList,
payload: currentNetworkList,
})
networksCache.save(currentNetworkList)
})
return () => {
systemScriptSubscription.unsubscribe()
dataUpdateSubscription.unsubscribe()
networkListSubscription.unsubscribe()
}
}, [walletID, pageNo, pageSize, keywords, txHash, dispatch])
}
Expand Down
1 change: 0 additions & 1 deletion packages/neuron-ui/src/services/UILayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export enum WalletsMethod {
}

export enum NetworksMethod {
GetAll = 'getAll',
Get = 'get',
Create = 'create',
Update = 'update',
Expand Down
13 changes: 11 additions & 2 deletions packages/neuron-ui/src/services/subjects.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const SUBJECT_PATH = `./models/subjects`

const FallbackSubject = {
subscribe: (args: any) => {
console.warn('remote is not supported')
Expand All @@ -13,18 +15,25 @@ const FallbackSubject = {
},
}
export const SystemScript = window.remote
? (window.remote.require('./models/subjects/system-script').default as NeuronWalletSubject<{ codeHash: string }>)
? (window.remote.require(`${SUBJECT_PATH}/system-script`).default as NeuronWalletSubject<{ codeHash: string }>)
: FallbackSubject

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

export const NetworkList = window.remote
? (window.remote.require(`${SUBJECT_PATH}/networks`).NetworkListSubject as NeuronWalletSubject<{
currentNetworkList: State.Network[]
}>)
: FallbackSubject

export default {
SystemScript,
DataUpdate,
NetworkList,
}
10 changes: 10 additions & 0 deletions packages/neuron-ui/src/states/stateProvider/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum NeuronWalletActions {
Wallet = 'wallet',
Settings = 'settings',
UpdateCodeHash = 'updateCodeHash',
UpdateNetworkList = 'updateNetworkList',
}
export enum AppActions {
UpdateTransactionID = 'updateTransactionID',
Expand Down Expand Up @@ -137,6 +138,15 @@ export const reducer = (
},
}
}
case NeuronWalletActions.UpdateNetworkList: {
return {
...state,
settings: {
...settings,
networks: payload,
},
}
}
// Actions of App
case AppActions.UpdateTipBlockNumber: {
/**
Expand Down
24 changes: 8 additions & 16 deletions packages/neuron-ui/src/utils/initializeApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import initStates from 'states/initStates'

import {
wallets as walletsCache,
networks as networksCache,
addresses as addressesCache,
currentNetworkID as currentNetworkIDCache,
currentWallet as currentWalletCache,
Expand All @@ -27,8 +26,6 @@ const intializeApp = ({
}) => {
const {
locale = '',
networks = [],
currentNetworkID: networkID = '',
wallets = [],
currentWallet: wallet = initStates.wallet,
addresses = [],
Expand All @@ -47,17 +44,14 @@ const intializeApp = ({
} else {
history.push(`${Routes.WalletWizard}${WalletWizardPath.Welcome}`)
}
if (networks.length) {
dispatch({
type: NeuronWalletActions.Initiate,
payload: {
networks,
networkID,
wallet: { ...wallet, balance: addressesToBalance(addresses), addresses },
wallets,
},
})
}
dispatch({
type: NeuronWalletActions.Initiate,
payload: {
networkID,
wallet: { ...wallet, balance: addressesToBalance(addresses), addresses },
wallets,
},
})
dispatch({
type: NeuronWalletActions.Chain,
payload: {
Expand All @@ -69,10 +63,8 @@ const intializeApp = ({
})

currentWalletCache.save(wallet)
currentNetworkIDCache.save(networkID)
walletsCache.save(wallets)
addressesCache.save(addresses)
networksCache.save(networks)
systemScriptCache.save({ codeHash })
}
export default intializeApp
3 changes: 0 additions & 3 deletions packages/neuron-wallet/src/controllers/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ export default class AppController {
currentWallet = null,
wallets = [],
currentNetworkID = '',
networks = [],
tipNumber = '0',
connectionStatus = false,
codeHash = '',
] = await Promise.all([
walletsService.getCurrent(),
walletsService.getAll(),
networksService.getCurrentID(),
networksService.getAll(),
SyncInfoController.currentBlockNumber()
.then(res => {
if (res.status) {
Expand Down Expand Up @@ -80,7 +78,6 @@ export default class AppController {
wallets: [...wallets.map(({ name, id }) => ({ id, name }))],
addresses,
currentNetworkID,
networks,
transactions,
locale,
tipNumber,
Expand Down

0 comments on commit b56ae1c

Please sign in to comment.