Fix/58 roll issues (#67)

* fix/58-roll-issues: handle edge cases
This commit is contained in:
Carlos Valente
2021-12-15 22:31:34 +01:00
committed by GitHub
parent 1321aa555e
commit cd911aaaa2
6 changed files with 129 additions and 35 deletions
+17 -5
View File
@@ -1,3 +1,14 @@
export const DAYMS = 86400000;
/**
* @description handle events that span over midnight
* @param {num} start - When does the event start
* @param {num} end - When does the event end
* @returns {num} normalised time
*/
export const normaliseEndTime = (start, end) => (end < start ? end + DAYMS : end);
/**
* @description Sorts an array of objects by given property
* @param {array} arr - array to be sorted
@@ -41,8 +52,9 @@ export const getSelectionByRoll = (arr, now) => {
let nowFound = false;
// exit early if we are past the events
const lastEventEnd = orderedEvents[orderedEvents.length - 1].timeEnd;
if (now > lastEventEnd) {
const lastEvent = orderedEvents[orderedEvents.length - 1];
const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
if (now > lastNormalEnd) {
return {
nowIndex,
nowId,
@@ -57,8 +69,7 @@ export const getSelectionByRoll = (arr, now) => {
// loop through events, look for where we should be
for (const e of orderedEvents) {
// When does the event end (handle midnight)
const normalEnd =
e.timeEnd < e.timeStart ? (e.timeEnd += this.DAYMS) : e.timeEnd;
const normalEnd = normaliseEndTime(e.timeStart, e.timeEnd);
if (normalEnd <= now) {
// event ran already
@@ -83,7 +94,7 @@ export const getSelectionByRoll = (arr, now) => {
// set timers
timers = {
_startedAt: e.timeStart,
_finishAt: normalEnd,
_finishAt: e.timeEnd,
duration: normalEnd - e.timeStart,
current: normalEnd - now,
};
@@ -118,3 +129,4 @@ export const getSelectionByRoll = (arr, now) => {
timeToNext,
};
};