From b6b8a9e203f3db90e0f55d23e46e2234d24baeaf Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Tue, 10 Dec 2019 15:26:10 +0100 Subject: [PATCH] improving progress fetching --- .../ui/settings/scheduled-tasks.service.ts | 120 +++++++++--------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/src/frontend/app/ui/settings/scheduled-tasks.service.ts b/src/frontend/app/ui/settings/scheduled-tasks.service.ts index a36d065a..c191f81b 100644 --- a/src/frontend/app/ui/settings/scheduled-tasks.service.ts +++ b/src/frontend/app/ui/settings/scheduled-tasks.service.ts @@ -7,81 +7,81 @@ import {NetworkService} from '../../model/network/network.service'; export class ScheduledTasksService { - public progress: BehaviorSubject<{ [key: string]: TaskProgressDTO }>; - public onTaskFinish: EventEmitter = new EventEmitter(); - timer: number = null; - private subscribers = 0; + public progress: BehaviorSubject<{ [key: string]: TaskProgressDTO }>; + public onTaskFinish: EventEmitter = new EventEmitter(); + timer: number = null; + private subscribers = 0; - constructor(private _networkService: NetworkService) { - this.progress = new BehaviorSubject({}); - } + constructor(private _networkService: NetworkService) { + this.progress = new BehaviorSubject({}); + } - public calcTimeElapsed(progress: TaskProgressDTO) { - if (progress) { - return (progress.time.current - progress.time.start); - } + public calcTimeElapsed(progress: TaskProgressDTO) { + if (progress) { + return (progress.time.current - progress.time.start); } + } - public calcTimeLeft(progress: TaskProgressDTO) { - if (progress) { - return (progress.time.current - progress.time.start) / progress.progress * progress.left; - } + public calcTimeLeft(progress: TaskProgressDTO) { + if (progress) { + return (progress.time.current - progress.time.start) / progress.progress * progress.left; } + } - subscribeToProgress(): void { - this.incSubscribers(); - } + subscribeToProgress(): void { + this.incSubscribers(); + } - unsubscribeFromProgress(): void { - this.decSubscribers(); - } + unsubscribeFromProgress(): void { + this.decSubscribers(); + } - public forceUpdate() { - return this.getProgress(); - } + public forceUpdate() { + return this.getProgress(); + } - public async start(id: string, config?: any): Promise { - await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/start', {config: config}); - this.forceUpdate(); - } + public async start(id: string, config?: any): Promise { + await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/start', {config: config}); + this.forceUpdate(); + } - public async stop(id: string): Promise { - await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/stop'); - this.forceUpdate(); - } + public async stop(id: string): Promise { + await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/stop'); + this.forceUpdate(); + } - protected async getProgress() { - const prevPrg = this.progress.value; - this.progress.next(await this._networkService.getJson<{ [key: string]: TaskProgressDTO }>('/admin/tasks/scheduled/progress')); - for (const prg in prevPrg) { - if (!this.progress.value.hasOwnProperty(prg)) { - this.onTaskFinish.emit(prg); - } - } + protected async getProgress() { + const prevPrg = this.progress.value; + this.progress.next(await this._networkService.getJson<{ [key: string]: TaskProgressDTO }>('/admin/tasks/scheduled/progress')); + for (const prg in prevPrg) { + if (!this.progress.value.hasOwnProperty(prg)) { + this.onTaskFinish.emit(prg); + } } + } - protected getProgressPeriodically() { - if (this.timer != null || this.subscribers === 0) { - return; - } - let repeatTime = 5000; - if (Object.values(this.progress.value).length === 0) { - repeatTime = 10000; - } - this.timer = window.setTimeout(async () => { - await this.getProgress(); - this.timer = null; - this.getProgressPeriodically(); - }, repeatTime); + protected getProgressPeriodically() { + if (this.timer != null || this.subscribers === 0) { + return; } + let repeatTime = 5000; + if (Object.values(this.progress.value).length === 0) { + repeatTime = 10000; + } + this.timer = window.setTimeout(async () => { + this.timer = null; + this.getProgressPeriodically(); + }, repeatTime); + this.getProgress().catch(console.error); + } - private incSubscribers() { - this.subscribers++; - this.getProgressPeriodically(); - } + private incSubscribers() { + this.subscribers++; + this.getProgressPeriodically(); + } - private decSubscribers() { - this.subscribers--; - } + private decSubscribers() { + this.subscribers--; + } }