Skip to content

Commit

Permalink
only subscribe to websocket when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-wilson committed Nov 26, 2023
1 parent 1879a84 commit fe97e56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ <h2>Results</h2>
</div>

<div class="card mt-2">
<h2>Realtime Logs <button (click)="showLogs = !showLogs" style="margin-left: 15px;"
class="btn btn-secondary">{{showLogs ? 'Hide': 'Show'}}
<h2>Realtime Logs <button (click)="toggleLogs()" style="margin-left: 15px;" class="btn btn-secondary">{{showLogs
? 'Hide': 'Show'}}
Logs</button></h2>

<div *ngIf="showLogs" id="logs" #scrollContainer>
Expand Down
29 changes: 19 additions & 10 deletions main/http_server/axe-os/src/app/components/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class HomeComponent implements AfterViewChecked, OnDestroy {

public logs: string[] = [];

private websocketSubscription: Subscription;
private websocketSubscription?: Subscription;

public showLogs = false;

Expand Down Expand Up @@ -47,26 +47,35 @@ export class HomeComponent implements AfterViewChecked, OnDestroy {
})
)

this.websocketSubscription = this.websocketService.ws$.subscribe({
next: (val) => {
this.logs.push(val);
if (this.logs.length > 100) {
this.logs.shift();
}

}
})

}
ngOnDestroy(): void {
this.websocketSubscription.unsubscribe();
this.websocketSubscription?.unsubscribe();
}
ngAfterViewChecked(): void {
if (this.scrollContainer?.nativeElement != null) {
this.scrollContainer.nativeElement.scrollTo({ left: 0, top: this.scrollContainer.nativeElement.scrollHeight, behavior: 'smooth' });
}
}

public toggleLogs() {
this.showLogs = !this.showLogs;

if (this.showLogs) {
this.websocketSubscription = this.websocketService.ws$.subscribe({
next: (val) => {
this.logs.push(val);
if (this.logs.length > 100) {
this.logs.shift();
}
}
})
} else {
this.websocketSubscription?.unsubscribe();
}
}


}

0 comments on commit fe97e56

Please sign in to comment.