Skip to content

Commit 5eb61fc

Browse files
committed
wip: more refactoring
1 parent b5af9c1 commit 5eb61fc

File tree

1 file changed

+80
-60
lines changed

1 file changed

+80
-60
lines changed

packages/job-worker/src/ingest/syncChangesToPartInstance.ts

Lines changed: 80 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { PlayoutPartInstanceModel } from '../playout/model/PlayoutPartInstanceMo
99
import { IngestModelReadonly } from './model/IngestModel'
1010
import { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
1111
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
12-
import { Piece } from '@sofie-automation/corelib/dist/dataModel/Piece'
1312
import { stringifyError } from '@sofie-automation/shared-lib/dist/lib/stringifyError'
1413
import { logger } from '../logging'
1514
import {
@@ -35,16 +34,17 @@ import { PlayoutRundownModel } from '../playout/model/PlayoutRundownModel'
3534
import { PieceInstance } from '@sofie-automation/corelib/dist/dataModel/PieceInstance'
3635
import { setNextPart } from '../playout/setNext'
3736
import { ensureNextPartIsValid } from './updateNext'
38-
import { PartId } from '@sofie-automation/corelib/dist/dataModel/Ids'
37+
import { PartId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
3938
import type { WrappedShowStyleBlueprint } from '../blueprints/cache'
4039

4140
type PlayStatus = 'previous' | 'current' | 'next'
4241
export interface PartInstanceToSync {
42+
playoutRundownModel: PlayoutRundownModel
4343
existingPartInstance: PlayoutPartInstanceModel
4444
previousPartInstance: PlayoutPartInstanceModel | null
4545
playStatus: PlayStatus
4646
newPart: ReadonlyDeep<DBPart> | undefined
47-
piecesThatMayBeActive: Promise<ReadonlyDeep<Piece>[]>
47+
proposedPieceInstances: Promise<PieceInstance[]>
4848
}
4949

5050
/**
@@ -84,7 +84,7 @@ export async function syncChangesToPartInstances(
8484
blueprint
8585
)
8686

87-
const instancesToSync = worker.findInstancesToSync(context, playoutModel, ingestModel)
87+
const instancesToSync = worker.findInstancesToSync()
8888

8989
for (const instanceToSync of instancesToSync) {
9090
await worker.syncChangesToPartInstance(instanceToSync)
@@ -120,14 +120,10 @@ export class SyncChangesToPartInstancesWorker {
120120
this.#blueprint = blueprint
121121
}
122122

123-
findInstancesToSync(
124-
context: JobContext,
125-
playoutModel: PlayoutModel,
126-
ingestModel: IngestModelReadonly
127-
): PartInstanceToSync[] {
128-
const currentPartInstance = playoutModel.currentPartInstance
129-
const nextPartInstance = playoutModel.nextPartInstance
130-
const previousPartInstance = playoutModel.previousPartInstance
123+
findInstancesToSync(): PartInstanceToSync[] {
124+
const currentPartInstance = this.#playoutModel.currentPartInstance
125+
const nextPartInstance = this.#playoutModel.nextPartInstance
126+
const previousPartInstance = this.#playoutModel.previousPartInstance
131127

132128
const instancesToSync: PartInstanceToSync[] = []
133129
if (currentPartInstance) {
@@ -138,39 +134,59 @@ export class SyncChangesToPartInstancesWorker {
138134
// it's ran first through the blueprints.
139135
if (currentPartInstance.partInstance.orphaned === 'adlib-part') {
140136
const partAndPartInstance = findLastUnorphanedPartInstanceInSegment(
141-
playoutModel,
137+
this.#playoutModel,
142138
currentPartInstance.partInstance
143139
)
144140
if (partAndPartInstance) {
141+
const lastPartRundownModel = findPlayoutRundownModel(
142+
this.#playoutRundownModel,
143+
this.#playoutModel,
144+
partAndPartInstance.partInstance.partInstance.part.rundownId
145+
)
146+
145147
insertToSyncedInstanceCandidates(
146-
context,
148+
this.#context,
147149
instancesToSync,
148-
playoutModel,
149-
ingestModel,
150+
this.#playoutModel,
151+
lastPartRundownModel,
152+
this.#ingestModel,
150153
partAndPartInstance.partInstance,
151154
null,
152155
partAndPartInstance.part,
153156
'previous'
154157
)
155158
}
156159
}
160+
157161
// We can now run the current Part Instance.
162+
const currentPartRundownModel = findPlayoutRundownModel(
163+
this.#playoutRundownModel,
164+
this.#playoutModel,
165+
currentPartInstance.partInstance.part.rundownId
166+
)
158167
findPartAndInsertToSyncedInstanceCandidates(
159-
context,
168+
this.#context,
160169
instancesToSync,
161-
playoutModel,
162-
ingestModel,
170+
this.#playoutModel,
171+
currentPartRundownModel,
172+
this.#ingestModel,
163173
currentPartInstance,
164174
previousPartInstance,
165175
'current'
166176
)
167177
}
168178
if (nextPartInstance) {
179+
const nextPartRundownModel = findPlayoutRundownModel(
180+
this.#playoutRundownModel,
181+
this.#playoutModel,
182+
nextPartInstance.partInstance.part.rundownId
183+
)
169184
findPartAndInsertToSyncedInstanceCandidates(
170-
context,
185+
this.#context,
171186
instancesToSync,
172-
playoutModel,
173-
ingestModel,
187+
this.#playoutModel,
188+
nextPartRundownModel,
189+
this.#ingestModel,
174190
nextPartInstance,
175191
currentPartInstance,
176192
currentPartInstance?.isTooCloseToAutonext(false) ? 'current' : 'next'
@@ -192,40 +208,9 @@ export class SyncChangesToPartInstancesWorker {
192208

193209
const part = instanceToSync.newPart ?? existingPartInstance.partInstance.part
194210

195-
let playoutRundownModelForPart: PlayoutRundownModel | undefined = this.#playoutRundownModel
196-
// Handle a case where the part is in a different rundown than the playoutRundownModel:
197-
if (this.#playoutRundownModel.rundown._id !== part.rundownId) {
198-
playoutRundownModelForPart = this.#playoutModel.getRundown(part.rundownId)
199-
}
200-
if (!playoutRundownModelForPart)
201-
throw new Error(`Internal Error: playoutRundownModelForPart is undefined (it should never be)`)
202-
203-
// TMP: wrap in try/catch for troubleshooting:
204-
let proposedPieceInstances: PieceInstance[] = []
205-
try {
206-
proposedPieceInstances = getPieceInstancesForPart(
207-
this.#context,
208-
this.#playoutModel,
209-
instanceToSync.previousPartInstance,
210-
playoutRundownModelForPart,
211-
part,
212-
await instanceToSync.piecesThatMayBeActive,
213-
existingPartInstance.partInstance._id
214-
)
215-
} catch (e) {
216-
logger.error(
217-
`TROUBLESHOOTING: currentPartInstance: ${JSON.stringify(this.#playoutModel.currentPartInstance)}`
218-
)
219-
logger.error(`TROUBLESHOOTING: nextPartInstance: ${JSON.stringify(this.#playoutModel.nextPartInstance)}`)
220-
logger.error(
221-
`TROUBLESHOOTING: previousPartInstance: ${JSON.stringify(this.#playoutModel.previousPartInstance)}`
222-
)
223-
224-
throw e
225-
}
226-
227211
logger.info(`Syncing ingest changes for part: ${part._id} (orphaned: ${!!instanceToSync.newPart})`)
228212

213+
const proposedPieceInstances = await instanceToSync.proposedPieceInstances
229214
const newResultData = this.collectNewIngestDataToSync(part._id, instanceToSync, proposedPieceInstances)
230215
const partInstanceSnapshot = existingPartInstance.snapshotMakeCopy()
231216

@@ -363,26 +348,59 @@ function insertToSyncedInstanceCandidates(
363348
context: JobContext,
364349
instances: PartInstanceToSync[],
365350
playoutModel: PlayoutModel,
351+
playoutRundownModel: PlayoutRundownModel,
366352
ingestModel: IngestModelReadonly,
367353
thisPartInstance: PlayoutPartInstanceModel,
368354
previousPartInstance: PlayoutPartInstanceModel | null,
369355
part: ReadonlyDeep<DBPart> | undefined,
370356
playStatus: PlayStatus
371357
): void {
358+
const partOrInstancePart = part ?? thisPartInstance.partInstance.part
359+
360+
const piecesThatMayBeActive = fetchPiecesThatMayBeActiveForPart(
361+
context,
362+
playoutModel,
363+
ingestModel,
364+
partOrInstancePart
365+
)
366+
367+
const proposedPieceInstances = piecesThatMayBeActive.then((piecesThatMayBeActive) =>
368+
getPieceInstancesForPart(
369+
context,
370+
playoutModel,
371+
previousPartInstance,
372+
playoutRundownModel,
373+
partOrInstancePart,
374+
piecesThatMayBeActive,
375+
thisPartInstance.partInstance._id
376+
)
377+
)
378+
372379
instances.push({
380+
playoutRundownModel,
373381
existingPartInstance: thisPartInstance,
374382
previousPartInstance: previousPartInstance,
375383
playStatus,
376384
newPart: part,
377-
piecesThatMayBeActive: fetchPiecesThatMayBeActiveForPart(
378-
context,
379-
playoutModel,
380-
ingestModel,
381-
part ?? thisPartInstance.partInstance.part
382-
),
385+
proposedPieceInstances,
383386
})
384387
}
385388

389+
function findPlayoutRundownModel(
390+
ingestPlayoutRundownModel: PlayoutRundownModel,
391+
playoutModel: PlayoutModel,
392+
rundownId: RundownId
393+
): PlayoutRundownModel {
394+
if (ingestPlayoutRundownModel.rundown._id === rundownId) return ingestPlayoutRundownModel
395+
396+
// Handle a case where the part is in a different rundown than the playoutRundownModel:
397+
const playoutRundownModelForPart = playoutModel.getRundown(rundownId)
398+
if (!playoutRundownModelForPart)
399+
throw new Error(`Internal Error: playoutRundownModelForPart is undefined (it should never be)`)
400+
401+
return playoutRundownModelForPart
402+
}
403+
386404
/**
387405
* Finds the underlying Part for a given `thisPartInstance` and inserts it to the list of PartInstances to be synced.
388406
* Doesn't do anything if it can't find the underlying Part in `model`.
@@ -391,6 +409,7 @@ function findPartAndInsertToSyncedInstanceCandidates(
391409
context: JobContext,
392410
instances: PartInstanceToSync[],
393411
playoutModel: PlayoutModel,
412+
playoutRundownModel: PlayoutRundownModel,
394413
ingestModel: IngestModelReadonly,
395414
thisPartInstance: PlayoutPartInstanceModel,
396415
previousPartInstance: PlayoutPartInstanceModel | null,
@@ -402,6 +421,7 @@ function findPartAndInsertToSyncedInstanceCandidates(
402421
context,
403422
instances,
404423
playoutModel,
424+
playoutRundownModel,
405425
ingestModel,
406426
thisPartInstance,
407427
previousPartInstance,

0 commit comments

Comments
 (0)