Skip to content

Commit

Permalink
fix: Disable btn while refreshing, better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mrv777 committed Nov 10, 2024
1 parent 13ad7c7 commit 20eac23
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
<div class="flex gap-3 justify-content-between">
<div class="flex gap-1 sm:gap-3 text-sm md:text-base">
<button pButton (click)="scanNetwork()" [disabled]="scanning">{{scanning ? 'Scanning...' : 'Automatic Scan'}}</button>
<button pButton severity="secondary" (click)="refreshList()" [disabled]="scanning">Refresh List ({{refreshIntervalTime}})</button>
<button pButton severity="secondary" (click)="refreshList()" [disabled]="scanning || isRefreshing">
{{isRefreshing ? 'Refreshing...' : 'Refresh List (' + refreshIntervalTime + ')'}}
</button>
</div>
<div class="text-sm md:text-base">
Total Hash Rate: <span class="text-primary">{{totalHashRate * 1000000000 | hashSuffix}}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class SwarmComponent implements OnInit, OnDestroy {

public totalHashRate: number = 0;

public isRefreshing = false;

constructor(
private fb: FormBuilder,
private systemService: SystemService,
Expand All @@ -54,9 +56,11 @@ export class SwarmComponent implements OnInit, OnDestroy {
}

this.refreshIntervalRef = window.setInterval(() => {
this.refreshIntervalTime --;
if(this.refreshIntervalTime <= 0){
this.refreshList();
if (!this.isRefreshing) {
this.refreshIntervalTime--;
if (this.refreshIntervalTime <= 0) {
this.refreshList();
}
}
}, 1000);
}
Expand Down Expand Up @@ -168,6 +172,7 @@ export class SwarmComponent implements OnInit, OnDestroy {
public refreshList() {
this.refreshIntervalTime = REFRESH_TIME_SECONDS;
const ips = this.swarm.map(axeOs => axeOs.IP);
this.isRefreshing = true;

from(ips).pipe(
mergeMap(ipAddr =>
Expand All @@ -180,8 +185,21 @@ export class SwarmComponent implements OnInit, OnDestroy {
}),
timeout(5000),
catchError(error => {
this.toastr.error('Error', 'Failed to get info from ' + ipAddr + ' - ' + error);
return of(this.swarm.find(axeOs => axeOs.IP == ipAddr));
const errorMessage = error?.message || error?.statusText || error?.toString() || 'Unknown error';
this.toastr.error('Failed to get info from ' + ipAddr, errorMessage);
// Return existing device with zeroed stats instead of the previous state
const existingDevice = this.swarm.find(axeOs => axeOs.IP === ipAddr);
return of({
...existingDevice,
hashRate: 0,
sharesAccepted: 0,
power: 0,
voltage: 0,
temp: 0,
bestDiff: 0,
version: 0,
uptimeSeconds: 0,
});
})
),
128 // Limit concurrency to avoid overload
Expand All @@ -192,11 +210,12 @@ export class SwarmComponent implements OnInit, OnDestroy {
this.swarm = result.sort(this.sortByIp.bind(this));
this.localStorageService.setObject(SWARM_DATA, this.swarm);
this.calculateTotalHashRate();
this.isRefreshing = false;
},
complete: () => {
this.isRefreshing = false;
}
});

}

private sortByIp(a: any, b: any): number {
Expand Down

0 comments on commit 20eac23

Please sign in to comment.