From d4024a16ad49e0a8d5d767343797b90f0d40edf1 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Mon, 2 Sep 2019 13:16:39 +0200 Subject: [PATCH] Delay updates while queue is running Closes #42 --- lib/manager/man.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/manager/man.ts b/lib/manager/man.ts index a2df999..34efcb4 100644 --- a/lib/manager/man.ts +++ b/lib/manager/man.ts @@ -12,17 +12,19 @@ import { Prefs } from "../prefs"; import { _ } from "../i18n"; import { CoalescedUpdate, mapFilterInSitu, filterInSitu } from "../util"; import { PromiseSerializer } from "../pserializer"; -import {Download} from "./download"; -import {ManagerPort} from "./port"; -import {Scheduler} from "./scheduler"; -import {Limits} from "./limits"; -import { downloads } from "../browser"; +import { Download } from "./download"; +import { ManagerPort } from "./port"; +import { Scheduler } from "./scheduler"; +import { Limits } from "./limits"; +import { downloads, runtime } from "../browser"; const AUTOSAVE_TIMEOUT = 2000; const DIRTY_TIMEOUT = 100; // eslint-disable-next-line no-magic-numbers const MISSING_TIMEOUT = 12 * 1000; +const RELOAD_TIMEOUT = 10 * 1000; + export class Manager extends EventEmitter { private items: Download[]; @@ -45,9 +47,12 @@ export class Manager extends EventEmitter { private scheduler: Scheduler | null; + private shouldReload: boolean; + constructor() { super(); this.active = true; + this.shouldReload = false; this.notifiedFinished = true; this.items = []; this.saveQueue = new CoalescedUpdate( @@ -91,6 +96,13 @@ export class Manager extends EventEmitter { await this.resetScheduler(); this.emit("inited"); setTimeout(() => this.checkMissing(), MISSING_TIMEOUT); + runtime.onUpdateAvailable.addListener(() => { + if (this.running.size) { + this.shouldReload = true; + return; + } + runtime.reload(); + }); return this; } @@ -169,6 +181,14 @@ export class Manager extends EventEmitter { } this.notifiedFinished = true; new Notification(null, _("queue-finished")); + if (this.shouldReload) { + setTimeout(() => { + if (this.running.size) { + return; + } + runtime.reload(); + }, RELOAD_TIMEOUT); + } } addManId(id: number, download: Download) {