From b58330594094f1462f55ec40e5819a4fc3931525 Mon Sep 17 00:00:00 2001 From: Isaac Grynsztein Date: Tue, 21 Apr 2020 20:10:53 -0400 Subject: [PATCH] Downloads in the download manager now get updated smoothly, preventing the DOM from updating on object reassign --- .../downloads/downloads.component.ts | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/app/components/downloads/downloads.component.ts b/src/app/components/downloads/downloads.component.ts index f07d8d7..0cefc39 100644 --- a/src/app/components/downloads/downloads.component.ts +++ b/src/app/components/downloads/downloads.component.ts @@ -34,7 +34,7 @@ import { trigger, transition, animateChild, stagger, query, style, animate } fro export class DownloadsComponent implements OnInit { downloads_check_interval = 500; - downloads = null; + downloads = {}; keys = Object.keys; @@ -52,10 +52,7 @@ export class DownloadsComponent implements OnInit { getCurrentDownloads() { this.postsService.getCurrentDownloads().subscribe(res => { if (res['downloads']) { - if (JSON.stringify(this.downloads) !== JSON.stringify(res['downloads'])) { - // if they're not the same, then replace - this.downloads = res['downloads']; - } + this.assignNewValues(res['downloads']); } else { // failed to get downloads } @@ -89,6 +86,34 @@ export class DownloadsComponent implements OnInit { }); } + assignNewValues(new_downloads_by_session) { + const session_keys = Object.keys(new_downloads_by_session); + for (let i = 0; i < session_keys.length; i++) { + const session_id = session_keys[i]; + const session_downloads_by_id = new_downloads_by_session[session_id]; + const session_download_ids = Object.keys(session_downloads_by_id); + + if (!this.downloads[session_id]) { + this.downloads[session_id] = session_downloads_by_id; + } else { + for (let j = 0; j < session_download_ids.length; j++) { + const download_id = session_download_ids[j]; + const download = new_downloads_by_session[session_id][download_id] + if (!this.downloads[session_id][download_id]) { + this.downloads[session_id][download_id] = download; + } else { + const download_to_update = this.downloads[session_id][download_id]; + download_to_update['percent_complete'] = download['percent_complete']; + download_to_update['complete'] = download['complete']; + download_to_update['timestamp_end'] = download['timestamp_end']; + download_to_update['downloading'] = download['downloading']; + download_to_update['error'] = download['error']; + } + } + } + } + } + downloadsValid() { let valid = false; const keys = this.keys(this.downloads);