Skip to content

Commit

Permalink
feat(neuron-ui): subscribe current network id from neuron-wallet in n…
Browse files Browse the repository at this point in the history
…euron-ui
  • Loading branch information
Keith-CY committed Jul 25, 2019
1 parent b56ae1c commit 1173622
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 44 deletions.
1 change: 1 addition & 0 deletions packages/neuron-ui/src/components/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ErrorBoundary extends Component<{ children: React.ReactChild }, { hasError
}

static getDerivedStateFromError(error: Error) {
window.alert(error.stack)
return handleError(error)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-ui/src/containers/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ const Footer = ({
location: { pathname },
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
const {
app: { tipBlockNumber },
chain: { networkID, connectionStatus, tipBlockNumber: syncedBlockNumber },
settings: { networks },
app: { tipBlockNumber = '0' },
chain: { networkID = '', connectionStatus = ConnectionStatus.Offline, tipBlockNumber: syncedBlockNumber = '0' },
settings: { networks = [] },
} = useContext(NeuronWalletContext)
const [t] = useTranslation()

Expand Down
17 changes: 9 additions & 8 deletions packages/neuron-ui/src/containers/Main/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SystemScript as SystemScriptSubject,
DataUpdate as DataUpdateSubject,
NetworkList as NetworkListSubject,
CurrentNetworkID as CurrentNetworkIDSubject,
} from 'services/subjects'
import { ckbCore, getTipBlockNumber, getBlockchainInfo } from 'services/chain'
import { Routes, Channel, ConnectionStatus } from 'utils/const'
Expand Down Expand Up @@ -266,14 +267,6 @@ export const useChannelListeners = ({
UILayer.on(Channel.Networks, (_e: Event, method: NetworksMethod, args: ChannelResponse<any>) => {
if (args.status) {
switch (method) {
case NetworksMethod.CurrentID: {
dispatch({
type: NeuronWalletActions.Chain,
payload: { networkID: args.result },
})
currentNetworkIDCache.save(args.result)
break
}
case NetworksMethod.Create:
case NetworksMethod.Update: {
history.push(Routes.SettingsNetworks)
Expand Down Expand Up @@ -458,10 +451,18 @@ export const useSubscription = ({
})
networksCache.save(currentNetworkList)
})
const currentNetworkIDSubscription = CurrentNetworkIDSubject.subscribe(({ currentNetworkID = '' }) => {
dispatch({
type: NeuronWalletActions.UpdateCurrentNetworkID,
payload: currentNetworkID,
})
currentNetworkIDCache.save(currentNetworkID)
})
return () => {
systemScriptSubscription.unsubscribe()
dataUpdateSubscription.unsubscribe()
networkListSubscription.unsubscribe()
currentNetworkIDSubscription.unsubscribe()
}
}, [walletID, pageNo, pageSize, keywords, txHash, dispatch])
}
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/containers/Main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ const MainContent = ({
}: React.PropsWithoutRef<{ dispatch: StateDispatch } & RouteComponentProps>) => {
const neuronWalletState = useState()
const {
wallet: { id: walletID },
wallet: { id: walletID = '' },
chain,
settings: { networks },
settings: { networks = [] },
} = neuronWalletState
const { networkID } = chain
const [, i18n] = useTranslation()
Expand Down
7 changes: 7 additions & 0 deletions packages/neuron-ui/src/services/subjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ export const NetworkList = window.remote
}>)
: FallbackSubject

export const CurrentNetworkID = window.remote
? (window.remote.require(`${SUBJECT_PATH}/networks`).CurrentNetworkIDSubject as NeuronWalletSubject<{
currentNetworkID: string
}>)
: FallbackSubject

export default {
SystemScript,
DataUpdate,
NetworkList,
CurrentNetworkID,
}
14 changes: 11 additions & 3 deletions packages/neuron-ui/src/states/stateProvider/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum NeuronWalletActions {
Settings = 'settings',
UpdateCodeHash = 'updateCodeHash',
UpdateNetworkList = 'updateNetworkList',
UpdateCurrentNetworkID = 'updateCurrentNetworkID',
}
export enum AppActions {
UpdateTransactionID = 'updateTransactionID',
Expand Down Expand Up @@ -53,18 +54,16 @@ export const reducer = (
switch (type) {
// Actions of Neuron Wallet
case NeuronWalletActions.Initiate: {
const { networks, networkID, wallets, wallet: incomingWallet } = payload
const { wallets, wallet: incomingWallet } = payload
return {
...state,
wallet: incomingWallet || wallet,
chain: {
...state.chain,
networkID,
},
settings: {
...state.settings,
wallets,
networks,
},
}
}
Expand Down Expand Up @@ -147,6 +146,15 @@ export const reducer = (
},
}
}
case NeuronWalletActions.UpdateCurrentNetworkID: {
return {
...state,
chain: {
...chain,
networkID: payload,
},
}
}
// Actions of App
case AppActions.UpdateTipBlockNumber: {
/**
Expand Down
2 changes: 0 additions & 2 deletions packages/neuron-ui/src/utils/initializeApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import initStates from 'states/initStates'
import {
wallets as walletsCache,
addresses as addressesCache,
currentNetworkID as currentNetworkIDCache,
currentWallet as currentWalletCache,
systemScript as systemScriptCache,
language as languageCache,
Expand Down Expand Up @@ -47,7 +46,6 @@ const intializeApp = ({
dispatch({
type: NeuronWalletActions.Initiate,
payload: {
networkID,
wallet: { ...wallet, balance: addressesToBalance(addresses), addresses },
wallets,
},
Expand Down
5 changes: 0 additions & 5 deletions packages/neuron-wallet/src/controllers/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import app from '../../app'
import { URL, contextMenuTemplate } from './options'

import TransactionsController from '../transactions'
import NetworksService from '../../services/networks'
import WalletsService from '../../services/wallets'
import NodeService from '../../services/node'
import WalletsController from '../wallets'
Expand All @@ -18,7 +17,6 @@ import WindowManager from '../../models/window-manager'
import i18n from '../../utils/i18n'
import env from '../../env'

const networksService = NetworksService.getInstance()
const nodeService = NodeService.getInstance()

@ControllerDecorator(Channel.App)
Expand All @@ -28,14 +26,12 @@ export default class AppController {
const [
currentWallet = null,
wallets = [],
currentNetworkID = '',
tipNumber = '0',
connectionStatus = false,
codeHash = '',
] = await Promise.all([
walletsService.getCurrent(),
walletsService.getAll(),
networksService.getCurrentID(),
SyncInfoController.currentBlockNumber()
.then(res => {
if (res.status) {
Expand Down Expand Up @@ -77,7 +73,6 @@ export default class AppController {
},
wallets: [...wallets.map(({ name, id }) => ({ id, name }))],
addresses,
currentNetworkID,
transactions,
locale,
tipNumber,
Expand Down
20 changes: 5 additions & 15 deletions packages/neuron-wallet/src/models/subjects/networks.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { Subject } from 'rxjs'
import { debounceTime } from 'rxjs/operators'
import DataUpdateSubject from './data-update'
import { BehaviorSubject } from 'rxjs'

const DEBOUNCE_TIME = 50

export const NetworkListSubject = new Subject<{
export const NetworkListSubject = new BehaviorSubject<{
currentNetworkList: Controller.Network[]
}>()
export const CurrentNetworkIDSubject = new Subject<{ currentNetworkID: Controller.NetworkID }>()

NetworkListSubject.pipe(debounceTime(DEBOUNCE_TIME)).subscribe(() => {
DataUpdateSubject.next({ dataType: 'network', actionType: 'update' })
})

CurrentNetworkIDSubject.pipe(debounceTime(DEBOUNCE_TIME)).subscribe(() => {
DataUpdateSubject.next({ dataType: 'network', actionType: 'update' })
}>({ currentNetworkList: [] })
export const CurrentNetworkIDSubject = new BehaviorSubject<{ currentNetworkID: Controller.NetworkID }>({
currentNetworkID: '',
})

export default {
Expand Down
20 changes: 14 additions & 6 deletions packages/neuron-wallet/src/services/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ export default class NetworksService extends Store {
constructor() {
super('networks', 'index.json', JSON.stringify(env.presetNetworks))

this.getAll().then(currentNetworkList => {
if (currentNetworkList) {
NetworkListSubject.next({
currentNetworkList,
})
}
})

this.getCurrentID().then(currentNetworkID => {
if (currentNetworkID) {
CurrentNetworkIDSubject.next({ currentNetworkID })
}
})

this.on(NetworksKey.List, async (_, currentNetworkList: NetworkWithID[] = []) => {
NetworkListSubject.next({ currentNetworkList })

Expand All @@ -69,12 +83,6 @@ export default class NetworksService extends Store {
NodeService.getInstance().setNetwork(currentNetwork.remote)
networkSwitchSubject.next(currentNetwork)
})

this.getCurrentID().then(currentID => {
if (currentID) {
this.emit(NetworksKey.Current, null, currentID)
}
})
}

public getAll = async () => {
Expand Down

0 comments on commit 1173622

Please sign in to comment.