From 9d30a09dfdc90a50bcc20c56b0bbba0f3e1c0724 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Thu, 22 Nov 2018 21:40:57 +0100 Subject: [PATCH] [enh] `sync`: also broadcast peers + sandbox progress through events --- app/modules/crawler/lib/sync.ts | 10 +++++----- app/modules/crawler/lib/sync/Watcher.ts | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/modules/crawler/lib/sync.ts b/app/modules/crawler/lib/sync.ts index a4b055f92..9d0fa1f51 100644 --- a/app/modules/crawler/lib/sync.ts +++ b/app/modules/crawler/lib/sync.ts @@ -43,11 +43,11 @@ export class Synchroniser extends stream.Duplex { // Wrapper to also push event stream this.watcher = new EventWatcher(interactive ? new MultimeterWatcher() : new LoggerWatcher(this.logger)) - this.watcher.onEvent('downloadChange', (pct: number) => this.push({ download: pct })) - this.watcher.onEvent('storageChange', (pct: number) => this.push({ saved: pct })) - this.watcher.onEvent('appliedChange', (pct: number) => this.push({ applied: pct })) - this.watcher.onEvent('sbxChange', (pct: number) => this.push({ sandbox: pct })) - this.watcher.onEvent('peersChange', (pct: number) => this.push({ peersSync: pct })) + this.watcher.onEvent('downloadChange', () => this.push(this.watcher.getStats())) + this.watcher.onEvent('storageChange', () => this.push(this.watcher.getStats())) + this.watcher.onEvent('appliedChange', () => this.push(this.watcher.getStats())) + this.watcher.onEvent('sbxChange', () => this.push(this.watcher.getStats())) + this.watcher.onEvent('peersChange', () => this.push(this.watcher.getStats())) this.syncStrategy.setWatcher(this.watcher) diff --git a/app/modules/crawler/lib/sync/Watcher.ts b/app/modules/crawler/lib/sync/Watcher.ts index 77da032b5..bbb5f113e 100644 --- a/app/modules/crawler/lib/sync/Watcher.ts +++ b/app/modules/crawler/lib/sync/Watcher.ts @@ -59,6 +59,16 @@ export class EventWatcher extends events.EventEmitter implements Watcher { onEvent(e: EventName, cb: (pct: number) => void) { this.on(e, cb) } + + getStats() { + return { + download: this.downloadPercent(), + saved: this.storagePercent(), + applied: this.appliedPercent(), + sandbox: this.sbxPercent(), + peersSync: this.peersPercent(), + } + } } export class MultimeterWatcher implements Watcher { @@ -173,6 +183,8 @@ export class LoggerWatcher implements Watcher { private downPct = 0 private savedPct = 0 private appliedPct = 0 + private sbxPct = 0 + private peersPct = 0 private lastMsg = "" constructor(private logger:any) { @@ -202,11 +214,17 @@ export class LoggerWatcher implements Watcher { } sbxPercent(pct:number) { - return 0 + if (pct > this.sbxPct) { + this.sbxPct = pct + } + return this.sbxPct } peersPercent(pct:number) { - return 0 + if (pct > this.peersPct) { + this.peersPct = pct + } + return this.peersPct } change(prop: 'downPct'|'savedPct'|'appliedPct', pct:number) { -- GitLab