From 24bf9c085af9729329bd39e1f85630449802ad49 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Sun, 12 May 2019 22:22:28 +0800 Subject: [PATCH] feat: listen network and address changes --- .../neuron-wallet/src/services/syncBlocks.ts | 10 +++- .../src/startup/loopTask/create.ts | 46 +++++++++++++++++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/packages/neuron-wallet/src/services/syncBlocks.ts b/packages/neuron-wallet/src/services/syncBlocks.ts index cf64c90e40..554308c44e 100644 --- a/packages/neuron-wallet/src/services/syncBlocks.ts +++ b/packages/neuron-wallet/src/services/syncBlocks.ts @@ -39,6 +39,8 @@ export default class SyncBlocksService { private tryBlocksTime = 0 + private stopFlag = false + constructor(lockHashes: string[]) { this.lockHashList = lockHashes } @@ -53,12 +55,16 @@ export default class SyncBlocksService { // continue to loop blocks, follow chain height async loopBlocks() { - const flag = true - while (flag) { + while (!this.stopFlag) { await this.resolveBatchBlocks() } } + // stop loop + stop() { + this.stopFlag = true + } + // resolve block to async resolveBatchBlocks() { // using database to sync currentBlockNumber value diff --git a/packages/neuron-wallet/src/startup/loopTask/create.ts b/packages/neuron-wallet/src/startup/loopTask/create.ts index 72c5047169..29a361dec6 100644 --- a/packages/neuron-wallet/src/startup/loopTask/create.ts +++ b/packages/neuron-wallet/src/startup/loopTask/create.ts @@ -1,5 +1,6 @@ import { BrowserWindow } from 'electron' import path from 'path' +import { Subject, BehaviorSubject } from 'rxjs' import SyncBlocksService from '../../services/syncBlocks' import initConnection from '../../typeorm' import Address from '../../services/addresses' @@ -7,6 +8,16 @@ import TransactionsService from '../../services/transactions' const loadURL = `file://${path.join(__dirname, 'index.html')}` +const stopLoopSubject = new Subject() + +// TODO: mock as an address subject +const addressChangeSubject = new Subject() + +// TODO: mock as an network init or change subject +// init-message means network not initialized +const networkChangeSubject = new BehaviorSubject('init-message') +networkChangeSubject.next('testnet') + // maybe should call this every time when new address generated // load all addresses and convert to lockHashes const loadAddressesAndConvert = async (): Promise => { @@ -19,6 +30,30 @@ const loadAddressesAndConvert = async (): Promise => { return lockHashes } +// call this after network switched +// TODO: listen to network switch +const switchNetwork = async (networkId: string) => { + // stop all blocks service + stopLoopSubject.next('stop') + // disconnect old connection and connect to new database + await initConnection(networkId) + // load lockHashes + const lockHashes: string[] = await loadAddressesAndConvert() + // start sync blocks service + const syncBlocksService = new SyncBlocksService(lockHashes) + + // TODO: should change to listen real address module event + addressChangeSubject.subscribe(async () => { + syncBlocksService.lockHashes = await loadAddressesAndConvert() + }) + + stopLoopSubject.subscribe(() => { + syncBlocksService.stop() + }) + + await syncBlocksService.loopBlocks() +} + // create a background task to sync transactions // this task is a renderer process const createLoopTask = () => { @@ -36,11 +71,12 @@ const createLoopTask = () => { loopWindow.on('ready-to-show', async () => { loopWindow!.hide() - // TODO: call this function after get network name - await initConnection('testnet') - const lockHashes: string[] = await loadAddressesAndConvert() - const syncBlocksService = new SyncBlocksService(lockHashes) - await syncBlocksService.loopBlocks() + // TODO: add an event on networkId for when it init and changed, it should broadcast a message with networkId + networkChangeSubject.subscribe(async (networkId: string) => { + if (networkId !== 'init-message') { + await switchNetwork(networkId) + } + }) }) loopWindow.on('closed', () => {