From b56ae1c862643e13291708df0f8cfac5fa1a64c0 Mon Sep 17 00:00:00 2001 From: Keith Date: Wed, 24 Jul 2019 16:33:57 +0800 Subject: [PATCH] feat: subscribe network list from neuron-wallet in neuron-ui --- .../neuron-ui/src/containers/Main/hooks.ts | 22 ++++++++++------- packages/neuron-ui/src/services/UILayer.ts | 1 - packages/neuron-ui/src/services/subjects.ts | 13 ++++++++-- .../src/states/stateProvider/reducer.ts | 10 ++++++++ packages/neuron-ui/src/utils/initializeApp.ts | 24 +++++++------------ .../src/controllers/app/index.ts | 3 --- 6 files changed, 42 insertions(+), 31 deletions(-) diff --git a/packages/neuron-ui/src/containers/Main/hooks.ts b/packages/neuron-ui/src/containers/Main/hooks.ts index 094bb5a8e3..c9d32b7e78 100644 --- a/packages/neuron-ui/src/containers/Main/hooks.ts +++ b/packages/neuron-ui/src/containers/Main/hooks.ts @@ -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 { @@ -262,14 +266,6 @@ export const useChannelListeners = ({ UILayer.on(Channel.Networks, (_e: Event, method: NetworksMethod, args: ChannelResponse) => { 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, @@ -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]) } diff --git a/packages/neuron-ui/src/services/UILayer.ts b/packages/neuron-ui/src/services/UILayer.ts index 14d5a9e9aa..c0a8d8790c 100644 --- a/packages/neuron-ui/src/services/UILayer.ts +++ b/packages/neuron-ui/src/services/UILayer.ts @@ -36,7 +36,6 @@ export enum WalletsMethod { } export enum NetworksMethod { - GetAll = 'getAll', Get = 'get', Create = 'create', Update = 'update', diff --git a/packages/neuron-ui/src/services/subjects.ts b/packages/neuron-ui/src/services/subjects.ts index 6523260f5a..bc93517b2d 100644 --- a/packages/neuron-ui/src/services/subjects.ts +++ b/packages/neuron-ui/src/services/subjects.ts @@ -1,3 +1,5 @@ +const SUBJECT_PATH = `./models/subjects` + const FallbackSubject = { subscribe: (args: any) => { console.warn('remote is not supported') @@ -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, } diff --git a/packages/neuron-ui/src/states/stateProvider/reducer.ts b/packages/neuron-ui/src/states/stateProvider/reducer.ts index 9797fdbe8d..632e0a7553 100644 --- a/packages/neuron-ui/src/states/stateProvider/reducer.ts +++ b/packages/neuron-ui/src/states/stateProvider/reducer.ts @@ -7,6 +7,7 @@ export enum NeuronWalletActions { Wallet = 'wallet', Settings = 'settings', UpdateCodeHash = 'updateCodeHash', + UpdateNetworkList = 'updateNetworkList', } export enum AppActions { UpdateTransactionID = 'updateTransactionID', @@ -137,6 +138,15 @@ export const reducer = ( }, } } + case NeuronWalletActions.UpdateNetworkList: { + return { + ...state, + settings: { + ...settings, + networks: payload, + }, + } + } // Actions of App case AppActions.UpdateTipBlockNumber: { /** diff --git a/packages/neuron-ui/src/utils/initializeApp.ts b/packages/neuron-ui/src/utils/initializeApp.ts index 2a7281c665..27415c01e2 100644 --- a/packages/neuron-ui/src/utils/initializeApp.ts +++ b/packages/neuron-ui/src/utils/initializeApp.ts @@ -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, @@ -27,8 +26,6 @@ const intializeApp = ({ }) => { const { locale = '', - networks = [], - currentNetworkID: networkID = '', wallets = [], currentWallet: wallet = initStates.wallet, addresses = [], @@ -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: { @@ -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 diff --git a/packages/neuron-wallet/src/controllers/app/index.ts b/packages/neuron-wallet/src/controllers/app/index.ts index 26aafda06f..c648356536 100644 --- a/packages/neuron-wallet/src/controllers/app/index.ts +++ b/packages/neuron-wallet/src/controllers/app/index.ts @@ -29,7 +29,6 @@ export default class AppController { currentWallet = null, wallets = [], currentNetworkID = '', - networks = [], tipNumber = '0', connectionStatus = false, codeHash = '', @@ -37,7 +36,6 @@ export default class AppController { walletsService.getCurrent(), walletsService.getAll(), networksService.getCurrentID(), - networksService.getAll(), SyncInfoController.currentBlockNumber() .then(res => { if (res.status) { @@ -80,7 +78,6 @@ export default class AppController { wallets: [...wallets.map(({ name, id }) => ({ id, name }))], addresses, currentNetworkID, - networks, transactions, locale, tipNumber,