Skip to content

Commit

Permalink
feat: Expose a flat API controller from neuron wallet to UI layer
Browse files Browse the repository at this point in the history
Neuron UI should require this api controller only.
  • Loading branch information
ashchan committed Oct 21, 2019
1 parent 54600d8 commit 500c37c
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ export const RemoteNotLoadError = {
},
}

export const controllerNotLoaded = (controllerName: string) => ({
status: 0 as 0,
message: {
content: `${controllerName} controller not loaded`,
},
})

export const controllerMethodWrapper = (controllerName: string) => (
export const apiMethodWrapper = () => (
callControllerMethod: (
controller: any
) => (
Expand All @@ -42,13 +35,18 @@ export const controllerMethodWrapper = (controllerName: string) => (
if (!window.remote) {
return RemoteNotLoadError
}
const controller = window.remote.require(`./controllers/${controllerName}`).default
const controller = window.remote.require('./controllers/api').default
if (!controller) {
return controllerNotLoaded(controllerName)
return {
status: 0,
message: {
content: 'api controller not loaded',
},
}
}
const res = await callControllerMethod(controller)(realParams)
if (process.env.NODE_ENV === 'development' && window.localStorage.getItem('log-response')) {
console.group(`${controllerName} controller`)
console.group('api controller')
console.info(JSON.stringify(res, null, 2))
console.groupEnd()
}
Expand All @@ -75,6 +73,5 @@ export const controllerMethodWrapper = (controllerName: string) => (

export default {
RemoteNotLoadError,
controllerNotLoaded,
controllerMethodWrapper,
controllerMethodWrapper: apiMethodWrapper,
}
18 changes: 5 additions & 13 deletions packages/neuron-ui/src/services/remote/app.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { controllerMethodWrapper } from './controllerMethodWrapper'
import { apiMethodWrapper } from './apiMethodWrapper'

const CONTROLLER_NAME = 'api'
export const getNeuronWalletState = controllerMethodWrapper(CONTROLLER_NAME)(controller => () =>
controller.loadInitData()
)
export const getNeuronWalletState = apiMethodWrapper()(controller => () => controller.loadInitData())

export const handleViewError = controllerMethodWrapper(CONTROLLER_NAME)(controller => (errorMessage: string) =>
export const handleViewError = apiMethodWrapper()(controller => (errorMessage: string) =>
controller.handleViewError(errorMessage)
)
export const contextMenu = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: { type: string; id: string }) => controller.contextMenu(params)
)

export const showTransactionDetails = controllerMethodWrapper(CONTROLLER_NAME)(controller => (hash: string) =>
controller.showTransactionDetails(hash)
export const contextMenu = apiMethodWrapper()(controller => (params: { type: string; id: string }) =>
controller.contextMenu(params)
)

export default {
getNeuronWalletState,
handleViewError,
contextMenu,
showTransactionDetails,
}
2 changes: 2 additions & 0 deletions packages/neuron-ui/src/services/remote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const validateMnemonic = (mnemonic: string): boolean => {
console.warn(REMOTE_MODULE_NOT_FOUND)
return true
}
// TODO: use api controller, do NOT require models
const { validateMnemonic: remoteValidateMnemonic } = window.remote.require('./models/keys/mnemonic')
return remoteValidateMnemonic(mnemonic)
}
Expand All @@ -37,6 +38,7 @@ export const generateMnemonic = (): string => {
console.warn(REMOTE_MODULE_NOT_FOUND)
return ''
}
// TODO: use api controller, do NOT require models
const { generateMnemonic: remoteGenerateMnemonic } = window.remote.require('./models/keys/key')
return remoteGenerateMnemonic()
}
Expand Down
32 changes: 13 additions & 19 deletions packages/neuron-ui/src/services/remote/networks.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import { controllerMethodWrapper } from './controllerMethodWrapper'
import { apiMethodWrapper } from './apiMethodWrapper'

const CONTROLLER_NAME = 'networks'

export const setCurrentNetowrk = controllerMethodWrapper(CONTROLLER_NAME)((controller: any) => (networkID: string) => {
return controller.activate(networkID)
export const setCurrentNetowrk = apiMethodWrapper()((api: any) => (networkID: string) => {
return api.setCurrentNetowrk(networkID)
})

export const createNetwork = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.CreateNetworkParams) => {
return controller.create(params)
}
)
export const createNetwork = apiMethodWrapper()(api => (params: Controller.CreateNetworkParams) => {
return api.createNetwork(params)
})

export const updateNetwork = controllerMethodWrapper(CONTROLLER_NAME)(
controller => ({ networkID, options }: Controller.UpdateNetworkParams) => {
return controller.update(networkID, options)
}
)
export const updateNetwork = apiMethodWrapper()(api => ({ networkID, options }: Controller.UpdateNetworkParams) => {
return api.updateNetwork(networkID, options)
})

export const getAllNetworks = controllerMethodWrapper(CONTROLLER_NAME)(controller => () => {
return controller.getAll()
export const getAllNetworks = apiMethodWrapper()(api => () => {
return api.getAllNetworks()
})

export const getCurrentNetworkID = controllerMethodWrapper(CONTROLLER_NAME)(controller => () => {
return controller.currentID()
export const getCurrentNetworkID = apiMethodWrapper()(api => () => {
return api.getCurrentNetworkID()
})

export default {
Expand Down
12 changes: 4 additions & 8 deletions packages/neuron-ui/src/services/remote/skipDataAndType.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { controllerMethodWrapper } from './controllerMethodWrapper'
import { apiMethodWrapper } from './apiMethodWrapper'

const CONTROLLER_NAME = 'skip-data-and-type'

export const setSkipDataAndType = controllerMethodWrapper(CONTROLLER_NAME)(
(controller: any) => (params: Controller.SetSkipAndTypeParam) => {
return controller.update(params)
}
)
export const setSkipDataAndType = apiMethodWrapper()((api: any) => (params: Controller.SetSkipAndTypeParam) => {
return api.updateSkipDataAndType(params)
})

export default { setSkipDataAndType }
29 changes: 15 additions & 14 deletions packages/neuron-ui/src/services/remote/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { controllerMethodWrapper } from './controllerMethodWrapper'
import { apiMethodWrapper } from './apiMethodWrapper'

export interface GetTransactionListParams {
pageNo: number
Expand All @@ -7,26 +7,27 @@ export interface GetTransactionListParams {
walletID: string
}

const CONTROLLER_NAME = 'transactions'
export const getTransactionList = apiMethodWrapper()(api => (params: GetTransactionListParams) => {
return api.getTransactionList(params)
})

export const getTransactionList = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: GetTransactionListParams) => {
return controller.getAllByKeywords(params)
}
)
export const getTransaction = apiMethodWrapper()(api => ({ walletID, hash }: { walletID: string; hash: string }) => {
return api.getTransaction(walletID, hash)
})

export const getTransaction = controllerMethodWrapper(CONTROLLER_NAME)(
controller => ({ walletID, hash }: { walletID: string; hash: string }) => {
return controller.get(walletID, hash)
export const updateTransactionDescription = apiMethodWrapper()(
api => (params: Controller.UpdateTransactionDescriptionParams) => {
return api.updateTransactionDescription(params)
}
)
export const updateTransactionDescription = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.UpdateTransactionDescriptionParams) => {
return controller.updateDescription(params)
}

export const showTransactionDetails = apiMethodWrapper()(controller => (hash: string) =>
controller.showTransactionDetails(hash)
)

export default {
getTransactionList,
getTransaction,
updateTransactionDescription,
showTransactionDetails,
}
53 changes: 25 additions & 28 deletions packages/neuron-ui/src/services/remote/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
import { controllerMethodWrapper } from './controllerMethodWrapper'
import { apiMethodWrapper } from './apiMethodWrapper'

const CONTROLLER_NAME = 'wallets'

export const updateWallet = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.UpdateWalletParams) => controller.update(params)
export const updateWallet = apiMethodWrapper()(api => (params: Controller.UpdateWalletParams) =>
api.updateWallet(params)
)

export const getCurrentWallet = controllerMethodWrapper(CONTROLLER_NAME)(controller => () => controller.getCurrent())
export const getCurrentWallet = apiMethodWrapper()(api => () => api.getCurrentWallet())

export const getWalletList = controllerMethodWrapper(CONTROLLER_NAME)(controller => () => controller.getAll())
export const getWalletList = apiMethodWrapper()(api => () => api.getAllWallets())

export const createWallet = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.CreateWalletParams) => controller.create(params)
export const createWallet = apiMethodWrapper()(api => (params: Controller.CreateWalletParams) =>
api.createWallet(params)
)

export const importMnemonic = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.ImportMnemonicParams) => controller.importMnemonic(params)
export const importMnemonic = apiMethodWrapper()(api => (params: Controller.ImportMnemonicParams) =>
api.importMnemonic(params)
)

export const importKeystore = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.ImportKeystoreParams) => controller.importKeystore(params)
export const importKeystore = apiMethodWrapper()(api => (params: Controller.ImportKeystoreParams) =>
api.importKeystore(params)
)

export const deleteWallet = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.DeleteWalletParams) => controller.delete(params)
export const deleteWallet = apiMethodWrapper()(api => (params: Controller.DeleteWalletParams) =>
api.deleteWallet(params)
)

export const backupWallet = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.DeleteWalletParams) => controller.backup(params)
export const backupWallet = apiMethodWrapper()(api => (params: Controller.DeleteWalletParams) =>
api.backupWallet(params)
)

export const setCurrentWallet = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (id: Controller.SetCurrentWalletParams) => controller.activate(id)
)
export const sendCapacity = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.SendTransaction) => controller.sendCapacity(params)
export const setCurrentWallet = apiMethodWrapper()(api => (id: Controller.SetCurrentWalletParams) =>
api.setCurrentWallet(id)
)

export const getAddressesByWalletID = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (walletID: Controller.GetAddressesByWalletIDParams) => controller.getAllAddresses(walletID)
export const sendCapacity = apiMethodWrapper()(api => (params: Controller.SendTransaction) => api.sendCapacity(params))

export const getAddressesByWalletID = apiMethodWrapper()(api => (walletID: Controller.GetAddressesByWalletIDParams) =>
api.getAddressesByWalletID(walletID)
)

export const updateAddressDescription = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.UpdateAddressDescriptionParams) => controller.updateAddressDescription(params)
export const updateAddressDescription = apiMethodWrapper()(api => (params: Controller.UpdateAddressDescriptionParams) =>
api.updateAddressDescription(params)
)

export const calculateCycles = controllerMethodWrapper(CONTROLLER_NAME)(
controller => (params: Controller.ComputeCycles) => controller.computeCycles(params)
export const calculateCycles = apiMethodWrapper()(api => (params: Controller.ComputeCycles) =>
api.computeCycles(params)
)

export default {
Expand Down
Loading

0 comments on commit 500c37c

Please sign in to comment.