refactor: cleanup store (#727)

* refactor: isolate clock

* refactor: isolate private

* refactor: remove duplicate

* chore: rename loaded > runtime

* fix: handle no events loaded
This commit is contained in:
Carlos Valente
2024-01-24 00:20:10 +01:00
committed by GitHub
parent 0cd2ca4191
commit b4ad533e6a
31 changed files with 411 additions and 384 deletions
@@ -36,10 +36,10 @@ class RuntimeService {
* Checks if a list of IDs is in the current selection
*/
private affectsLoaded(affectedIds: string[]): boolean {
const now = state.runtime.selectedEventId;
const nowPublic = state.runtime.selectedPublicEventId;
const next = state.runtime.nextEventId;
const nextPublic = state.runtime.nextPublicEventId;
const now = state.eventNow?.id;
const nowPublic = state.publicEventNow?.id;
const next = state.eventNext?.id;
const nextPublic = state.publicEventNext?.id;
return (
affectedIds.includes(now) ||
affectedIds.includes(nowPublic) ||
@@ -50,8 +50,8 @@ class RuntimeService {
private isNewNext() {
const timedEvents = EventLoader.getPlayableEvents();
const now = state.runtime.selectedEventId;
const next = state.runtime.nextEventId;
const now = state.eventNow?.id;
const next = state.eventNext?.id;
// check whether the index of now and next are consecutive
const indexNow = timedEvents.findIndex((event) => event.id === now);
@@ -61,8 +61,8 @@ class RuntimeService {
return true;
}
// iterate through timed events and see if there are public events between nowPublic and nextPublic
const nowPublic = state.runtime.selectedPublicEventId;
const nextPublic = state.runtime.nextPublicEventId;
const nowPublic = state.publicEventNow?.id;
const nextPublic = state.publicEventNext?.id;
let foundNew = false;
let isAfter = false;
@@ -93,7 +93,7 @@ class RuntimeService {
* check whether underlying data of runtime has changed
*/
update(affectedIds?: string[]) {
const hasLoadedElements = state.runtime.selectedEventId && state.runtime.nextEventId;
const hasLoadedElements = state.eventNow && state.eventNext;
if (!hasLoadedElements) {
return;
}
@@ -107,11 +107,11 @@ class RuntimeService {
let isNext = false;
if (safeOption || eventInMemory) {
if (state.playback === Playback.Roll) {
if (state.timer.playback === Playback.Roll) {
this.roll();
}
// load stuff again, but keep running if our events still exist
const eventNow = EventLoader.getEventWithId(state.runtime.selectedEventId);
const eventNow = EventLoader.getEventWithId(state.eventNow.id);
if (eventNow) {
stateMutations.reload(eventNow);
}
@@ -139,7 +139,7 @@ class RuntimeService {
const timedEvents = EventLoader.getPlayableEvents();
stateMutations.load(event, timedEvents);
const success = event.id === state.runtime.selectedEventId;
const success = event.id === state.eventNow?.id;
if (success) {
logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`);
@@ -227,7 +227,7 @@ class RuntimeService {
* @return {boolean} success - whether an event was loaded
*/
loadPrevious(): boolean {
const previousEvent = EventLoader.findPrevious(state.runtime.selectedEventId);
const previousEvent = EventLoader.findPrevious(state.eventNow?.id);
if (previousEvent) {
const success = this.loadEvent(previousEvent);
return success;
@@ -240,7 +240,7 @@ class RuntimeService {
* @return {boolean} success
*/
loadNext(): boolean {
const nextEvent = EventLoader.findNext(state.runtime.selectedEventId);
const nextEvent = EventLoader.findNext(state.eventNow?.id);
if (nextEvent) {
const success = this.loadEvent(nextEvent);
return success;
@@ -254,10 +254,10 @@ class RuntimeService {
* Starts playback on selected event
*/
start() {
const canStart = validatePlayback(state.playback).start;
const canStart = validatePlayback(state.timer.playback).start;
if (canStart) {
this.eventTimer.start();
logger.info(LogOrigin.Playback, `Play Mode ${state.playback.toUpperCase()}`);
logger.info(LogOrigin.Playback, `Play Mode ${state.timer.playback.toUpperCase()}`);
}
}
@@ -275,9 +275,9 @@ class RuntimeService {
* Pauses playback on selected event
*/
pause() {
if (validatePlayback(state.playback).pause) {
if (validatePlayback(state.timer.playback).pause) {
this.eventTimer.pause();
const newState = state.playback;
const newState = state.timer.playback;
logger.info(LogOrigin.Playback, `Play Mode ${newState.toUpperCase()}`);
}
}
@@ -286,9 +286,9 @@ class RuntimeService {
* Stops timer and unloads any events
*/
stop() {
if (validatePlayback(state.playback).stop) {
if (validatePlayback(state.timer.playback).stop) {
this.eventTimer.stop();
const newState = state.playback;
const newState = state.timer.playback;
logger.info(LogOrigin.Playback, `Play Mode ${newState.toUpperCase()}`);
}
}
@@ -297,7 +297,7 @@ class RuntimeService {
* Reloads current event
*/
reload() {
if (state.runtime.selectedEventId) {
if (state.eventNow) {
stateMutations.reload();
}
}
@@ -313,7 +313,7 @@ class RuntimeService {
logger.warning(LogOrigin.Server, `Roll: ${error}`);
}
const newState = state.playback;
const newState = state.timer.playback;
logger.info(LogOrigin.Playback, `Play Mode ${newState.toUpperCase()}`);
}