mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-16 04:43:35 +00:00
V3 (#657)
* refactor: cleanup routes * style: smaller base font * chore: upgrade dependencies * chore: lock node version to electron * refactor: pass HTTP to integration controller (#652) * refactor: deprecate onair control * refactor: remove playback router * Several project files user folder (#617) * chore: automated screenshots (#667) * feat: app settings (#658) * refactor: remove deprecated event data (#674) * Studio clock (#663) --------- Co-authored-by: Carlos Valente <carlosvalente@pm.me> * Feat: reorder events with alt+ctrl + arrow up/down (#645) * Warning and danger per event (#677) --------- Co-authored-by: Fabian Posenau <fabian@fphome.de> * refactor: stabilise actionHandler (#683) Co-authored-by: Fabian Posenau <fabian@fphome.de> * improvement: hide seconds (#675) * wip: overview (#688) * fix: focus cursor (#695) * refactor: update lower third (#665) * Refactor/time formatting (#696) --------- Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me> * feat: multiple selection (#703) --------- Co-authored-by: asharonbaltazar <asharonbaltazar@outlook.com> Co-authored-by: Alex <ac@omnivox.dk> * fix: test - go to `Edit mode` befor tying to click `Event options` button (#708) * refactor: runtime service (#715) * fix: issue with loosing cursor position on message (#719) * remove info panel (#721) * Event editor continue (#722) * update API - part (#709) --------- Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me> * refactor: update timers (#729) * feat: many timers (#706) --------- Co-authored-by: arc-alex <ac@omnivox.dk> * refactor: excel cleanup (#734) * refactor: allow import of blocks and skip import (#735) * Project manager (#697) * refactor: UI for linking events (#763) * upgraded pipeline actions (#777) * Over under (#771) * custom fields (#744) --------- Co-authored-by: Carlos Valente <carlosvalente@pm.me> * Sheets settings (#774) --------- Co-authored-by: arc-alex <ac@omnivox.dk> * style: tweaks to lower thirds (#785) * refactor: delays account for gaps (#784) * refactor: partial state updates (#780) * feat: generate crash report (#787) * Sheet use limited input device auth flow (#782) --------- Co-authored-by: cv <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me> * Custom fields views (#789) * refactor: deprecate presenter and subtitle (#795) * refactor: organise API around resources (#798) --------- Co-authored-by: Bianca Procopio <biancahprocopio@gmail.com> * Time to end (#804) * Skip fixes (#805) * fix: onair derives from playback * Param nav (#822) --------- Co-authored-by: Alex Christoffer Rasmussen <ac@omnivox.dk> * refactor: download files from interface (#831) * Quick options (#814) * End pause (#832) * chore: bump node version in docker (#834) * refactor: follow in run mode (#840) * fix: uncaught error in http integration (#837) * Apply project (#843) Co-authored-by: Matteo Gheza <matteo.gheza07@gmail.com> Co-authored-by: Ary <arylmoraesn@gmail.com> Co-authored-by: Alex Christoffer Rasmussen <ac@omnivox.dk> Co-authored-by: Fabian Posenau <19673098+kellhogs@users.noreply.github.com> Co-authored-by: Fabian Posenau <fabian@fphome.de> Co-authored-by: Alex Rohleder <alexrohleder96@gmail.com> Co-authored-by: asharonbaltazar <asharonbaltazar@outlook.com> Co-authored-by: Bianca Procopio <biancahprocopio@gmail.com> Co-authored-by: Fabian Posenau <fabianpos99+github@gmail.com>
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import { RuntimeStore } from 'ontime-types';
|
||||
|
||||
import { socket } from '../adapters/WebsocketAdapter.js';
|
||||
import { eventTimer } from '../services/TimerService.js';
|
||||
import { messageService } from '../services/message-service/MessageService.js';
|
||||
import { eventLoader } from '../classes/event-loader/EventLoader.js';
|
||||
|
||||
export type PublishFn = <T extends keyof RuntimeStore>(key: T, value: RuntimeStore[T]) => void;
|
||||
|
||||
@@ -24,14 +22,12 @@ export const eventStore = {
|
||||
},
|
||||
set<T extends keyof RuntimeStore>(key: T, value: RuntimeStore[T]) {
|
||||
store[key] = value;
|
||||
// TODO: Partial updates seems to cause issues on the client
|
||||
// socket.send({
|
||||
// type: `ontime-${key}`,
|
||||
// payload: value,
|
||||
// });
|
||||
this.broadcast();
|
||||
socket.sendAsJson({
|
||||
type: `ontime-${key}`,
|
||||
payload: value,
|
||||
});
|
||||
},
|
||||
batchSet<K extends keyof RuntimeStore>(values: Record<K, RuntimeStore[K]>) {
|
||||
batchSet(values: Partial<RuntimeStore>) {
|
||||
Object.entries(values).forEach(([key, value]) => {
|
||||
store[key] = value;
|
||||
});
|
||||
@@ -47,34 +43,3 @@ export const eventStore = {
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Module initialises the services and provides initial payload for the store
|
||||
* Currently registered objects in store
|
||||
* - Timer Service timer
|
||||
* - Timer Service playback
|
||||
* - Message Service timerMessage
|
||||
* - Message Service publicMessage
|
||||
* - Message Service lowerMessage
|
||||
* - Message Service onAir
|
||||
* - Event Loader loaded
|
||||
* - Event Loader eventNow
|
||||
* - Event Loader publicEventNow
|
||||
* - Event Loader eventNext
|
||||
* - Event Loader publicEventNext
|
||||
*/
|
||||
|
||||
export const getInitialPayload = () => ({
|
||||
timer: eventTimer.timer,
|
||||
playback: eventTimer.playback,
|
||||
timerMessage: messageService.timerMessage,
|
||||
publicMessage: messageService.publicMessage,
|
||||
lowerMessage: messageService.lowerMessage,
|
||||
externalMessage: messageService.externalMessage,
|
||||
onAir: messageService.onAir,
|
||||
loaded: eventLoader.loaded,
|
||||
eventNow: eventLoader.eventNow,
|
||||
publicEventNow: eventLoader.publicEventNow,
|
||||
eventNext: eventLoader.eventNext,
|
||||
publicEventNext: eventLoader.publicEventNext,
|
||||
});
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
import { runtimeCacheStore } from '../cachingStore.js';
|
||||
|
||||
describe('cachingStore()', () => {
|
||||
beforeEach(() => {
|
||||
runtimeCacheStore.clear(); // Clear the cache before each test
|
||||
});
|
||||
|
||||
it('should check if an item is cached', () => {
|
||||
// Add an item to the cache
|
||||
runtimeCacheStore.setCached('key', 'value');
|
||||
|
||||
// Check if the item is cached
|
||||
expect(runtimeCacheStore.checkCached('key')).toBe(true);
|
||||
expect(runtimeCacheStore.checkCached('non-existent-key')).toBe(false);
|
||||
});
|
||||
|
||||
it('should get an item from the cache', () => {
|
||||
// Add an item to the cache
|
||||
runtimeCacheStore.setCached('key', 'value');
|
||||
|
||||
// Get the item from the cache
|
||||
const result = runtimeCacheStore.getCached('key', () => 'default-value');
|
||||
|
||||
// Check the returned value
|
||||
expect(result).toBe('value');
|
||||
});
|
||||
|
||||
it('should retrieve default value when item is not cached', () => {
|
||||
// Get an item that is not in the cache
|
||||
const result = runtimeCacheStore.getCached('non-existent-key', () => 'default-value');
|
||||
|
||||
// Check the returned value
|
||||
expect(result).toBe('default-value');
|
||||
});
|
||||
|
||||
it('should set an item in the cache', () => {
|
||||
// Set an item in the cache
|
||||
runtimeCacheStore.setCached('key', 'value');
|
||||
|
||||
// Check if the item is cached
|
||||
expect(runtimeCacheStore.checkCached('key')).toBe(true);
|
||||
});
|
||||
|
||||
it('should invalidate an item in the cache', () => {
|
||||
// Add an item to the cache
|
||||
runtimeCacheStore.setCached('key', 'value');
|
||||
|
||||
// Invalidate the item
|
||||
runtimeCacheStore.invalidate('key');
|
||||
|
||||
// Check if the item is no longer cached
|
||||
expect(runtimeCacheStore.checkCached('key')).toBe(false);
|
||||
});
|
||||
|
||||
it('should clear the cache', () => {
|
||||
// Add items to the cache
|
||||
runtimeCacheStore.setCached('key1', 'value1');
|
||||
runtimeCacheStore.setCached('key2', 'value2');
|
||||
|
||||
// Clear the cache
|
||||
runtimeCacheStore.clear();
|
||||
|
||||
// Check if the cache is empty
|
||||
expect(runtimeCacheStore.checkCached('key1')).toBe(false);
|
||||
expect(runtimeCacheStore.checkCached('key2')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,196 @@
|
||||
import { OntimeEvent, Playback } from 'ontime-types';
|
||||
import { deepmerge } from 'ontime-utils';
|
||||
|
||||
import { RuntimeState, addTime, clear, getState, load, pause, start, stop } from '../runtimeState.js';
|
||||
import { initRundown } from '../../services/rundown-service/RundownService.js';
|
||||
|
||||
const mockEvent = {
|
||||
type: 'event',
|
||||
id: 'mock',
|
||||
cue: 'mock',
|
||||
timeStart: 0,
|
||||
timeEnd: 1000,
|
||||
duration: 1000,
|
||||
} as OntimeEvent;
|
||||
|
||||
const mockState = {
|
||||
clock: 666,
|
||||
eventNow: null,
|
||||
publicEventNow: null,
|
||||
eventNext: null,
|
||||
publicEventNext: null,
|
||||
runtime: {
|
||||
selectedEventIndex: null,
|
||||
numEvents: 0,
|
||||
},
|
||||
timer: {
|
||||
addedTime: 0,
|
||||
current: null,
|
||||
duration: null,
|
||||
elapsed: null,
|
||||
expectedFinish: null,
|
||||
finishedAt: null,
|
||||
playback: Playback.Stop,
|
||||
secondaryTimer: null,
|
||||
startedAt: null,
|
||||
},
|
||||
_timer: {
|
||||
pausedAt: null,
|
||||
secondaryTarget: null,
|
||||
},
|
||||
} as RuntimeState;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const makeMockState = (patch: RuntimeState): RuntimeState => {
|
||||
return deepmerge(mockState, patch);
|
||||
};
|
||||
|
||||
describe('mutation on runtimeState', () => {
|
||||
beforeEach(() => {
|
||||
clear();
|
||||
|
||||
vi.mock('../../services/rundown-service/RundownService.js', async (importOriginal) => {
|
||||
const actual = (await importOriginal()) as object;
|
||||
|
||||
return {
|
||||
...actual,
|
||||
getPlayableEvents: vi.fn().mockReturnValue([
|
||||
{
|
||||
id: 'mock',
|
||||
cue: 'mock',
|
||||
timeStart: 0,
|
||||
timeEnd: 1000,
|
||||
duration: 1000,
|
||||
},
|
||||
]),
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('playback operations', () => {
|
||||
it('refuses if nothing is loaded', () => {
|
||||
let success = start(mockState);
|
||||
expect(success).toBe(false);
|
||||
|
||||
success = pause();
|
||||
expect(success).toBe(false);
|
||||
});
|
||||
test('normal playback cycle', () => {
|
||||
// 1. Load event
|
||||
load(mockEvent, [mockEvent]);
|
||||
let newState = getState();
|
||||
expect(newState.eventNow?.id).toBe(mockEvent.id);
|
||||
expect(newState.timer.playback).toBe(Playback.Armed);
|
||||
expect(newState.clock).not.toBe(666);
|
||||
|
||||
// 2. Start event
|
||||
let success = start();
|
||||
newState = getState();
|
||||
expect(success).toBe(true);
|
||||
expect(newState.timer).toMatchObject({
|
||||
playback: Playback.Play,
|
||||
});
|
||||
expect(newState.runtime.actualStart).toBe(newState.clock);
|
||||
|
||||
// 3. Pause event
|
||||
success = pause();
|
||||
newState = getState();
|
||||
expect(success).toBe(true);
|
||||
expect(newState.clock).not.toBe(666);
|
||||
expect(newState.timer).toMatchObject({
|
||||
playback: Playback.Pause,
|
||||
addedTime: 0,
|
||||
});
|
||||
expect(newState._timer.pausedAt).toEqual(newState.clock);
|
||||
|
||||
success = pause();
|
||||
expect(success).toBe(false);
|
||||
|
||||
// 4. Restart event
|
||||
success = start();
|
||||
newState = getState();
|
||||
expect(success).toBe(true);
|
||||
expect(newState.timer).toMatchObject({
|
||||
playback: Playback.Play,
|
||||
secondaryTimer: null,
|
||||
});
|
||||
expect(newState.timer).toEqual(
|
||||
expect.objectContaining({
|
||||
current: expect.any(Number),
|
||||
duration: expect.any(Number),
|
||||
elapsed: expect.any(Number),
|
||||
expectedFinish: expect.any(Number),
|
||||
startedAt: expect.any(Number),
|
||||
}),
|
||||
);
|
||||
expect(newState._timer.pausedAt).toBeNull();
|
||||
|
||||
// 5. Stop event
|
||||
success = stop();
|
||||
expect(success).toBe(true);
|
||||
expect(newState.eventNow).toBe(null);
|
||||
expect(newState.timer).toMatchObject({
|
||||
playback: Playback.Stop,
|
||||
duration: null,
|
||||
elapsed: null,
|
||||
expectedFinish: null,
|
||||
startedAt: null,
|
||||
});
|
||||
expect(newState.runtime.actualStart).toBeNull();
|
||||
});
|
||||
|
||||
// do this before the test so that it is applied
|
||||
const event1 = { ...mockEvent, id: 'event1', timeStart: 0, timeEnd: 1000, duration: 1000 };
|
||||
const event2 = { ...mockEvent, id: 'event2', timeStart: 1000, timeEnd: 1500, duration: 500 };
|
||||
// force update
|
||||
initRundown([event1, event2], {});
|
||||
test('runtime offset', () => {
|
||||
// 1. Load event
|
||||
load(event1, [event1, event2]);
|
||||
let newState = getState();
|
||||
expect(newState.runtime.actualStart).toBeNull();
|
||||
expect(newState.runtime.plannedStart).toBe(0);
|
||||
expect(newState.runtime.plannedEnd).toBe(1500);
|
||||
|
||||
// 2. Start event
|
||||
start();
|
||||
newState = getState();
|
||||
const firstStart = newState.clock;
|
||||
expect(newState.runtime.actualStart).toBe(newState.clock);
|
||||
expect(newState.runtime.offset).toBe(newState.clock - event1.timeStart);
|
||||
expect(newState.runtime.expectedEnd).toBe(newState.runtime.offset + event2.timeEnd);
|
||||
|
||||
// 3. Next event
|
||||
load(event2, [event1, event2]);
|
||||
start();
|
||||
newState = getState();
|
||||
expect(newState.runtime.actualStart).toBe(firstStart);
|
||||
// we are over-under, the difference between the schedule and the actual start
|
||||
const delayBefore = newState.clock - event2.timeStart;
|
||||
expect(newState.runtime.offset).toBe(delayBefore);
|
||||
// finish is the difference between the runtime and the schedule
|
||||
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd + newState.runtime.offset);
|
||||
|
||||
// 4. Add time
|
||||
addTime(10);
|
||||
newState = getState();
|
||||
expect(newState.runtime.offset).toBe(delayBefore + 10);
|
||||
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd + newState.runtime.offset);
|
||||
|
||||
// 5. Stop event
|
||||
stop();
|
||||
newState = getState();
|
||||
expect(newState.runtime.actualStart).toBeNull();
|
||||
expect(newState.runtime.offset).toBeNull();
|
||||
expect(newState.runtime.expectedEnd).toBeNull();
|
||||
});
|
||||
|
||||
test.todo('runtime offset on timers in overtime', () => {});
|
||||
|
||||
test.todo('roll mode', () => {});
|
||||
});
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
interface CacheData {
|
||||
data: unknown;
|
||||
}
|
||||
|
||||
const runtimeCache: Map<string, CacheData> = new Map();
|
||||
|
||||
export function checkCached(key: string): boolean {
|
||||
return runtimeCache.has(key);
|
||||
}
|
||||
|
||||
export function getCached<T>(key: string, callback: () => T): T {
|
||||
if (!runtimeCache.has(key)) {
|
||||
try {
|
||||
const data = callback();
|
||||
runtimeCache.set(key, { data });
|
||||
} catch (error) {
|
||||
console.log(`Failed retrieving data from callback: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
return runtimeCache.get(key).data as T;
|
||||
}
|
||||
|
||||
export function setCached<T>(key: string, value: T): T {
|
||||
runtimeCache.set(key, { data: value });
|
||||
return runtimeCache.get(key).data as T;
|
||||
}
|
||||
|
||||
export function invalidate(key) {
|
||||
runtimeCache.delete(key);
|
||||
}
|
||||
|
||||
export function clear() {
|
||||
runtimeCache.clear();
|
||||
}
|
||||
|
||||
function createCacheStore() {
|
||||
return {
|
||||
checkCached,
|
||||
getCached,
|
||||
setCached,
|
||||
invalidate,
|
||||
clear,
|
||||
};
|
||||
}
|
||||
|
||||
export const runtimeCacheStore = createCacheStore();
|
||||
@@ -0,0 +1,457 @@
|
||||
import { MaybeNumber, OntimeEvent, Playback, Runtime, TimerState, TimerType } from 'ontime-types';
|
||||
import { calculateDuration, dayInMs } from 'ontime-utils';
|
||||
|
||||
import { clock } from '../services/Clock.js';
|
||||
import { RestorePoint } from '../services/RestoreService.js';
|
||||
|
||||
import {
|
||||
getCurrent,
|
||||
getExpectedEnd,
|
||||
getExpectedFinish,
|
||||
getRollTimers,
|
||||
getRuntimeOffset,
|
||||
skippedOutOfEvent,
|
||||
updateRoll,
|
||||
} from '../services/timerUtils.js';
|
||||
import { timerConfig } from '../config/config.js';
|
||||
|
||||
const initialRuntime: Runtime = {
|
||||
selectedEventIndex: null,
|
||||
numEvents: 0,
|
||||
offset: null,
|
||||
plannedStart: 0,
|
||||
plannedEnd: 0,
|
||||
actualStart: null,
|
||||
expectedEnd: null,
|
||||
};
|
||||
|
||||
const initialTimer: TimerState = {
|
||||
addedTime: 0,
|
||||
current: null,
|
||||
duration: null,
|
||||
elapsed: null,
|
||||
expectedFinish: null, // TODO: expected finish could account for midnight, we cleanup in the clients
|
||||
finishedAt: null,
|
||||
playback: Playback.Stop,
|
||||
secondaryTimer: null,
|
||||
startedAt: null,
|
||||
};
|
||||
|
||||
export type RuntimeState = {
|
||||
clock: number; // realtime clock
|
||||
eventNow: OntimeEvent | null;
|
||||
publicEventNow: OntimeEvent | null;
|
||||
eventNext: OntimeEvent | null;
|
||||
publicEventNext: OntimeEvent | null;
|
||||
runtime: Runtime;
|
||||
timer: TimerState;
|
||||
// private properties of the timer calculations
|
||||
_timer: {
|
||||
totalDelay: number; // this value comes from rundown service
|
||||
pausedAt: MaybeNumber;
|
||||
secondaryTarget: MaybeNumber;
|
||||
};
|
||||
};
|
||||
|
||||
const runtimeState: RuntimeState = {
|
||||
clock: clock.timeNow(),
|
||||
eventNow: null,
|
||||
publicEventNow: null,
|
||||
eventNext: null,
|
||||
publicEventNext: null,
|
||||
runtime: initialRuntime,
|
||||
timer: { ...initialTimer },
|
||||
_timer: {
|
||||
totalDelay: 0,
|
||||
pausedAt: null,
|
||||
secondaryTarget: null,
|
||||
},
|
||||
};
|
||||
|
||||
export function getState(): Readonly<RuntimeState> {
|
||||
return runtimeState;
|
||||
}
|
||||
|
||||
export function clear() {
|
||||
runtimeState.eventNow = null;
|
||||
runtimeState.publicEventNow = null;
|
||||
runtimeState.eventNext = null;
|
||||
runtimeState.publicEventNext = null;
|
||||
|
||||
runtimeState.runtime = {
|
||||
...initialRuntime,
|
||||
// persist session stuff
|
||||
actualStart: runtimeState.runtime.actualStart,
|
||||
numEvents: runtimeState.runtime.numEvents,
|
||||
};
|
||||
|
||||
runtimeState.timer.playback = Playback.Stop;
|
||||
runtimeState.clock = clock.timeNow();
|
||||
runtimeState.timer = { ...initialTimer };
|
||||
|
||||
// we maintain the total delay
|
||||
runtimeState._timer.pausedAt = null;
|
||||
runtimeState._timer.secondaryTarget = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to allow modifying the state from the outside
|
||||
* @param newState
|
||||
*/
|
||||
function patchTimer(newState: Partial<TimerState>) {
|
||||
for (const key in newState) {
|
||||
if (key in runtimeState.timer) {
|
||||
runtimeState.timer[key] = newState[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type RundownData = {
|
||||
numEvents: number;
|
||||
firstStart: MaybeNumber;
|
||||
lastEnd: MaybeNumber;
|
||||
totalDelay: number;
|
||||
totalDuration: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Utility, allows updating data derived from the rundown
|
||||
* @param playableRundown
|
||||
*/
|
||||
export function updateRundownData(rundownData: RundownData) {
|
||||
runtimeState._timer.totalDelay = rundownData.totalDelay;
|
||||
|
||||
runtimeState.runtime.numEvents = rundownData.numEvents;
|
||||
runtimeState.runtime.plannedStart = rundownData.firstStart;
|
||||
runtimeState.runtime.plannedEnd = rundownData.firstStart + rundownData.totalDuration;
|
||||
runtimeState.runtime.expectedEnd = getExpectedEnd(runtimeState);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a given event into state
|
||||
* @param event
|
||||
* @param rundown
|
||||
* @param initialData
|
||||
*/
|
||||
export function load(
|
||||
event: OntimeEvent,
|
||||
rundown: OntimeEvent[],
|
||||
initialData?: Partial<TimerState & RestorePoint>,
|
||||
): boolean {
|
||||
clear();
|
||||
|
||||
const eventIndex = rundown.findIndex((eventInMemory) => eventInMemory.id === event.id);
|
||||
|
||||
runtimeState.runtime.selectedEventIndex = eventIndex;
|
||||
|
||||
loadNow(event, rundown);
|
||||
loadNext(rundown);
|
||||
|
||||
runtimeState.clock = clock.timeNow();
|
||||
runtimeState.timer.playback = Playback.Armed;
|
||||
runtimeState.timer.duration = calculateDuration(event.timeStart, event.timeEnd);
|
||||
runtimeState.timer.current = getCurrent(runtimeState);
|
||||
|
||||
if (initialData) {
|
||||
patchTimer(initialData);
|
||||
|
||||
const firstStart = initialData?.firstStart;
|
||||
if (firstStart === null || typeof firstStart === 'number') {
|
||||
runtimeState.runtime.actualStart = firstStart;
|
||||
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
||||
runtimeState.runtime.expectedEnd = getExpectedEnd(runtimeState);
|
||||
}
|
||||
}
|
||||
|
||||
return event.id === runtimeState.eventNow?.id;
|
||||
}
|
||||
|
||||
export function loadNow(event: OntimeEvent, playableEvents: OntimeEvent[]) {
|
||||
runtimeState.eventNow = event;
|
||||
|
||||
// check if current is also public
|
||||
if (event.isPublic) {
|
||||
runtimeState.publicEventNow = event;
|
||||
} else {
|
||||
// assume there is no public event
|
||||
runtimeState.publicEventNow = null;
|
||||
|
||||
// if there is nothing before, return
|
||||
if (!runtimeState.runtime.selectedEventIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
// iterate backwards to find it
|
||||
for (let i = runtimeState.runtime.selectedEventIndex; i >= 0; i--) {
|
||||
if (playableEvents[i].isPublic) {
|
||||
runtimeState.publicEventNow = playableEvents[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function loadNext(playableEvents: OntimeEvent[]) {
|
||||
// assume there are no next events
|
||||
runtimeState.eventNext = null;
|
||||
runtimeState.publicEventNext = null;
|
||||
|
||||
if (runtimeState.runtime.selectedEventIndex === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const numEvents = playableEvents.length;
|
||||
|
||||
if (runtimeState.runtime.selectedEventIndex < numEvents - 1) {
|
||||
let nextPublic = false;
|
||||
let nextProduction = false;
|
||||
|
||||
for (let i = runtimeState.runtime.selectedEventIndex + 1; i < numEvents; i++) {
|
||||
// if we have not set private
|
||||
if (!nextProduction) {
|
||||
runtimeState.eventNext = playableEvents[i];
|
||||
nextProduction = true;
|
||||
}
|
||||
|
||||
// if event is public
|
||||
if (playableEvents[i].isPublic) {
|
||||
runtimeState.publicEventNext = playableEvents[i];
|
||||
nextPublic = true;
|
||||
}
|
||||
|
||||
// Stop if both are set
|
||||
if (nextPublic && nextProduction) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function resume(restorePoint: RestorePoint, event: OntimeEvent, rundown: OntimeEvent[]) {
|
||||
load(event, rundown, restorePoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* We only pass an event if we are hot reloading
|
||||
* @param {OntimeEvent} event only passed if we are changing the data if a playing timer
|
||||
*/
|
||||
export function reload(event?: OntimeEvent) {
|
||||
if (event) {
|
||||
runtimeState.eventNow = event;
|
||||
|
||||
// update data which is duplicate between eventNow and timer objects
|
||||
runtimeState.timer.duration = calculateDuration(runtimeState.eventNow.timeStart, runtimeState.eventNow.timeEnd);
|
||||
runtimeState.timer.current = getCurrent(runtimeState);
|
||||
runtimeState.timer.expectedFinish = getExpectedFinish(runtimeState);
|
||||
return;
|
||||
}
|
||||
runtimeState.timer.playback = Playback.Armed;
|
||||
|
||||
runtimeState.timer.duration = calculateDuration(runtimeState.eventNow.timeStart, runtimeState.eventNow.timeEnd);
|
||||
runtimeState.timer.current = runtimeState.timer.duration;
|
||||
runtimeState.timer.elapsed = null;
|
||||
|
||||
runtimeState.timer.startedAt = null;
|
||||
runtimeState.timer.finishedAt = null;
|
||||
runtimeState._timer.pausedAt = null;
|
||||
runtimeState.timer.addedTime = 0;
|
||||
|
||||
runtimeState.timer.expectedFinish = getExpectedFinish(runtimeState);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in situations when we want to reload all events
|
||||
* without interrupting timer
|
||||
* @param eventNow
|
||||
* @param playableEvents
|
||||
*/
|
||||
export function reloadAll(eventNow: OntimeEvent, playableEvents: OntimeEvent[]) {
|
||||
loadNow(eventNow, playableEvents);
|
||||
loadNext(playableEvents);
|
||||
reload(eventNow);
|
||||
}
|
||||
|
||||
export function start(state: RuntimeState = runtimeState): boolean {
|
||||
if (state.eventNow === null) {
|
||||
return false;
|
||||
}
|
||||
if (state.timer.playback === Playback.Play) {
|
||||
return false;
|
||||
}
|
||||
state.clock = clock.timeNow();
|
||||
state.timer.secondaryTimer = null;
|
||||
state._timer.secondaryTarget = null;
|
||||
|
||||
// add paused time if it exists
|
||||
if (state._timer.pausedAt) {
|
||||
const timeToAdd = state.clock - state._timer.pausedAt;
|
||||
state.timer.addedTime += timeToAdd;
|
||||
state._timer.pausedAt = null;
|
||||
}
|
||||
|
||||
if (state.timer.startedAt === null) {
|
||||
state.timer.startedAt = state.clock;
|
||||
}
|
||||
|
||||
state.timer.playback = Playback.Play;
|
||||
state.timer.expectedFinish = getExpectedFinish(state);
|
||||
state.timer.elapsed = 0;
|
||||
|
||||
// update runtime delays: over - under
|
||||
if (state.runtime.actualStart === null) {
|
||||
state.runtime.actualStart = state.clock;
|
||||
}
|
||||
|
||||
state.runtime.offset = getRuntimeOffset(state);
|
||||
state.runtime.expectedEnd = state.runtime.plannedEnd + state.runtime.offset;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function pause(state: RuntimeState = runtimeState): boolean {
|
||||
if (state.timer.playback !== Playback.Play) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state.timer.playback = Playback.Pause;
|
||||
state.clock = clock.timeNow();
|
||||
state._timer.pausedAt = state.clock;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function stop(state: RuntimeState = runtimeState): boolean {
|
||||
if (state.timer.playback === Playback.Stop) {
|
||||
return false;
|
||||
}
|
||||
|
||||
clear();
|
||||
runtimeState.runtime.actualStart = null;
|
||||
runtimeState.runtime.expectedEnd = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function addTime(amount: number) {
|
||||
if (runtimeState.timer.current === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
runtimeState.timer.addedTime += amount;
|
||||
runtimeState.timer.expectedFinish += amount;
|
||||
runtimeState.timer.current += amount;
|
||||
|
||||
// handle edge cases
|
||||
const willGoNegative = amount < 0 && Math.abs(amount) > runtimeState.timer.current;
|
||||
const hasFinished = runtimeState.timer.finishedAt !== null;
|
||||
if (willGoNegative && !hasFinished) {
|
||||
runtimeState.timer.finishedAt = clock.timeNow();
|
||||
} else {
|
||||
const willGoPositive = runtimeState.timer.current < 0 && runtimeState.timer.current + amount > 0;
|
||||
if (willGoPositive) {
|
||||
runtimeState.timer.finishedAt = null;
|
||||
}
|
||||
}
|
||||
|
||||
// update runtime delays: over - under
|
||||
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
||||
runtimeState.runtime.expectedEnd = getExpectedEnd(runtimeState);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export type UpdateResult = {
|
||||
hasTimerFinished: boolean;
|
||||
shouldCallRoll: boolean;
|
||||
};
|
||||
|
||||
export function update(): UpdateResult {
|
||||
let hasTimerFinished = false;
|
||||
let shouldCallRoll = false; // we also need to call roll if a secondary timer has finished
|
||||
|
||||
const previousTime = runtimeState.clock;
|
||||
runtimeState.clock = clock.timeNow();
|
||||
|
||||
// we call integrations if we update timers
|
||||
if (runtimeState.timer.playback === Playback.Roll) {
|
||||
const result = onRollUpdate();
|
||||
shouldCallRoll = result.doRoll;
|
||||
hasTimerFinished = result.isFinished;
|
||||
} else if (runtimeState.timer.startedAt !== null) {
|
||||
// we only update timer if a timer has been started
|
||||
const result = onPlayUpdate();
|
||||
hasTimerFinished = result.isFinished;
|
||||
} else if (runtimeState.eventNow?.timerType === TimerType.TimeToEnd) {
|
||||
// or if we are in a time-to-end timer
|
||||
runtimeState.timer.current = getCurrent(runtimeState);
|
||||
runtimeState.timer.duration = runtimeState.timer.current;
|
||||
}
|
||||
|
||||
// update offset
|
||||
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
||||
|
||||
return {
|
||||
hasTimerFinished,
|
||||
shouldCallRoll,
|
||||
};
|
||||
|
||||
function onRollUpdate() {
|
||||
const hasSkippedOutOfEvent = skippedOutOfEvent(runtimeState, previousTime, timerConfig.skipLimit);
|
||||
if (hasSkippedOutOfEvent) {
|
||||
return { doRoll: true };
|
||||
}
|
||||
const { updatedTimer, updatedSecondaryTimer, doRollLoad, isFinished } = updateRoll(runtimeState);
|
||||
runtimeState.timer.current = updatedTimer;
|
||||
runtimeState.timer.secondaryTimer = updatedSecondaryTimer;
|
||||
runtimeState.timer.elapsed = runtimeState.timer.duration - runtimeState.timer.current;
|
||||
|
||||
return { doRoll: doRollLoad, isFinished };
|
||||
}
|
||||
|
||||
function onPlayUpdate() {
|
||||
let isFinished = false;
|
||||
runtimeState.timer.current = getCurrent(runtimeState);
|
||||
const finishedNow =
|
||||
runtimeState.timer.current <= timerConfig.triggerAhead && runtimeState.timer.finishedAt === null;
|
||||
|
||||
if (runtimeState.timer.playback === Playback.Play && finishedNow) {
|
||||
runtimeState.timer.finishedAt = runtimeState.clock;
|
||||
isFinished = true;
|
||||
} else {
|
||||
runtimeState.timer.expectedFinish = getExpectedFinish(runtimeState);
|
||||
}
|
||||
|
||||
runtimeState.timer.elapsed = runtimeState.timer.duration - runtimeState.timer.current;
|
||||
|
||||
return { isFinished };
|
||||
}
|
||||
}
|
||||
|
||||
export function roll(rundown: OntimeEvent[]) {
|
||||
clear();
|
||||
|
||||
runtimeState.runtime.numEvents = rundown.length;
|
||||
const { nextEvent, currentEvent } = getRollTimers(rundown, runtimeState.clock);
|
||||
|
||||
if (currentEvent) {
|
||||
// there is something running, load
|
||||
runtimeState.timer.secondaryTimer = null;
|
||||
runtimeState._timer.secondaryTarget = null;
|
||||
|
||||
// account for event that finishes the day after
|
||||
const endTime =
|
||||
currentEvent.timeEnd < currentEvent.timeStart ? currentEvent.timeEnd + dayInMs : currentEvent.timeEnd;
|
||||
|
||||
// when we load a timer in roll, we do the same things as before
|
||||
// but also pre-populate some data as to the running state
|
||||
load(currentEvent, rundown, {
|
||||
startedAt: currentEvent.timeStart,
|
||||
expectedFinish: currentEvent.timeEnd,
|
||||
current: endTime - runtimeState.clock,
|
||||
});
|
||||
} else if (nextEvent) {
|
||||
// account for day after
|
||||
const nextStart = nextEvent.timeStart < runtimeState.clock ? nextEvent.timeStart + dayInMs : nextEvent.timeStart;
|
||||
// nothing now, but something coming up
|
||||
runtimeState.timer.secondaryTimer = nextStart - runtimeState.clock;
|
||||
runtimeState._timer.secondaryTarget = nextStart;
|
||||
}
|
||||
|
||||
runtimeState.timer.playback = Playback.Roll;
|
||||
}
|
||||
Reference in New Issue
Block a user