From e3fa3fe818f897049023b6690b25ac071b226a96 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:14:48 -0800 Subject: [PATCH] Chore: use rxjs instead of JS setInterval for timers (#8461) --- src-ui/messages.xlf | 22 ++++++++-------- .../components/admin/logs/logs.component.html | 2 +- .../admin/logs/logs.component.spec.ts | 10 ++------ .../components/admin/logs/logs.component.ts | 25 ++++++++----------- .../admin/tasks/tasks.component.html | 2 +- .../admin/tasks/tasks.component.spec.ts | 4 +-- .../components/admin/tasks/tasks.component.ts | 24 ++++++++---------- 7 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src-ui/messages.xlf b/src-ui/messages.xlf index d00235957..c0fe2eba2 100644 --- a/src-ui/messages.xlf +++ b/src-ui/messages.xlf @@ -1592,7 +1592,7 @@ src/app/components/admin/tasks/tasks.component.ts - 30 + 31 src/app/components/admin/trash/trash.component.html @@ -1818,7 +1818,7 @@ src/app/components/admin/tasks/tasks.component.ts - 122 + 129 @@ -1874,63 +1874,63 @@ Result src/app/components/admin/tasks/tasks.component.ts - 31 + 32 Dismiss selected src/app/components/admin/tasks/tasks.component.ts - 75 + 76 Dismiss all src/app/components/admin/tasks/tasks.component.ts - 76 + 77 Confirm Dismiss All src/app/components/admin/tasks/tasks.component.ts - 119 + 126 Dismiss all tasks? src/app/components/admin/tasks/tasks.component.ts - 120 + 127 queued src/app/components/admin/tasks/tasks.component.ts - 205 + 212 started src/app/components/admin/tasks/tasks.component.ts - 207 + 214 completed src/app/components/admin/tasks/tasks.component.ts - 209 + 216 failed src/app/components/admin/tasks/tasks.component.ts - 211 + 218 diff --git a/src-ui/src/app/components/admin/logs/logs.component.html b/src-ui/src/app/components/admin/logs/logs.component.html index efdba66d0..d6685d857 100644 --- a/src-ui/src/app/components/admin/logs/logs.component.html +++ b/src-ui/src/app/components/admin/logs/logs.component.html @@ -4,7 +4,7 @@ info="Review the log files for the application and for email checking." i18n-info>
- +
diff --git a/src-ui/src/app/components/admin/logs/logs.component.spec.ts b/src-ui/src/app/components/admin/logs/logs.component.spec.ts index 482eebb66..15e2590dc 100644 --- a/src-ui/src/app/components/admin/logs/logs.component.spec.ts +++ b/src-ui/src/app/components/admin/logs/logs.component.spec.ts @@ -1,9 +1,4 @@ -import { - ComponentFixture, - TestBed, - fakeAsync, - tick, -} from '@angular/core/testing' +import { ComponentFixture, TestBed } from '@angular/core/testing' import { LogService } from 'src/app/services/rest/log.service' import { PageHeaderComponent } from '../../common/page-header/page-header.component' import { LogsComponent } from './logs.component' @@ -90,8 +85,7 @@ describe('LogsComponent', () => { jest.advanceTimersByTime(6000) expect(reloadSpy).toHaveBeenCalledTimes(2) - component.toggleAutoRefresh() - expect(component.autoRefreshInterval).toBeNull() + component.autoRefreshEnabled = false jest.advanceTimersByTime(6000) expect(reloadSpy).toHaveBeenCalledTimes(2) }) diff --git a/src-ui/src/app/components/admin/logs/logs.component.ts b/src-ui/src/app/components/admin/logs/logs.component.ts index 508f7b3b7..58d54fbd4 100644 --- a/src-ui/src/app/components/admin/logs/logs.component.ts +++ b/src-ui/src/app/components/admin/logs/logs.component.ts @@ -6,7 +6,7 @@ import { ChangeDetectorRef, OnDestroy, } from '@angular/core' -import { takeUntil } from 'rxjs' +import { filter, takeUntil, timer } from 'rxjs' import { LogService } from 'src/app/services/rest/log.service' import { LoadingComponentWithPermissions } from '../../loading-component/loading.component' @@ -32,7 +32,7 @@ export class LogsComponent public activeLog: string - public autoRefreshInterval: any + public autoRefreshEnabled: boolean = true @ViewChild('logContainer') logContainer: ElementRef @@ -47,13 +47,19 @@ export class LogsComponent this.activeLog = this.logFiles[0] this.reloadLogs() } - this.toggleAutoRefresh() + timer(5000, 5000) + .pipe( + filter(() => this.autoRefreshEnabled), + takeUntil(this.unsubscribeNotifier) + ) + .subscribe(() => { + this.reloadLogs() + }) }) } ngOnDestroy(): void { super.ngOnDestroy() - clearInterval(this.autoRefreshInterval) } reloadLogs() { @@ -96,15 +102,4 @@ export class LogsComponent behavior: 'auto', }) } - - toggleAutoRefresh(): void { - if (this.autoRefreshInterval) { - clearInterval(this.autoRefreshInterval) - this.autoRefreshInterval = null - } else { - this.autoRefreshInterval = setInterval(() => { - this.reloadLogs() - }, 5000) - } - } } diff --git a/src-ui/src/app/components/admin/tasks/tasks.component.html b/src-ui/src/app/components/admin/tasks/tasks.component.html index f5bf125be..084195221 100644 --- a/src-ui/src/app/components/admin/tasks/tasks.component.html +++ b/src-ui/src/app/components/admin/tasks/tasks.component.html @@ -37,7 +37,7 @@
- +
diff --git a/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts b/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts index 061cc7e1c..5d4158666 100644 --- a/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts +++ b/src-ui/src/app/components/admin/tasks/tasks.component.spec.ts @@ -283,9 +283,7 @@ describe('TasksComponent', () => { expect(reloadSpy).toHaveBeenCalledTimes(1) jest.advanceTimersByTime(5000) expect(reloadSpy).toHaveBeenCalledTimes(2) - - component.toggleAutoRefresh() - expect(component.autoRefreshInterval).toBeNull() + component.autoRefreshEnabled = false jest.advanceTimersByTime(6000) expect(reloadSpy).toHaveBeenCalledTimes(2) }) diff --git a/src-ui/src/app/components/admin/tasks/tasks.component.ts b/src-ui/src/app/components/admin/tasks/tasks.component.ts index f88490a05..e8078fb77 100644 --- a/src-ui/src/app/components/admin/tasks/tasks.component.ts +++ b/src-ui/src/app/components/admin/tasks/tasks.component.ts @@ -8,6 +8,7 @@ import { first, Subject, takeUntil, + timer, } from 'rxjs' import { PaperlessTask } from 'src/app/data/paperless-task' import { TasksService } from 'src/app/services/tasks.service' @@ -48,7 +49,7 @@ export class TasksComponent public pageSize: number = 25 public page: number = 1 - public autoRefreshInterval: any + public autoRefreshEnabled: boolean = true private _filterText: string = '' get filterText() { @@ -86,7 +87,14 @@ export class TasksComponent ngOnInit() { this.tasksService.reload() - this.toggleAutoRefresh() + timer(5000, 5000) + .pipe( + filter(() => this.autoRefreshEnabled), + takeUntil(this.unsubscribeNotifier) + ) + .subscribe(() => { + this.tasksService.reload() + }) this.filterDebounce .pipe( @@ -101,7 +109,6 @@ export class TasksComponent ngOnDestroy() { super.ngOnDestroy() this.tasksService.cancelPending() - clearInterval(this.autoRefreshInterval) } dismissTask(task: PaperlessTask) { @@ -212,17 +219,6 @@ export class TasksComponent } } - toggleAutoRefresh(): void { - if (this.autoRefreshInterval) { - clearInterval(this.autoRefreshInterval) - this.autoRefreshInterval = null - } else { - this.autoRefreshInterval = setInterval(() => { - this.tasksService.reload() - }, 5000) - } - } - public resetFilter() { this._filterText = '' }