From 4d2769dee4beff612ca3afc48a51e8d5279c99db Mon Sep 17 00:00:00 2001 From: cv <34649812+cpvalente@users.noreply.github.com> Date: Sun, 31 Oct 2021 12:53:46 +0100 Subject: [PATCH] fix: recalculate timers - recalculate timers in same update call if necessary - use new variable to keep track of upcoming rolling events without recalculating titles --- server/src/classes/EventTimer.js | 39 ++++++++++++++++++++++---------- server/src/classes/Timer.js | 2 ++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/server/src/classes/EventTimer.js b/server/src/classes/EventTimer.js index b47d15ee5..ee0d57607 100644 --- a/server/src/classes/EventTimer.js +++ b/server/src/classes/EventTimer.js @@ -232,21 +232,28 @@ export class EventTimer extends Timer { if (this.state !== 'roll') { super.update(); } else { - // get current time + // update timer as usual const now = this._getCurrentTime(); this.clock = now; - if (this.selectedEventId && this.current > 0) { - // update timer as usual + // something is running, update this.current = this._finishAt - now; - } else { - // look for event if none is loaded - if (this.current <= 0 || this.secondaryTimer <= 0) { - const newState = this.rollLoad(); + } else if (this.secondaryTimer > 0) { + // waiting to start, update secondary + this.secondaryTimer = this._secondaryTarget - now; + } - // broadcast state without recalling timer - if (newState) this.broadcastState(false); - } + // look for event if none is loaded + const currentRunning = this.current <= 0 && this.current !== null; + const secondaryRunning = + this.secondaryTimer <= 0 && this.secondaryTimer !== null; + + if (currentRunning || secondaryRunning) { + // look for events + this.rollLoad(); + + // broadcast state without recalling timer + this.broadcastState(false); } } @@ -566,6 +573,11 @@ export class EventTimer extends Timer { ? 'reload' : 'load'; this.loadEvent(this.selectedEventIndex, type); + } else if (e.id === this.nextEventId) { + // roll needs to recalculate + if (this.state === 'roll') { + this.rollLoad(); + } } else if ('title' in e || 'subtitle' in e || 'presenter') { // TODO: should be more selective on the need to load titles this._loadTitlesNext(); @@ -967,6 +979,8 @@ export class EventTimer extends Timer { // set timers this.secondaryTimer = null; + this._secondaryTarget = null; + this._startedAt = e.timeStart; this._finishAt = e.timeEnd; this.duration = e.timeEnd - e.timeStart; @@ -993,15 +1007,15 @@ export class EventTimer extends Timer { } } - // nothing to play next, unload + // nothing to play, unload if (foundNow == null && nextIndex == null) { this.unload(); console.log('Roll: no events found'); return; } + // we found something to play next if (nextIndex != null) { - // load titles const e = this._eventlist[nextIndex]; this._loadThisTitles(e, 'next'); @@ -1015,6 +1029,7 @@ export class EventTimer extends Timer { // timer counts to nextStart this.secondaryTimer = nextStart; + this._secondaryTarget = e.timeStart; } } } diff --git a/server/src/classes/Timer.js b/server/src/classes/Timer.js index b777063fa..543459c1e 100644 --- a/server/src/classes/Timer.js +++ b/server/src/classes/Timer.js @@ -12,6 +12,7 @@ export class Timer { current = null; timeTag = null; secondaryTimer = null; + _secondaryTarget = null; _finishAt = null; _finishedAt = null; _startedAt = null; @@ -122,6 +123,7 @@ export class Timer { this.current = this.duration; this.running = null; this.secondaryTimer = null; + this._secondaryTarget = null; this._finishAt = null; this._finishedAt = null; this._startedAt = null;