Skip to content

Commit

Permalink
fix: Fix the problem that balance not right if switch network from de…
Browse files Browse the repository at this point in the history
…fault network

Change WalletCreatedSubject from ReplaySubject to Subject, when switch network from default network, WalletCreatedSubject will start subscribe and replay the old created event which happend in default network, so two queue will started and balance will be incorrect
  • Loading branch information
classicalliu committed Nov 28, 2019
1 parent bbca5f7 commit 0f763a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ReplaySubject } from 'rxjs'
import { Subject } from 'rxjs'

export class WalletCreatedSubject {
static subject = new ReplaySubject<string>(1)
static subject = new Subject<string>()

static getSubject() {
return this.subject
}

static setSubject(subject: ReplaySubject<string>) {
static setSubject(subject: Subject<string>) {
this.subject = subject
}
}
Expand Down
18 changes: 12 additions & 6 deletions packages/neuron-wallet/src/startup/sync-block-task/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import DataUpdateSubject from 'models/subjects/data-update'
import logger from 'utils/logger'
import NodeService from 'services/node'
import NetworksService from 'services/networks'
import { distinctUntilChanged } from 'rxjs/operators'
import { distinctUntilChanged, pairwise, startWith } from 'rxjs/operators'
import LockUtils from 'models/lock-utils'
import DaoUtils from 'models/dao-utils'
import NetworkSwitchSubject from 'models/subjects/network-switch-subject'
Expand Down Expand Up @@ -59,11 +59,17 @@ const networkChange = async (network: NetworkWithID) => {

export const databaseInitSubject = new ReplaySubject<DatabaseInitParams>(1)

NetworkSwitchSubject.getSubject().subscribe(async (network: NetworkWithID | undefined) => {
if (network) {
await networkChange(network)
}
})
NetworkSwitchSubject
.getSubject()
.pipe(
startWith(undefined),
pairwise()
)
.subscribe(async ([previousNetwork, network]: (NetworkWithID | undefined)[]) => {
if ((!previousNetwork && network) || (previousNetwork && network && network.id !== previousNetwork.id)) {
await networkChange(network)
}
})

NodeService
.getInstance()
Expand Down

0 comments on commit 0f763a5

Please sign in to comment.