Skip to content

Commit

Permalink
feat: count to end (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente authored Aug 27, 2023
1 parent cd9dbcd commit 5323e62
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const EventEditorTimes = (props: EventEditorTimesProps) => {
>
<option value={TimerType.CountDown}>Count down</option>
<option value={TimerType.CountUp}>Count up</option>
<option value={TimerType.TimeToEnd}>Time to end</option>
<option value={TimerType.Clock}>Clock</option>
</Select>
<label className={style.inputLabel}>End Action</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { memo, useCallback, useEffect, useState } from 'react';
import { Tooltip } from '@chakra-ui/react';
import { BiArrowToBottom } from '@react-icons/all-files/bi/BiArrowToBottom';
import { IoArrowDown } from '@react-icons/all-files/io5/IoArrowDown';
import { IoArrowUp } from '@react-icons/all-files/io5/IoArrowUp';
import { IoOptions } from '@react-icons/all-files/io5/IoOptions';
Expand Down Expand Up @@ -196,5 +197,8 @@ function TimerIcon(props: { type: TimerType; className: string }) {
if (type === TimerType.Clock) {
return <IoTime className={className} />;
}
if (type === TimerType.TimeToEnd) {
return <BiArrowToBottom className={className} />;
}
return <IoArrowDown className={className} />;
}
2 changes: 1 addition & 1 deletion apps/client/src/features/viewers/common/viewerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getTimerByType(timerObject?: TimerTypeParams): string | number |
return timer;
}

if (timerObject.timerType === TimerType.CountDown) {
if (timerObject.timerType === TimerType.CountDown || timerObject.timerType === TimerType.TimeToEnd) {
timer = timerObject.current;
} else if (timerObject.timerType === TimerType.CountUp) {
timer = timerObject.elapsed;
Expand Down
28 changes: 26 additions & 2 deletions apps/server/src/services/TimerService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EndAction, OntimeEvent, Playback, TimerLifeCycle, TimerState } from 'ontime-types';
import { EndAction, OntimeEvent, Playback, TimerLifeCycle, TimerState, TimerType } from 'ontime-types';
import { calculateDuration, dayInMs } from 'ontime-utils';

import { eventStore } from '../stores/EventStore.js';
Expand All @@ -24,6 +24,9 @@ export class TimerService {
timer: TimerState;

loadedTimerId: string | null;
private loadedTimerStart: number | null;
private loadedTimerEnd: number | null;

private pausedTime: number;
private pausedAt: number | null;
private secondaryTarget: number | null;
Expand Down Expand Up @@ -61,6 +64,9 @@ export class TimerService {
endAction: null,
};
this.loadedTimerId = null;
this.loadedTimerStart = null;
this.loadedTimerEnd = null;

this.pausedTime = 0;
this.pausedAt = null;
this.secondaryTarget = null;
Expand Down Expand Up @@ -93,6 +99,8 @@ export class TimerService {
this.timer.duration = calculateDuration(timer.timeStart, timer.timeEnd);
this.timer.timerType = timer.timerType;
this.timer.endAction = timer.endAction;
this.loadedTimerStart = timer.timeStart;
this.loadedTimerEnd = timer.timeEnd;

// this might not be ideal
this.timer.finishedAt = null;
Expand All @@ -102,6 +110,8 @@ export class TimerService {
this.timer.duration,
this.pausedTime,
this.timer.addedTime,
this.loadedTimerEnd,
this.timer.timerType,
);
if (this.timer.startedAt === null) {
this.timer.current = this.timer.duration;
Expand Down Expand Up @@ -129,14 +139,22 @@ export class TimerService {
this._clear();

this.loadedTimerId = timer.id;
this.loadedTimerStart = timer.timeStart;
this.loadedTimerEnd = timer.timeEnd;

this.timer.duration = calculateDuration(timer.timeStart, timer.timeEnd);
this.timer.current = this.timer.duration;
this.playback = Playback.Armed;
this.timer.timerType = timer.timerType;
this.timer.endAction = timer.endAction;
this.pausedTime = 0;
this.pausedAt = 0;

this.timer.current = this.timer.duration;
if (this.timer.timerType === TimerType.TimeToEnd) {
const now = clock.timeNow();
this.timer.current = getCurrent(now, this.timer.duration, 0, 0, now, timer.timeEnd, this.timer.timerType);
}

if (typeof initialData !== 'undefined') {
this.timer = { ...this.timer, ...initialData };
}
Expand Down Expand Up @@ -188,6 +206,8 @@ export class TimerService {
this.timer.duration,
this.pausedTime,
this.timer.addedTime,
this.loadedTimerEnd,
this.timer.timerType,
);
this._onStart();
}
Expand Down Expand Up @@ -310,6 +330,8 @@ export class TimerService {
this.timer.duration,
this.pausedTime,
this.timer.addedTime,
this.loadedTimerEnd,
this.timer.timerType,
);
}
this.timer.current = getCurrent(
Expand All @@ -318,6 +340,8 @@ export class TimerService {
this.timer.addedTime,
this.pausedTime,
this.timer.clock,
this.loadedTimerEnd,
this.timer.timerType,
);
this.timer.elapsed = this.timer.duration - this.timer.current;
}
Expand Down
Loading

0 comments on commit 5323e62

Please sign in to comment.