Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add relevantBlock function #1100

Merged
merged 26 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a377e92
add relevantBlock function and test
alex-Arc Jun 23, 2024
e1c3f2a
rename to getRelevantBlock
alex-Arc Jun 23, 2024
4bc6b4c
use OntimeRundown not Normalized
alex-Arc Jun 23, 2024
b54589d
add block now to runtime state
alex-Arc Jun 23, 2024
4408690
filter in cusheet with currentBlockId
alex-Arc Jun 23, 2024
a173c27
do in only 1 iteration
alex-Arc Jun 25, 2024
9852190
fix test
alex-Arc Jun 25, 2024
6f47f33
Merge remote-tracking branch 'org/master' into relevantBlock
alex-Arc Jun 25, 2024
62cf573
0 index case
alex-Arc Jun 25, 2024
9df62dc
create block state object
alex-Arc Jun 29, 2024
e36aa4b
Merge branch 'master' into relevantBlock
alex-Arc Jul 11, 2024
83098ef
rename to currentBlock
alex-Arc Jul 17, 2024
1317e95
Merge branch 'master' into relevantBlock
alex-Arc Jul 17, 2024
92580d8
slight rename
alex-Arc Jul 17, 2024
1aaa07b
rename to CurrentBlockState
alex-Arc Jul 17, 2024
a6b6657
remove console.log
alex-Arc Jul 17, 2024
d9a139e
typo
alex-Arc Jul 17, 2024
dd5ec5f
Merge branch 'master' into relevantBlock
alex-Arc Jul 19, 2024
3cb3093
unused import
alex-Arc Jul 19, 2024
d0db8f3
lost in Merge
alex-Arc Jul 19, 2024
8c95019
filter events
alex-Arc Jul 19, 2024
75fc00d
pass full rundown to functions and use filter utils
alex-Arc Jul 19, 2024
84beba1
Merge branch 'master' into relevantBlock
alex-Arc Jul 22, 2024
0c51f2f
save to previousState to avoid constant update
alex-Arc Jul 22, 2024
b0385e4
in the same block keep the startedAt time
alex-Arc Jul 22, 2024
61a7d10
null chaining
alex-Arc Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/client/src/common/hooks/useSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const setAuxTimer = {
export const useCuesheet = () => {
const featureSelector = (state: RuntimeStore) => ({
playback: state.timer.playback,
currentBlockId: state.currentBlock.block?.id ?? null,
selectedEventId: state.eventNow?.id ?? null,
selectedEventIndex: state.runtime.selectedEventIndex,
numEvents: state.runtime.numEvents,
Expand Down
4 changes: 4 additions & 0 deletions apps/client/src/common/stores/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export const runtimeStorePlaceholder: RuntimeStore = {
actualStart: null,
expectedEnd: null,
},
currentBlock: {
block: null,
startedAt: null,
},
eventNow: null,
eventNext: null,
publicEventNow: null,
Expand Down
5 changes: 5 additions & 0 deletions apps/client/src/common/utils/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ export const connectSocket = () => {
updateDevTools({ eventNow: payload });
break;
}
case 'ontime-currentBlock': {
patchRuntime('currentBlock', payload);
updateDevTools({ currentBlock: payload });
break;
}
case 'ontime-publicEventNow': {
patchRuntime('publicEventNow', payload);
updateDevTools({ publicEventNow: payload });
Expand Down
14 changes: 8 additions & 6 deletions apps/client/src/features/cuesheet/Cuesheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ interface CuesheetProps {
columns: ColumnDef<OntimeRundownEntry>[];
handleUpdate: (rowIndex: number, accessor: keyof OntimeRundownEntry, payload: unknown) => void;
selectedId: string | null;
currentBlockId: string | null;
}

export default function Cuesheet({ data, columns, handleUpdate, selectedId }: CuesheetProps) {
export default function Cuesheet({ data, columns, handleUpdate, selectedId, currentBlockId }: CuesheetProps) {
const { followSelected, showSettings, showDelayBlock, showPrevious, showIndexColumn } = useCuesheetSettings();

const {
columnVisibility,
columnOrder,
Expand Down Expand Up @@ -114,11 +114,16 @@ export default function Cuesheet({ data, columns, handleUpdate, selectedId }: Cu
}

if (isOntimeBlock(row.original)) {
if (isPast && !showPrevious && key !== currentBlockId) {
return null;
}
return <BlockRow key={key} title={row.original.title} />;
}
if (isOntimeDelay(row.original)) {
if (isPast && !showPrevious) {
return null;
}
const delayVal = row.original.duration;

if (!showDelayBlock || delayVal === 0) {
return null;
}
Expand All @@ -128,9 +133,6 @@ export default function Cuesheet({ data, columns, handleUpdate, selectedId }: Cu
if (isOntimeEvent(row.original)) {
eventIndex++;
const isSelected = key === selectedId;
if (isSelected) {
isPast = false;
}

if (isPast && !showPrevious) {
return null;
Expand Down
1 change: 1 addition & 0 deletions apps/client/src/features/cuesheet/CuesheetWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export default function CuesheetWrapper() {
columns={columns}
handleUpdate={handleUpdate}
selectedId={featureData.selectedEventId}
currentBlockId={featureData.currentBlockId}
/>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export const startServer = async (
message: messageService.getState(),
runtime: state.runtime,
eventNow: state.eventNow,
currentBlock: {
block: null,
startedAt: null,
},
publicEventNow: state.publicEventNow,
eventNext: state.eventNext,
publicEventNext: state.publicEventNext,
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/services/TimerService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OntimeEvent } from 'ontime-types';
import { OntimeRundown } from 'ontime-types';

import * as runtimeState from '../stores/runtimeState.js';
import type { UpdateResult } from '../stores/runtimeState.js';
Expand Down Expand Up @@ -106,7 +106,7 @@ export class TimerService {
* Loads roll information into timer service
* @param {OntimeEvent[]} rundown -- list of events to run
*/
roll(rundown: OntimeEvent[]) {
roll(rundown: OntimeRundown) {
runtimeState.roll(rundown);
}

Expand Down
17 changes: 9 additions & 8 deletions apps/server/src/services/rundown-service/RundownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import { eventStore } from '../../stores/EventStore.js';

type PatchWithId = (Partial<OntimeEvent> | Partial<OntimeBlock> | Partial<OntimeDelay>) & { id: string };

type CompleteEntry<T> = T extends Partial<OntimeEvent>
? OntimeEvent
: T extends Partial<OntimeDelay>
? OntimeDelay
: T extends Partial<OntimeBlock>
? OntimeBlock
: never;
type CompleteEntry<T> =
T extends Partial<OntimeEvent>
? OntimeEvent
: T extends Partial<OntimeDelay>
? OntimeDelay
: T extends Partial<OntimeBlock>
? OntimeBlock
: never;

function generateEvent<T extends Partial<OntimeEvent> | Partial<OntimeDelay> | Partial<OntimeBlock>>(
eventData: T,
Expand Down Expand Up @@ -240,7 +241,7 @@ function notifyChanges(options: { timer?: boolean | string[]; external?: boolean
// notify timer service of changed events
// timer can be true or an array of changed IDs
const affected = Array.isArray(options.timer) ? options.timer : undefined;
runtimeService.maybeUpdate(playableEvents, affected);
runtimeService.maybeUpdate(affected);
}
}

Expand Down
33 changes: 21 additions & 12 deletions apps/server/src/services/runtime-service/RuntimeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TimerLifeCycle,
TimerPhase,
} from 'ontime-types';
import { millisToString, validatePlayback } from 'ontime-utils';
import { filterPlayable, millisToString, validatePlayback } from 'ontime-utils';

import { deepEqual } from 'fast-equals';

Expand All @@ -28,6 +28,7 @@ import {
getNextEventWithCue,
getEventWithId,
getPlayableEvents,
getRundown,
} from '../rundown-service/rundownUtils.js';
import { skippedOutOfEvent } from '../timerUtils.js';
import { integrationService } from '../integration-service/IntegrationService.js';
Expand Down Expand Up @@ -95,7 +96,7 @@ class RuntimeService {
}

// we dont call this.roll because we need to bypass the checks
const rundown = getPlayableEvents();
const rundown = getRundown();
// TODO: by not calling roll, we dont get the events
this.eventTimer.roll(rundown);
}
Expand Down Expand Up @@ -217,7 +218,7 @@ class RuntimeService {
* Called when the underlying data has changed,
* we check if the change affects the runtime
*/
maybeUpdate(playableEvents: OntimeEvent[], affectedIds?: string[]) {
maybeUpdate(affectedIds?: string[]) {
const state = runtimeState.getState();
const hasLoadedElements = state.eventNow !== null || state.eventNext !== null;
if (!hasLoadedElements) {
Expand Down Expand Up @@ -245,15 +246,17 @@ class RuntimeService {
if (onlyChangedNow) {
runtimeState.reload(eventNow);
} else {
runtimeState.reloadAll(eventNow, playableEvents);
const rundown = getRundown();
runtimeState.reloadAll(eventNow, rundown);
}
return;
}

// Maybe the event will become the next
isNext = this.isNewNext();
if (isNext) {
runtimeState.loadNext(playableEvents);
const rundown = getRundown();
runtimeState.loadNext(rundown);
}
}

Expand All @@ -269,8 +272,8 @@ class RuntimeService {
return false;
}

const timedEvents = getPlayableEvents();
const success = runtimeState.load(event, timedEvents);
const rundown = getRundown();
const success = runtimeState.load(event, rundown);

if (success) {
logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`);
Expand Down Expand Up @@ -513,13 +516,14 @@ class RuntimeService {
return;
}

const playableEvents = getPlayableEvents();
const rundown = getRundown();
const playableEvents = filterPlayable(rundown);
if (playableEvents.length === 0) {
logger.warning(LogOrigin.Server, 'Roll: no events found');
return;
}

this.eventTimer.roll(playableEvents);
this.eventTimer.roll(rundown);

const state = runtimeState.getState();
const newState = state.timer.playback;
Expand All @@ -543,14 +547,14 @@ class RuntimeService {
}

// the db would have to change for the event not to exist
// we do not kow the reason for the crash, so we check anyway
// we do not know the reason for the crash, so we check anyway
const event = getEventWithId(selectedEventId);
if (!event || !isOntimeEvent(event)) {
return;
}

const timedEvents = getPlayableEvents();
runtimeState.resume(restorePoint, event, timedEvents);
const rundown = getRundown();
runtimeState.resume(restorePoint, event, rundown);
logger.info(LogOrigin.Playback, 'Resuming playback');
}

Expand Down Expand Up @@ -620,6 +624,11 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
updateEventIfChanged('eventNext', state);
updateEventIfChanged('publicEventNext', state);

if (!deepEqual(RuntimeService?.previousState.currentBlock, state.currentBlock)) {
eventStore.set('currentBlock', state.currentBlock);
RuntimeService.previousState.currentBlock = { ...state.currentBlock };
}

if (shouldUpdateClock) {
RuntimeService.previousClockUpdate = state.clock;
eventStore.set('clock', state.clock);
Expand Down
12 changes: 8 additions & 4 deletions apps/server/src/services/timerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ type RollTimers = {

/**
* Finds loading information given a current rundown and time
* @param {OntimeEvent[]} rundown - List of playable events
* @param {OntimeEvent[]} playableEvents - List of playable events
* @param {number} timeNow - time now in ms
*/
export const getRollTimers = (rundown: OntimeEvent[], timeNow: number, currentIndex?: number | null): RollTimers => {
export const getRollTimers = (
playableEvents: OntimeEvent[],
timeNow: number,
currentIndex?: number | null,
): RollTimers => {
let nowIndex: MaybeNumber = null; // index of event now
let nowId: MaybeString = null; // id of event now
let publicIndex: MaybeNumber = null; // index of public event now
Expand All @@ -140,8 +144,8 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number, currentIn
let publicTimeToNext: MaybeNumber = null; // counter: time for next public event

const hasLoaded = currentIndex !== null;
const canFilter = hasLoaded && currentIndex === rundown.length - 1;
const filteredRundown = canFilter ? rundown.slice(currentIndex) : rundown;
const canFilter = hasLoaded && currentIndex === playableEvents.length - 1;
const filteredRundown = canFilter ? playableEvents.slice(currentIndex) : playableEvents;

const lastEvent = filteredRundown.at(-1);
const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/stores/__tests__/runtimeState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('mutation on runtimeState', () => {
expect(newState.eventNow?.id).toBe(mockEvent.id);
expect(newState.timer.playback).toBe(Playback.Armed);
expect(newState.clock).not.toBe(666);
expect(newState.currentBlock.block).toBeNull();

// 2. Start event
let success = start();
Expand Down Expand Up @@ -171,6 +172,7 @@ describe('mutation on runtimeState', () => {
expect(newState.runtime.actualStart).toBeNull();
expect(newState.runtime.plannedStart).toBe(0);
expect(newState.runtime.plannedEnd).toBe(1500);
expect(newState.currentBlock.block).toBeNull();

// 2. Start event
start();
Expand Down Expand Up @@ -202,6 +204,7 @@ describe('mutation on runtimeState', () => {
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);
expect(newState.currentBlock.block).toBeNull();

// 4. Add time
addTime(10);
Expand Down
Loading
Loading