From 9875883fb3e10544186b27cb47b5894dd8df38bf Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Sun, 25 Oct 2020 20:26:40 +0800 Subject: [PATCH] TransferDataStore: fix initial transfer speed history population --- .../src/javascript/stores/TransferDataStore.ts | 16 ++++++++++++---- server/services/historyService.ts | 10 ++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/client/src/javascript/stores/TransferDataStore.ts b/client/src/javascript/stores/TransferDataStore.ts index 19c1e392..b0e1accf 100644 --- a/client/src/javascript/stores/TransferDataStore.ts +++ b/client/src/javascript/stores/TransferDataStore.ts @@ -6,7 +6,12 @@ import type {TransferDirection, TransferHistory, TransferSummary} from '@shared/ export const TRANSFER_DIRECTIONS: Readonly> = ['download', 'upload'] as const; class TransferDataStoreClass { - transferRates: TransferHistory = {download: [], upload: [], timestamps: []}; + transferRates: TransferHistory = { + download: new Array(30).fill(0), + upload: new Array(30).fill(0), + timestamps: new Array(30).fill(Date.now()), + }; + transferSummary: TransferSummary = { downRate: 0, downThrottle: 0, @@ -25,13 +30,16 @@ class TransferDataStoreClass { const upload = this.transferRates.upload.slice(); const timestamps = this.transferRates.timestamps.slice(); - download.shift(); download.push(this.transferSummary.downRate); - upload.shift(); upload.push(this.transferSummary.upRate); - timestamps.shift(); timestamps.push(Date.now()); + if (timestamps.length > 30) { + download.shift(); + upload.shift(); + timestamps.shift(); + } + this.transferRates = {download, upload, timestamps}; } diff --git a/server/services/historyService.ts b/server/services/historyService.ts index 328fa54e..6fd57809 100644 --- a/server/services/historyService.ts +++ b/server/services/historyService.ts @@ -152,12 +152,10 @@ class HistoryService extends BaseService { handleFetchTransferSummarySuccess(nextTransferSummary: TransferSummary) { const summaryDiff = jsonpatch.compare(this.transferSummary, nextTransferSummary); - if (summaryDiff.length > 0) { - this.emit('TRANSFER_SUMMARY_DIFF_CHANGE', { - diff: summaryDiff, - id: Date.now(), - }); - } + this.emit('TRANSFER_SUMMARY_DIFF_CHANGE', { + diff: summaryDiff, + id: Date.now(), + }); this.errorCount = 0; this.transferSummary = nextTransferSummary;