Skip to content

Commit

Permalink
feat: Add ApiController
Browse files Browse the repository at this point in the history
* neuron-ui talks to remote/api instead of remote/app
* AppController is used as instance, not static functions object
* Detach menu from app controller
* Remove controller and UI action dependency from services
  • Loading branch information
ashchan committed Oct 21, 2019
1 parent 69d2b8b commit 7a00d33
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 322 deletions.
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/services/remote/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { controllerMethodWrapper } from './controllerMethodWrapper'

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

export const handleViewError = controllerMethodWrapper(CONTROLLER_NAME)(controller => (errorMessage: string) =>
Expand Down
121 changes: 121 additions & 0 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { take } from 'rxjs/operators'

import env from 'env'
import i18n from 'utils/i18n'
import { popContextMenu } from './app/menu'
import { showWindow } from './app/show-window'
import { TransactionsController, WalletsController, SyncInfoController } from 'controllers'
import NetworksService from 'services/networks'
import WalletsService from 'services/wallets'
import SkipDataAndType from 'services/settings/skip-data-and-type'
import { ConnectionStatusSubject } from 'models/subjects/node'
import { SystemScriptSubject } from 'models/subjects/system-script'
import { ResponseCode } from 'utils/const'

export default class ApiController {
public static loadInitData = async () => {
const walletsService = WalletsService.getInstance()
const networksService = NetworksService.getInstance()
const [
currentWallet = null,
wallets = [],
currentNetworkID = '',
networks = [],
syncedBlockNumber = '0',
connectionStatus = false,
codeHash = '',
] = await Promise.all([
walletsService.getCurrent(),
walletsService.getAll(),
networksService.getCurrentID(),
networksService.getAll(),

SyncInfoController.currentBlockNumber()
.then(res => {
if (res.status) {
return res.result.currentBlockNumber
}
return '0'
})
.catch(() => '0'),
new Promise(resolve => {
ConnectionStatusSubject.pipe(take(1)).subscribe(
status => {
resolve(status)
},
() => {
resolve(false)
},
)
}),
new Promise(resolve => {
SystemScriptSubject.pipe(take(1)).subscribe(({ codeHash: currentCodeHash }) => resolve(currentCodeHash))
}),
])

const minerAddresses = await Promise.all(
wallets.map(({ id }) =>
WalletsController.getAllAddresses(id).then(addrRes => {
if (addrRes.result) {
const minerAddr = addrRes.result.find(addr => addr.type === 0 && addr.index === 0)
if (minerAddr) {
return {
address: minerAddr.address,
identifier: minerAddr.identifier,
}
}
}
return undefined
}),
),
)
const addresses: Controller.Address[] = await (currentWallet
? WalletsController.getAllAddresses(currentWallet.id).then(res => res.result)
: [])

const transactions = currentWallet
? await TransactionsController.getAllByKeywords({
pageNo: 1,
pageSize: 15,
keywords: '',
walletID: currentWallet.id,
}).then(res => res.result)
: []

const skipDataAndType = SkipDataAndType.getInstance().get()

const initState = {
currentWallet,
wallets: [...wallets.map(({ name, id }, idx: number) => ({ id, name, minerAddress: minerAddresses[idx] }))],
currentNetworkID,
networks,
addresses,
transactions,
syncedBlockNumber,
connectionStatus,
codeHash,
skipDataAndType,
}

return { status: ResponseCode.Success, result: initState }
}

public static handleViewError = (error: string) => {
if (env.isDevMode) {
console.error(error)
}
}

public static isMainWindow = (winID: number) => {
// TODO: Fix this
return winID === 0 // AppController.mainWindow && winID === AppController.mainWindow.id
}

public static async contextMenu(params: { type: string; id: string }) {
return popContextMenu(params)
}

public static async showTransactionDetails(hash: string) {
showWindow(`${env.mainURL}#/transaction/${hash}`, i18n.t(`messageBox.transaction.title`, { hash }))
}
}
Loading

0 comments on commit 7a00d33

Please sign in to comment.