Skip to content

Commit

Permalink
Remove loop, instead hook into 'locked' state
Browse files Browse the repository at this point in the history
Change 'coldstaking' to 'ui:coldstaking'
  • Loading branch information
kewde committed Oct 31, 2017
1 parent 05e6afe commit 99d824c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
61 changes: 34 additions & 27 deletions src/app/core/rpc/rpc-state.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RPCStateClass {
this.lastBlockTimeState();
this.blockLoop();
this.walletLockedState();
this.coldStakeLoop();
this.coldStakeHook();
this.initWalletState();
}

Expand Down Expand Up @@ -50,36 +50,43 @@ export class RPCStateClass {
.set('locked', ['Locked', 'Unlocked, staking only'].includes(status)));
}

private coldStakeLoop() {
if (this.rpc.state.get('locked') === false) {
// only available if unlocked
this.rpc.call('walletsettings', ['changeaddress'])
.subscribe(
response => this.rpc.state.set('coldstaking',
response.changeaddress === 'default' ? false : !!response.changeaddress.coldstakingaddress
),
error => this.log.er('walletsettings changeaddress, returned an error', error));

}
/*
* coldStakeHook
* Subscribes to general unlock events and makes use of the time to
* update the coldstaking state.
*/
private coldStakeHook() {
this.rpc.state.observe('locked').subscribe(
locked => {
if (locked === false) {
// only available if unlocked
this.rpc.call('walletsettings', ['changeaddress'])
.subscribe(
// set state for coldstaking
response => this.rpc.state.set('ui:coldstaking',
response.changeaddress === 'default' ? false : !!response.changeaddress.coldstakingaddress
),
error => this.log.er('walletsettings changeaddress, returned an error', error));

setTimeout(this.coldStakeLoop.bind(this), 1000);
}
});
}

private initWalletState() {
private initWalletState() {

this.rpc.state.observe('encryptionstatus').take(1)
.subscribe(
status => {
const locked = this.rpc.state.get('locked');
this.rpc.state.observe('encryptionstatus').take(1)
.subscribe(
status => {
const locked = this.rpc.state.get('locked');

if (locked) {
this.rpc.state.set('walletInitialized', true);
return;
}
if (locked) {
this.rpc.state.set('walletInitialized', true);
return;
}

this.rpc.call('extkey', ['list'])
.subscribe(
response => {
this.rpc.call('extkey', ['list'])
.subscribe(
response => {
// check if account is active
if (response.result === 'No keys to list.') {
this.rpc.state.set('walletInitialized', false);
Expand All @@ -88,6 +95,6 @@ export class RPCStateClass {
}
},
error => this.log.er('RPC Call returned an error', error));
});
}
});
}
}
2 changes: 1 addition & 1 deletion src/app/overview/widgets/coldstake/coldstake.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ColdstakeComponent implements OnInit {
private _modals: ModalsService,
private _rpc: RPCService
) {
this._rpc.state.observe('coldstaking').subscribe(status => this.coldStakingEnabled = status);
this._rpc.state.observe('ui:coldstaking').subscribe(status => this.coldStakingEnabled = status);
}

ngOnInit() {
Expand Down

0 comments on commit 99d824c

Please sign in to comment.