feat: reload on change

This commit is contained in:
cv
2021-04-24 23:24:36 +02:00
parent 01eec95637
commit 65ffd09410
4 changed files with 78 additions and 10 deletions
+66 -7
View File
@@ -76,7 +76,7 @@ class EventTimer extends Timer {
// broadcast state // broadcast state
broadcastState() { broadcastState() {
this.io.emit('timer', this.getObject()); this.io.emit('timer', this.getObject());
this.io.emit('playstate', this.playState); this.io.emit('playstate', this.state);
this.io.emit('selected-id', this.selectedEventId); this.io.emit('selected-id', this.selectedEventId);
this.io.emit('titles', this.titles); this.io.emit('titles', this.titles);
} }
@@ -121,7 +121,7 @@ class EventTimer extends Timer {
// Not yet implemented // Not yet implemented
// else if (payload === 'roll') this.roll(); // else if (payload === 'roll') this.roll();
this.broadcastThis('playstate', this.playState); this.broadcastThis('playstate', this.state);
this.broadcastThis('selected-id', this.selectedEventId); this.broadcastThis('selected-id', this.selectedEventId);
this.broadcastThis('titles', this.titles); this.broadcastThis('titles', this.titles);
@@ -179,7 +179,7 @@ class EventTimer extends Timer {
// send state // send state
socket.emit('timer', this.getObject()); socket.emit('timer', this.getObject());
socket.emit('playstate', this.playState); socket.emit('playstate', this.state);
socket.emit('selected-id', this.selectedEventId); socket.emit('selected-id', this.selectedEventId);
socket.emit('titles', this.titles); socket.emit('titles', this.titles);
@@ -203,7 +203,7 @@ class EventTimer extends Timer {
// general playback state // general playback state
socket.on('get-state', () => { socket.on('get-state', () => {
socket.emit('timer', this.getObject()); socket.emit('timer', this.getObject());
socket.emit('playstate', this.playState); socket.emit('playstate', this.state);
socket.emit('selected-id', this.selectedEventId); socket.emit('selected-id', this.selectedEventId);
socket.emit('titles', this.titles); socket.emit('titles', this.titles);
}); });
@@ -225,7 +225,7 @@ class EventTimer extends Timer {
}); });
socket.on('get-playstate', () => { socket.on('get-playstate', () => {
socket.emit('playstate', this.playState); socket.emit('playstate', this.state);
}); });
/*******************************************/ /*******************************************/
@@ -261,7 +261,6 @@ class EventTimer extends Timer {
/*******************************************/ /*******************************************/
// Presenter message // Presenter message
socket.on('set-presenter-text', (data) => { socket.on('set-presenter-text', (data) => {
console.log(data);
this._setterManager('set-presenter-text', data); this._setterManager('set-presenter-text', data);
}); });
@@ -326,10 +325,69 @@ class EventTimer extends Timer {
// set general // set general
this._eventList = events; this._eventList = events;
this.numEvents = numEvents; this.numEvents = numEvents;
// handle reload selected
if (this.selectedEventId != null) {
// Look for event (order might have changed)
const eventIndex = this._eventList.findIndex(
(e) => e.id === this.selectedEventId
);
// TODO: What to do about reloading // Maybe is missing
if (eventIndex === -1) {
this._resetTimers();
this._resetSelection();
return;
} }
// Reload data if running
if (this._startedAt != null) this.reloadRunning(eventIndex);
else if (this.selectedEventId != null) this.loadEvent(eventIndex);
}
}
// Reloads changed info in current event
reloadRunning(eventIndex) {
const e = this._eventList[eventIndex];
const start = e.timeStart == null || e.timeStart === '' ? 0 : e.timeStart;
let end = e.timeEnd == null || e.timeEnd === '' ? 0 : e.timeEnd;
// in case the end is earlier than start, we assume is the day after
if (end < start) end += 86400000;
// time stuff
const now = this._getCurrentTime();
const elapsed = this.getElapsed();
this.duration = end - start;
this.selectedEvent = eventIndex;
this._finishAt = now + (this.duration - elapsed);
// event titles
this.titles.titleNow = e.title;
this.titles.subtitleNow = e.subtitle;
this.titles.presenterNow = e.presenter;
// assume tere is no next event
this.titles.titleNext = null;
this.titles.subtitleNext = null;
this.titles.presenterNext = null;
// look for event after
if (eventIndex < this.numEvents - 1) {
for (let i = eventIndex + 1; i < this.numEvents; i++) {
// check that is the right type
if (this._eventList[i].type === 'event') {
this.titles.titleNext = this._eventList[i].title;
this.titles.subtitleNext = this._eventList[i].subtitle;
this.titles.presenterNext = this._eventList[i].presenter;
break;
}
}
}
this.broadcastState();
}
// Loads a given event
loadEvent(eventIndex) { loadEvent(eventIndex) {
// set event specific // set event specific
const e = this._eventList[eventIndex]; const e = this._eventList[eventIndex];
@@ -368,6 +426,7 @@ class EventTimer extends Timer {
} }
} }
} }
this.broadcastState();
} }
_resetSelection() { _resetSelection() {
+6 -2
View File
@@ -54,7 +54,7 @@ class Timer {
if (!this.showNegative) { if (!this.showNegative) {
this.current = Math.max(this._finishAt + this._pausedTotal - now, 0); this.current = Math.max(this._finishAt + this._pausedTotal - now, 0);
} else { } else {
(this.current = this._finishAt + this._pausedTotal - now), 0; this.current = this._finishAt + this._pausedTotal - now;
} }
break; break;
case 'pause': case 'pause':
@@ -99,6 +99,11 @@ class Timer {
this._pausedTotal = null; this._pausedTotal = null;
} }
// get elapsed time
getElapsed() {
return this.duration - this.current;
}
// getObject // getObject
getObject() { getObject() {
this.update(); this.update();
@@ -142,7 +147,6 @@ 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; else if (this.duration === 0) return;
// update pause time // update pause time
+6
View File
@@ -39,6 +39,7 @@ function getEventEvents() {
.value(); .value();
} }
// Updates timer object
function _updateTimers() { function _updateTimers() {
const results = getEventEvents(); const results = getEventEvents();
global.timer.updateEventList(results); global.timer.updateEventList(results);
@@ -181,6 +182,11 @@ exports.eventsDelete = async (req, res) => {
const c = _getEventsCount(); const c = _getEventsCount();
const e = db.get('events').find({ id: req.params.eventId }).value(); const e = db.get('events').find({ id: req.params.eventId }).value();
if (e == null) {
res.status(400).send(`No match`);
return;
}
if (c > 0 && e.order < c) incrementFrom(e.order, -1); if (c > 0 && e.order < c) incrementFrom(e.order, -1);
// add new event // add new event
-1
View File
@@ -5,7 +5,6 @@ const event = {
presenter: '', presenter: '',
timeStart: 0, timeStart: 0,
timeEnd: 0, timeEnd: 0,
clockStarted: null,
isPublic: false, isPublic: false,
type: 'event', type: 'event',
}; };