refactor: pass events instead of derived data to titles (#493)

* refactor: pass events instead of derived data to titles
This commit is contained in:
Carlos Valente
2023-09-14 22:30:23 +02:00
committed by GitHub
parent 13eca98133
commit 9708f0bfc6
17 changed files with 178 additions and 399 deletions
@@ -1,4 +1,4 @@
import { Loaded, OntimeEvent, SupportedEvent, TitleBlock } from 'ontime-types';
import { Loaded, OntimeEvent, SupportedEvent } from 'ontime-types';
import { DataProvider } from '../data-provider/DataProvider.js';
import { getRollTimers } from '../../services/rollUtils.js';
@@ -10,10 +10,11 @@ let instance;
* Manages business logic around loading events
*/
export class EventLoader {
loadedEvent: OntimeEvent | null;
loaded: Loaded;
titles: TitleBlock;
titlesPublic: TitleBlock;
eventNow: OntimeEvent | null;
publicEventNow: OntimeEvent | null;
eventNext: OntimeEvent | null;
publicEventNext: OntimeEvent | null;
constructor() {
if (instance) {
@@ -56,7 +57,7 @@ export class EventLoader {
}
/**
* returns an event given its index
* returns an event given its index after filtering for OntimeEvents
* @param {number} eventIndex
* @return {OntimeEvent | undefined}
*/
@@ -65,16 +66,6 @@ export class EventLoader {
return timedEvents?.[eventIndex];
}
/**
* returns an event given its index
* @param {number} eventIndex
* @return {object | undefined}
*/
static getPlayableAtIndex(eventIndex) {
const timedEvents = EventLoader.getPlayableEvents();
return timedEvents?.[eventIndex];
}
/**
* returns an event given its id
* @param {string} eventId
@@ -167,16 +158,18 @@ export class EventLoader {
timeNow,
);
this.loadedEvent = currentEvent;
// load events
this.eventNow = currentEvent;
this.publicEventNow = currentPublicEvent;
this.eventNext = nextEvent;
this.publicEventNext = nextPublicEvent;
// loaded data summary
this.loaded.selectedEventIndex = nowIndex;
this.loaded.selectedEventId = currentEvent?.id || null;
this.loaded.numEvents = timedEvents.length;
// titles
this._loadThisTitles(currentEvent, 'now-private');
this._loadThisTitles(currentPublicEvent, 'now-public');
this._loadThisTitles(nextEvent, 'next-private');
this._loadThisTitles(nextPublicEvent, 'next-public');
this.loaded.nextEventId = nextEvent.id;
this.loaded.nextPublicEventId = nextPublicEvent.id;
return { currentEvent, nextEvent, timeToNext };
}
@@ -187,10 +180,11 @@ export class EventLoader {
*/
getLoaded() {
return {
loadedEvent: this.loadedEvent,
loaded: this.loaded,
titles: this.titles,
titlesPublic: this.titlesPublic,
eventNow: this.eventNow,
publicEventNow: this.publicEventNow,
eventNext: this.eventNext,
publicEventNext: this.publicEventNext,
};
}
@@ -206,7 +200,10 @@ export class EventLoader {
* Resets instance state
*/
reset(emit = true) {
this.loadedEvent = null;
this.eventNow = null;
this.publicEventNow = null;
this.eventNext = null;
this.publicEventNext = null;
this.loaded = {
selectedEventIndex: null,
selectedEventId: null,
@@ -215,26 +212,6 @@ export class EventLoader {
nextPublicEventId: null,
numEvents: EventLoader.getPlayableEvents().length,
};
this.titles = {
titleNow: null,
subtitleNow: null,
presenterNow: null,
noteNow: null,
titleNext: null,
subtitleNext: null,
presenterNext: null,
noteNext: null,
};
this.titlesPublic = {
titleNow: null,
subtitleNow: null,
presenterNow: null,
noteNow: null,
titleNext: null,
subtitleNext: null,
presenterNext: null,
noteNext: null,
};
// workaround for socket not being ready in constructor
if (emit) {
@@ -255,13 +232,12 @@ export class EventLoader {
const playableEvents = EventLoader.getPlayableEvents();
// we know some stuff now
this.loadedEvent = event;
this.loaded.selectedEventIndex = eventIndex;
this.loaded.selectedEventId = event.id;
this.loaded.numEvents = timedEvents.length;
// this.nextEventId = playableEvents[eventIndex + 1].id;
this._loadTitlesNow(event, playableEvents);
this._loadTitlesNext(playableEvents);
this.eventNow = event;
this._loadEventNow(event, playableEvents);
this._loadEventNext(playableEvents);
this._loadEvent();
@@ -274,29 +250,28 @@ export class EventLoader {
private _loadEvent() {
eventStore.batchSet({
loaded: this.loaded,
titles: this.titles,
titlesPublic: this.titlesPublic,
eventNow: this.eventNow,
publicEventNow: this.publicEventNow,
eventNext: this.eventNext,
publicEventNext: this.publicEventNext,
});
}
/**
* @description loads given title (now)
* @description loads currently running events
* @private
* @param {object} event
* @param {array} rundown
*/
private _loadTitlesNow(event, rundown) {
// private title is always current
private _loadEventNow(event, rundown) {
this.eventNow = event;
// check if current is also public
if (event.isPublic) {
this._loadThisTitles(event, 'now');
this.publicEventNow = event;
} else {
this._loadThisTitles(event, 'now-private');
// assume there is no public event
this.titlesPublic.titleNow = null;
this.titlesPublic.subtitleNow = null;
this.titlesPublic.presenterNow = null;
this.publicEventNow = null;
this.loaded.selectedPublicEventId = null;
// if there is nothing before, return
@@ -305,7 +280,8 @@ export class EventLoader {
// iterate backwards to find it
for (let i = this.loaded.selectedEventIndex; i >= 0; i--) {
if (rundown[i].isPublic) {
this._loadThisTitles(rundown[i], 'now-public');
this.publicEventNow = rundown[i];
this.loaded.selectedPublicEventId = rundown[i].id;
break;
}
}
@@ -313,180 +289,44 @@ export class EventLoader {
}
/**
* @description look for next titles to load
* @description look for next events
* @private
*/
private _loadTitlesNext(rundown) {
// maybe there is nothing to load
if (this.loaded.selectedEventIndex === null) return;
// assume there is no next event
this.titles.titleNext = null;
this.titles.subtitleNext = null;
this.titles.presenterNext = null;
this.titles.noteNext = null;
private _loadEventNext(rundown) {
// assume there are no next events
this.eventNext = null;
this.publicEventNext = null;
this.loaded.nextEventId = null;
this.titlesPublic.titleNext = null;
this.titlesPublic.subtitleNext = null;
this.titlesPublic.presenterNext = null;
this.loaded.nextPublicEventId = null;
if (this.loaded.selectedEventIndex === null) return;
const numEvents = rundown.length;
if (this.loaded.selectedEventIndex < numEvents - 1) {
let nextPublic = false;
let nextPrivate = false;
let nextProduction = false;
for (let i = this.loaded.selectedEventIndex + 1; i < numEvents; i++) {
// if we have not set private
if (!nextPrivate) {
this._loadThisTitles(rundown[i], 'next-private');
nextPrivate = true;
if (!nextProduction) {
this.eventNext = rundown[i];
this.loaded.nextEventId = rundown[i].id;
nextProduction = true;
}
// if event is public
if (rundown[i].isPublic) {
this._loadThisTitles(rundown[i], 'next-public');
this.publicEventNext = rundown[i];
this.loaded.nextPublicEventId = rundown[i].id;
nextPublic = true;
}
// Stop if both are set
if (nextPublic && nextPrivate) break;
if (nextPublic && nextProduction) break;
}
}
}
/**
* @description loads given title
* @param event
* @param type
* @private
*/
private _loadThisTitles(event, type) {
if (type === 'now') {
if (event === null) {
// public
this.titlesPublic.titleNow = null;
this.titlesPublic.subtitleNow = null;
this.titlesPublic.presenterNow = null;
this.titlesPublic.noteNow = null;
this.loaded.selectedPublicEventId = null;
// private
this.titles.titleNow = null;
this.titles.subtitleNow = null;
this.titles.presenterNow = null;
this.titles.noteNow = null;
this.loaded.selectedEventId = null;
} else {
// public
this.titlesPublic.titleNow = event.title;
this.titlesPublic.subtitleNow = event.subtitle;
this.titlesPublic.presenterNow = event.presenter;
this.titlesPublic.noteNow = event.note;
this.loaded.selectedPublicEventId = event.id;
// private
this.titles.titleNow = event.title;
this.titles.subtitleNow = event.subtitle;
this.titles.presenterNow = event.presenter;
this.titles.noteNow = event.note;
this.loaded.selectedEventId = event.id;
}
} else if (type === 'now-public') {
if (event === null) {
this.titlesPublic.titleNow = null;
this.titlesPublic.subtitleNow = null;
this.titlesPublic.presenterNow = null;
this.titlesPublic.noteNow = null;
this.loaded.selectedPublicEventId = null;
} else {
this.titlesPublic.titleNow = event.title;
this.titlesPublic.subtitleNow = event.subtitle;
this.titlesPublic.presenterNow = event.presenter;
this.titlesPublic.noteNow = event.note;
this.loaded.selectedPublicEventId = event.id;
}
} else if (type === 'now-private') {
if (event === null) {
this.titles.titleNow = null;
this.titles.subtitleNow = null;
this.titles.presenterNow = null;
this.titles.noteNow = null;
this.loaded.selectedEventId = null;
} else {
this.titles.titleNow = event.title;
this.titles.subtitleNow = event.subtitle;
this.titles.presenterNow = event.presenter;
this.titles.noteNow = event.note;
this.loaded.selectedEventId = event.id;
}
}
// next, load to both public and private
else if (type === 'next') {
if (event === null) {
// public
this.titlesPublic.titleNext = null;
this.titlesPublic.subtitleNext = null;
this.titlesPublic.presenterNext = null;
this.titlesPublic.noteNext = null;
this.loaded.nextPublicEventId = null;
// private
this.titles.titleNext = null;
this.titles.subtitleNext = null;
this.titles.presenterNext = null;
this.titles.noteNext = null;
this.loaded.nextEventId = null;
} else {
// public
this.titlesPublic.titleNext = event.title;
this.titlesPublic.subtitleNext = event.subtitle;
this.titlesPublic.presenterNext = event.presenter;
this.titlesPublic.noteNext = event.note;
this.loaded.nextPublicEventId = event.id;
// private
this.titles.titleNext = event.title;
this.titles.subtitleNext = event.subtitle;
this.titles.presenterNext = event.presenter;
this.titles.noteNext = event.note;
this.loaded.nextEventId = event.id;
}
} else if (type === 'next-public') {
if (event === null) {
this.titlesPublic.titleNext = null;
this.titlesPublic.subtitleNext = null;
this.titlesPublic.presenterNext = null;
this.titlesPublic.noteNext = null;
this.loaded.nextPublicEventId = null;
} else {
this.titlesPublic.titleNext = event.title;
this.titlesPublic.subtitleNext = event.subtitle;
this.titlesPublic.presenterNext = event.presenter;
this.titlesPublic.noteNext = event.note;
this.loaded.nextPublicEventId = event.id;
}
} else if (type === 'next-private') {
if (event === null) {
this.titles.titleNext = null;
this.titles.subtitleNext = null;
this.titles.presenterNext = null;
this.titles.noteNext = null;
this.loaded.nextEventId = null;
} else {
this.titles.titleNext = event.title;
this.titles.subtitleNext = event.subtitle;
this.titles.presenterNext = event.presenter;
this.titles.noteNext = event.note;
this.loaded.nextEventId = event.id;
}
} else {
throw new Error(`Unhandled title type: ${type}`);
}
}
}
export const eventLoader = new EventLoader();