Skip to content

Commit 77cc59b

Browse files
author
Mint de Wit
committed
chore: optimise getSegmentsBetweenMarkers
1 parent 5c78889 commit 77cc59b

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

packages/job-worker/src/playout/model/PlayoutModel.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ export interface PlayoutModel extends PlayoutModelReadonly, StudioPlayoutModelBa
335335
*/
336336
setQuickLoopMarker(type: 'start' | 'end', marker: QuickLoopMarker | null): void
337337

338+
/**
339+
* Returns any segmentId's that are found between 2 quickloop markers, none will be returned if
340+
* the end is before the start.
341+
* @param start A quickloop marker
342+
* @param end A quickloop marker
343+
*/
338344
getSegmentsBetweenQuickLoopMarker(start: QuickLoopMarker, end: QuickLoopMarker): SegmentId[]
339345

340346
calculatePartTimings(

packages/job-worker/src/playout/model/implementation/PlayoutModelImpl.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,11 @@ export class PlayoutModelImpl extends PlayoutModelReadonlyImpl implements Playou
589589

590590
if (regenerateActivationId) this.playlistImpl.activationId = getRandomId()
591591

592-
// reset quickloop:
593-
this.setQuickLoopMarker('start', null)
594-
this.setQuickLoopMarker('end', null)
592+
// reset quickloop if applicable:
593+
if (this.playlist.quickLoop && !this.playlist.quickLoop.locked) {
594+
this.setQuickLoopMarker('start', null)
595+
this.setQuickLoopMarker('end', null)
596+
}
595597

596598
this.#playlistHasChanged = true
597599
}

packages/job-worker/src/playout/model/services/QuickLoopService.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,35 +150,55 @@ export class QuickLoopService {
150150
}
151151

152152
getSegmentsBetweenMarkers(startMarker: QuickLoopMarker, endMarker: QuickLoopMarker): SegmentId[] {
153-
const orderedParts = this.playoutModel.getAllOrderedParts()
154-
const rundownIds = this.playoutModel.getRundownIds()
155-
156-
const start = this.findQuickLoopMarkerPosition(startMarker, 'start', orderedParts, rundownIds)
157-
const end = this.findQuickLoopMarkerPosition(endMarker, 'end', orderedParts, rundownIds)
153+
const segments = this.playoutModel.getAllOrderedSegments()
154+
const segmentIds: SegmentId[] = []
158155

159-
if (this.areMarkersFlipped(start, end)) return []
156+
let passedStart = false
157+
let seenLastRundown = false
160158

161-
const segmentIds: Set<SegmentId> = new Set()
159+
for (const s of segments) {
160+
if (
161+
(!passedStart &&
162+
((startMarker.type === QuickLoopMarkerType.PART && s.getPart(startMarker.id)) ||
163+
(startMarker.type === QuickLoopMarkerType.SEGMENT && s.segment._id === startMarker.id) ||
164+
(startMarker.type === QuickLoopMarkerType.RUNDOWN &&
165+
s.segment.rundownId === startMarker.id))) ||
166+
startMarker.type === QuickLoopMarkerType.PLAYLIST
167+
) {
168+
// the start marker is inside this segment, is this segment, or this is the first segment that is in the loop
169+
// segments from here on are included in the loop
170+
passedStart = true
171+
}
162172

163-
for (const part of orderedParts) {
164-
const currentSegment = this.playoutModel.findSegment(part.segmentId)?.segment
165-
const currentRundownIndex = rundownIds.findIndex((id) => id === part.rundownId)
173+
if (endMarker.type === QuickLoopMarkerType.RUNDOWN) {
174+
// last rundown needs to be inclusive so we need to break once the rundownId is not equal to segment's rundownId
175+
if (s.segment.rundownId === endMarker.id) {
176+
if (!passedStart) {
177+
// we hit the end before the start so quit now:
178+
break
179+
}
180+
seenLastRundown = true
181+
} else if (seenLastRundown) {
182+
// we have passed the last rundown
183+
break
184+
}
185+
}
166186

167-
if (!currentSegment) continue // ???
187+
if (passedStart) {
188+
// passed the start but we have not seen the end yet
189+
segmentIds.push(s.segment._id)
190+
}
168191

169192
if (
170-
currentRundownIndex >= start.rundownRank &&
171-
currentRundownIndex <= end.rundownRank &&
172-
currentSegment._rank >= start.segmentRank &&
173-
currentSegment._rank <= end.segmentRank &&
174-
part._rank >= start.partRank &&
175-
part._rank <= start.partRank
193+
(endMarker.type === QuickLoopMarkerType.PART && s.getPart(endMarker.id)) ||
194+
(endMarker.type === QuickLoopMarkerType.SEGMENT && s.segment._id === endMarker.id)
176195
) {
177-
segmentIds.add(currentSegment._id)
196+
// the endMarker is in this segment or this segment is the end marker
197+
break
178198
}
179199
}
180200

181-
return Array.from(segmentIds.values())
201+
return segmentIds
182202
}
183203

184204
private areMarkersFlipped(startPosition: MarkerPosition, endPosition: MarkerPosition) {

packages/job-worker/src/playout/quickLoopMarkers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { setNextPart } from './setNext'
88
import { resetPartInstancesWithPieceInstances } from './lib'
99
import { QuickLoopMarker, QuickLoopMarkerType } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
1010
import { SegmentId } from '@sofie-automation/corelib/dist/dataModel/Ids'
11+
import { clone } from 'underscore'
1112

1213
export async function handleSetQuickLoopMarker(context: JobContext, data: SetQuickLoopMarkerProps): Promise<void> {
1314
return runJobWithPlayoutModel(
@@ -20,7 +21,7 @@ export async function handleSetQuickLoopMarker(context: JobContext, data: SetQui
2021
async (playoutModel) => {
2122
const playlist = playoutModel.playlist
2223
if (!playlist.activationId) throw new Error(`Playlist has no activationId!`)
23-
const oldProps = playoutModel.playlist.quickLoop
24+
const oldProps = clone(playoutModel.playlist.quickLoop)
2425
const wasQuickLoopRunning = oldProps?.running
2526
playoutModel.setQuickLoopMarker(data.type, data.marker)
2627

0 commit comments

Comments
 (0)