bugfix + cleanup

+ fixed bugs on counting finishing time and event loading
+ small cleanups on stack
This commit is contained in:
cv
2021-05-02 11:03:56 +02:00
parent a12f68b48d
commit 7f94907056
2 changed files with 73 additions and 74 deletions
+44 -56
View File
@@ -113,12 +113,14 @@ class EventTimer extends Timer {
// if there is nothing selected, no nothing // if there is nothing selected, no nothing
if (this.selectedEventId == null) return; if (this.selectedEventId == null) return;
super.start(); super.start();
this.broadcastState();
} }
pause() { pause() {
// if there is nothing selected, no nothing // if there is nothing selected, no nothing
if (this.selectedEventId == null) return; if (this.selectedEventId == null) return;
super.pause(); super.pause();
this.broadcastState();
} }
_setterManager(action, payload) { _setterManager(action, payload) {
@@ -342,14 +344,15 @@ class EventTimer extends Timer {
setupWithEventList(eventlist) { setupWithEventList(eventlist) {
// filter only events // filter only events
const events = eventlist.filter((e) => e.type === 'event'); const events = eventlist.filter((e) => e.type === 'event');
const numEvents = events.length; const numEvents = events.length;
if (numEvents < 1) return;
// set general // set general
this._eventList = events; this._eventList = events;
this.numEvents = numEvents; this.numEvents = numEvents;
// list may be empty
if (numEvents < 1) return;
// load first event // load first event
this.loadEvent(0); this.loadEvent(0);
} }
@@ -357,10 +360,17 @@ class EventTimer extends Timer {
updateEventList(eventlist) { updateEventList(eventlist) {
// filter only events // filter only events
const events = eventlist.filter((e) => e.type === 'event'); const events = eventlist.filter((e) => e.type === 'event');
const numEvents = events.length;
// set general // set general
this._eventList = events; this._eventList = events;
this.numEvents = events.length; this.numEvents = numEvents;
// list may be empty
if (numEvents < 1) {
this.unload();
return;
}
// handle reload selected // handle reload selected
if (this.selectedEventId != null) { if (this.selectedEventId != null) {
@@ -377,7 +387,7 @@ class EventTimer extends Timer {
} }
// Reload data if running // Reload data if running
let type = this._startedAt != null ? 'reload' : 'load'; const type = this._startedAt != null ? 'reload' : 'load';
this.loadEvent(eventIndex, type); this.loadEvent(eventIndex, type);
} }
this.broadcastState(); this.broadcastState();
@@ -393,23 +403,23 @@ class EventTimer extends Timer {
this._eventList[eventIndex] = { ...e, ...entry }; this._eventList[eventIndex] = { ...e, ...entry };
try { try {
// check if entry is running // check if entry is running
if (e.id === this.selectedEventId) { if (e.id === this.selectedEventId) {
// handle reload selected // handle reload selected
// Reload data if running // Reload data if running
let type = let type =
this.selectedEventId === id && this._startedAt != null this.selectedEventId === id && this._startedAt != null
? 'reload' ? 'reload'
: 'load'; : 'load';
this.loadEvent(this.selectedEvent, type); this.loadEvent(this.selectedEvent, type);
} else if ('title' in e || 'subtitle' in e || 'presenter') { } else if ('title' in e || 'subtitle' in e || 'presenter') {
// TODO: should be more selective on the need to load titles // TODO: should be more selective on the need to load titles
this._loadTitlesNext(); this._loadTitlesNext();
this._loadTitlesNow(); this._loadTitlesNow();
}
} catch (error) {
console.log(error);
} }
} catch (error) {
console.log(error)
}
this.broadcastState(); this.broadcastState();
} }
@@ -429,6 +439,7 @@ class EventTimer extends Timer {
// time stuff changes on wheter we keep the running clock // time stuff changes on wheter we keep the running clock
if (type === 'load') { if (type === 'load') {
this._resetTimers(); this._resetTimers();
this.duration = end - start; this.duration = end - start;
this.current = this.duration; this.current = this.duration;
this.selectedEvent = eventIndex; this.selectedEvent = eventIndex;
@@ -451,7 +462,7 @@ class EventTimer extends Timer {
_loadTitlesNow() { _loadTitlesNow() {
const e = this._eventList[this.selectedEvent]; const e = this._eventList[this.selectedEvent];
if (e == null) return if (e == null) return;
// private title is always current // private title is always current
this.titles.titleNow = e.title; this.titles.titleNow = e.title;
@@ -493,7 +504,7 @@ class EventTimer extends Timer {
_loadTitlesNext() { _loadTitlesNext() {
// maybe there is nothing to load // maybe there is nothing to load
if (this.selectedEvent == null) return if (this.selectedEvent == null) return;
// assume there is no next event // assume there is no next event
this.titles.titleNext = null; this.titles.titleNext = null;
@@ -658,14 +669,6 @@ class EventTimer extends Timer {
this.broadcastState(); this.broadcastState();
} }
goto(eventIndex) {
// load event
this.loadEvent(eventIndex);
// broadcast current state
this.broadcastState();
}
roll() { roll() {
console.log('roll: not yet implemented'); console.log('roll: not yet implemented');
return false; return false;
@@ -676,33 +679,27 @@ class EventTimer extends Timer {
// check that we have events to run // check that we have events to run
if (this.numEvents < 1) return; if (this.numEvents < 1) return;
// change playstate
this.state = 'pause';
// if there is no event running, go to first // if there is no event running, go to first
if (this.selectedEvent == null) { if (this.selectedEvent == null) {
this.goto(0); this.loadEvent(0);
return; return;
} }
const gotoEvent = this.selectedEvent > 0 ? this.selectedEvent - 1 : 0; const gotoEvent = this.selectedEvent > 0 ? this.selectedEvent - 1 : 0;
if (gotoEvent === this.selectedEvent) return; if (gotoEvent === this.selectedEvent) return;
this.goto(gotoEvent); this.loadEvent(gotoEvent);
// broadcast current state // change playstate
this.broadcastState(); this.pause();
} }
next() { next() {
// check that we have events to run // check that we have events to run
if (this.numEvents < 1) return; if (this.numEvents < 1) return;
// change playstate
this.state = 'pause';
// if there is no event running, go to first // if there is no event running, go to first
if (this.selectedEvent == null) { if (this.selectedEvent == null) {
this.goto(0); this.loadEvent(0);
return; return;
} }
@@ -712,27 +709,21 @@ class EventTimer extends Timer {
: this.numEvents - 1; : this.numEvents - 1;
if (gotoEvent === this.selectedEvent) return; if (gotoEvent === this.selectedEvent) return;
this.goto(gotoEvent); this.loadEvent(gotoEvent);
// broadcast current state // change playstate
this.broadcastState(); this.pause();
} }
unload() { unload() {
// reset duration // reset duration
this.duration = null; this.duration = null;
// reset timers
this._resetTimers();
// reset playstate
this.state = 'stop';
// reset selected // reset selected
this._resetSelection(); this._resetSelection();
// broadcast current state // reset playstate
this.broadcastState(); this.stop();
} }
reload() { reload() {
@@ -740,10 +731,7 @@ class EventTimer extends Timer {
this.loadEvent(this.selectedEvent); this.loadEvent(this.selectedEvent);
// reset playstate // reset playstate
this.state = 'pause'; this.pause();
// broadcast current state
this.broadcastState();
} }
} }
+29 -18
View File
@@ -13,8 +13,8 @@ class Timer {
_pausedAt = null; _pausedAt = null;
_pausedInterval = null; _pausedInterval = null;
_pausedTotal = null; _pausedTotal = null;
_delays = null;
state = 'stop'; state = 'stop';
showNegative = false;
constructor() {} constructor() {}
@@ -38,6 +38,7 @@ class Timer {
this._pausedInterval = 0; this._pausedInterval = 0;
} }
this._pausedTotal = 0; this._pausedTotal = 0;
this._delays = 0;
this.update(); this.update();
} }
@@ -51,16 +52,13 @@ class Timer {
switch (this.state) { switch (this.state) {
case 'start': case 'start':
// update current timer // update current timer
if (!this.showNegative) { this.current = Math.max(this._finishAt + this._pausedTotal - now, 0);
this.current = Math.max(this._finishAt + this._pausedTotal - now, 0);
} else {
this.current = this._finishAt + this._pausedTotal - now;
}
break; break;
case 'pause': case 'pause':
if (this.current <= 0) return;
// update paused time // update paused time
this._pausedInterval = now - this._pausedAt; if (this.current > 0) {
this._pausedInterval = now - this._pausedAt;
}
break; break;
case 'stop': case 'stop':
// nothing here yet // nothing here yet
@@ -80,8 +78,8 @@ class Timer {
// get current time in epoc // get current time in epoc
_getCurrentTime() { _getCurrentTime() {
// date today at midnight // date today at midnight
let now = new Date(); const now = new Date();
let midnight = new Date(now).setHours(0, 0, 0); const midnight = new Date(now).setHours(0, 0, 0);
// return diffence // return diffence
return now - midnight; return now - midnight;
@@ -92,13 +90,14 @@ class Timer {
return this._finishAt + (this._pausedInterval + this._pausedTotal); return this._finishAt + (this._pausedInterval + this._pausedTotal);
} }
_resetTimers() { _resetTimers(resetDelay = true) {
this.current = this.duration; this.current = this.duration;
this._finishAt = null; this._finishAt = null;
this._startedAt = null; this._startedAt = null;
this._pausedAt = null; this._pausedAt = null;
this._pausedInterval = null; this._pausedInterval = null;
this._pausedTotal = null; this._pausedTotal = null;
if (resetDelay) this._delays = null;
} }
// get elapsed time // get elapsed time
@@ -132,10 +131,15 @@ class Timer {
if (this.state === 'start') return; if (this.state === 'start') return;
else if (this.duration <= 0) return; else if (this.duration <= 0) return;
else if (this._startedAt == null) { else if (this._startedAt == null) {
this._resetTimers(); // it hasnt started yet
const now = this._getCurrentTime(); const now = this._getCurrentTime();
// set start time as now
this._startedAt = now; this._startedAt = now;
// calculate expected finish time
this._finishAt = now + this.duration; this._finishAt = now + this.duration;
// reset pauses
this._pausedTotal = null;
this._pausedInterval = null;
} else { } else {
// check if there is paused time // check if there is paused time
if (this._pausedInterval) { if (this._pausedInterval) {
@@ -143,6 +147,7 @@ class Timer {
this._pausedInterval = null; this._pausedInterval = null;
} }
} }
// change state // change state
this.state = 'start'; this.state = 'start';
} }
@@ -150,35 +155,41 @@ class Timer {
pause() { pause() {
// do we need to change // do we need to change
if (this.state === 'pause') return; if (this.state === 'pause') return;
else if (this.duration === 0) return;
// update pause time // if there is already paused time (shouldnt)
if (this._pausedInterval) {
console.log('TIMER: it was not paused and had pausedInterval');
this._pausedTotal += this._pausedInterval;
this._pausedInterval = null;
}
// set pause time
this._pausedAt = this._getCurrentTime(); this._pausedAt = this._getCurrentTime();
// change state // change state
this.state = 'pause'; this.state = 'pause';
} }
stop() { stop() {
// do we need to change // do we need to change
if (this.state === 'stop') return; if (this.state === 'stop') return;
// clear all timers // clear all timers
this._resetTimers(); this._resetTimers();
this.state = 'stop'; this.state = 'stop';
} }
increment(amount) { increment(amount) {
if (amount < 0) { if (amount < 0) {
if (Math.abs(amount) > this.current) { if (Math.abs(amount) > this.current) {
console.log('ot os'); this._delays -= this.current;
this.current = 0; this.current = 0;
this._finishAt = this._getCurrentTime(); this._finishAt = this._getCurrentTime();
return; return;
} }
} }
this.current += amount; this._delays += amount;
this._finishAt += amount; this._finishAt += amount;
} }
} }