Skip to content

Commit

Permalink
feat: start/stop syncing with sync controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Nov 17, 2019
1 parent 7dfe670 commit afde49d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 35 deletions.
12 changes: 7 additions & 5 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import env from 'env'
import i18n from 'utils/i18n'
import { popContextMenu } from './app/menu'
import { showWindow } from './app/show-window'
import { TransactionsController, WalletsController, SyncInfoController, NetworksController } from 'controllers'
import { TransactionsController, WalletsController, SyncController, NetworksController } from 'controllers'
import { NetworkType, NetworkID, Network } from 'types/network'
import NetworksService from 'services/networks'
import WalletsService from 'services/wallets'
Expand Down Expand Up @@ -40,7 +40,7 @@ export default class ApiController {
networksService.getCurrentID(),
networksService.getAll(),

SyncInfoController.currentBlockNumber()
SyncController.currentBlockNumber()
.then(res => {
if (res.status) {
return res.result.currentBlockNumber
Expand Down Expand Up @@ -297,10 +297,12 @@ export default class ApiController {
) {
return DaoController.getDaoCells(params)
}

// settings
@MapApiResponse
public static async clearCellCache () {
return Promise.resolve()
public static async clearCellCache() {
await SyncController.stopSyncing()
// TODO: remove cache
return SyncController.startSyncing()
}
}
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AppController from './app'
import NetworksController from './networks'
import WalletsController from './wallets'
import TransactionsController from './transactions'
import SyncInfoController from './sync-info'
import SyncController from './sync'
import UpdateController from './update'

import ApiController from './api'
Expand All @@ -12,7 +12,7 @@ export {
NetworksController,
WalletsController,
TransactionsController,
SyncInfoController,
SyncController,
UpdateController,
ApiController,
}
16 changes: 0 additions & 16 deletions packages/neuron-wallet/src/controllers/sync-info.ts

This file was deleted.

35 changes: 35 additions & 0 deletions packages/neuron-wallet/src/controllers/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import BlockNumber from 'services/sync/block-number'
import { createSyncBlockTask, killSyncBlockTask } from 'startup/sync-block-task/create'
import { ResponseCode } from 'utils/const'

export default class SyncController {
public static async startSyncing() {
createSyncBlockTask()

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

public static async stopSyncing() {
killSyncBlockTask()

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

public static async currentBlockNumber() {
const blockNumber = new BlockNumber()
const current: bigint = await blockNumber.getCurrent()

return {
status: ResponseCode.Success,
result: {
currentBlockNumber: current.toString(),
},
}
}
}
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { AddInputIndexToInput1573461100330 } from './migrations/1573461100330-Ad

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'

const dbPath = (networkName: string): string => {
const name = `cell-${networkName}.sqlite`
return path.join(env.fileBasePath, 'cells', name)
const dbPath = (name: string): string => {
const filename = `cell-${name}.sqlite`
return path.join(env.fileBasePath, 'cells', filename)
}

const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectionOptions> => {
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { app } from 'electron'

import AppController from 'controllers/app'
import SyncController from 'controllers/sync'
import WalletService from 'services/wallets'
import createSyncBlockTask from 'startup/sync-block-task/create'
import { changeLanguage } from 'utils/i18n'

const appController = new AppController()
Expand All @@ -11,7 +11,7 @@ app.on('ready', async () => {
changeLanguage(app.getLocale())

WalletService.getInstance().generateAddressesIfNecessary()
createSyncBlockTask()
SyncController.startSyncing()

appController.openWindow()
})
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-wallet/src/services/sync/renderer-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { remote } from 'electron'

export const { networkSwitchSubject, nodeService, addressChangeSubject, addressesUsedSubject } = remote.require(
export const {
networkSwitchSubject, nodeService, addressChangeSubject, addressesUsedSubject
} = remote.require(
'./startup/sync-block-task/params'
)
22 changes: 17 additions & 5 deletions packages/neuron-wallet/src/startup/sync-block-task/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@ const loadURL = `file://${path.join(__dirname, 'index.html')}`

export { networkSwitchSubject }

let syncBlockBackgroundWindow: BrowserWindow | null

// create a background task to sync transactions
// this task is a renderer process
const createSyncBlockTask = () => {
let syncBlockBackgroundWindow: BrowserWindow | null = new BrowserWindow({
export const createSyncBlockTask = () => {
if (syncBlockBackgroundWindow) {
return
}

console.info('Start sync block background process')
syncBlockBackgroundWindow = new BrowserWindow({
width: 1366,
height: 768,
show: false,
Expand All @@ -86,8 +93,6 @@ const createSyncBlockTask = () => {
},
})

syncBlockBackgroundWindow.loadURL(loadURL)

syncBlockBackgroundWindow.on('ready-to-show', async () => {
if (env.isDevMode && process.env.DEV_SYNC_TASK) {
syncBlockBackgroundWindow!.show()
Expand All @@ -100,7 +105,14 @@ const createSyncBlockTask = () => {
syncBlockBackgroundWindow = null
})

syncBlockBackgroundWindow.loadURL(loadURL)

return syncBlockBackgroundWindow
}

export default createSyncBlockTask
export const killSyncBlockTask = async () => {
if (syncBlockBackgroundWindow) {
console.info('Kill sync block background process')
syncBlockBackgroundWindow.close()
}
}
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/startup/sync-block-task/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ export const switchNetwork = async (url: string, genesisBlockHash: string, _chai
})

blockListener.start()
}
}

0 comments on commit afde49d

Please sign in to comment.