Skip to content

Commit 4c00959

Browse files
committed
wip
1 parent ce24481 commit 4c00959

File tree

2 files changed

+93
-111
lines changed

2 files changed

+93
-111
lines changed

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/unbound-method */
22
import { setupDefaultJobEnvironment } from '../../__mocks__/context'
33
import { setupMockShowStyleCompound } from '../../__mocks__/presetCollections'
4-
import { PartInstanceToSync, SyncChangesToPartInstancesWorker } from '../syncChangesToPartInstance'
4+
import { findInstancesToSync, PartInstanceToSync, SyncChangesToPartInstancesWorker } from '../syncChangesToPartInstance'
55
import { mock } from 'jest-mock-extended'
66
import type { PlayoutModel } from '../../playout/model/PlayoutModel'
77
import type { IngestModelReadonly } from '../model/IngestModel'
@@ -20,9 +20,9 @@ const mockOptions = {
2020
funcPropSupport: true,
2121
}
2222

23-
function createMockPart(): DBPart {
23+
function createMockPart(id: string): DBPart {
2424
return {
25-
_id: protectString('mockPartId'),
25+
_id: protectString(id),
2626
_rank: 1,
2727
externalId: 'mockPartExternalId',
2828
segmentId: protectString('mockSegmentId'),
@@ -54,24 +54,12 @@ describe('SyncChangesToPartInstancesWorker', () => {
5454

5555
test('No partInstances', async () => {
5656
const context = setupDefaultJobEnvironment()
57-
const showStyleCompound = await setupMockShowStyleCompound(context)
58-
const blueprint = await context.getShowStyleBlueprint(showStyleCompound._id)
5957

6058
const playoutModel = createMockPlayoutModel()
6159
const ingestModel = createMockIngestModelReadonly()
6260
const rundownModel = createMockPlayoutRundownModel()
6361

64-
const worker = new SyncChangesToPartInstancesWorker(
65-
context,
66-
playoutModel,
67-
ingestModel,
68-
rundownModel,
69-
showStyleCompound,
70-
blueprint
71-
)
72-
expect(worker).toBeTruthy()
73-
74-
const instancesToSync = worker.findInstancesToSync()
62+
const instancesToSync = findInstancesToSync(context, playoutModel, ingestModel, rundownModel)
7563
expect(instancesToSync).toHaveLength(0)
7664
})
7765

@@ -104,12 +92,12 @@ describe('SyncChangesToPartInstancesWorker', () => {
10492
)
10593
}
10694

107-
function createMockPartInstance(): PlayoutPartInstanceModel {
95+
function createMockPartInstance(id: string): PlayoutPartInstanceModel {
10896
return mock<PlayoutPartInstanceModel>(
10997
{
11098
partInstance: {
111-
_id: protectString('mockPartInstanceId'),
112-
part: createMockPart(),
99+
_id: protectString(id),
100+
part: createMockPart(id),
113101
segmentId: protectString('mockSegmentId'),
114102
rundownId: protectString('mockRundownId'),
115103
takeCount: 0,
@@ -147,13 +135,12 @@ describe('SyncChangesToPartInstancesWorker', () => {
147135
context,
148136
playoutModel,
149137
ingestModel,
150-
rundownModel,
151138
showStyleCompound,
152139
blueprint
153140
)
154141

155-
const partInstance = createMockPartInstance()
156-
const part = createMockPart()
142+
const partInstance = createMockPartInstance('mockPartInstanceId')
143+
const part = createMockPart('mockPartId')
157144

158145
const instanceToSync: PartInstanceToSync = {
159146
playoutRundownModel: rundownModel,

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

Lines changed: 84 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,9 @@ export async function syncChangesToPartInstances(
7575
return
7676
}
7777

78-
const worker = new SyncChangesToPartInstancesWorker(
79-
context,
80-
playoutModel,
81-
ingestModel,
82-
playoutRundownModel,
83-
showStyle,
84-
blueprint
85-
)
78+
const instancesToSync = findInstancesToSync(context, playoutModel, ingestModel, playoutRundownModel)
8679

87-
const instancesToSync = worker.findInstancesToSync()
80+
const worker = new SyncChangesToPartInstancesWorker(context, playoutModel, ingestModel, showStyle, blueprint)
8881

8982
for (const instanceToSync of instancesToSync) {
9083
await worker.syncChangesToPartInstance(instanceToSync)
@@ -100,102 +93,23 @@ export class SyncChangesToPartInstancesWorker {
10093
readonly #context: JobContext
10194
readonly #playoutModel: PlayoutModel
10295
readonly #ingestModel: IngestModelReadonly
103-
readonly #playoutRundownModel: PlayoutRundownModel
10496
readonly #showStyle: ReadonlyDeep<ProcessedShowStyleCompound>
10597
readonly #blueprint: ReadonlyDeep<WrappedShowStyleBlueprint>
10698

10799
constructor(
108100
context: JobContext,
109101
playoutModel: PlayoutModel,
110102
ingestModel: IngestModelReadonly,
111-
playoutRundownModel: PlayoutRundownModel,
112103
showStyle: ReadonlyDeep<ProcessedShowStyleCompound>,
113104
blueprint: ReadonlyDeep<WrappedShowStyleBlueprint>
114105
) {
115106
this.#context = context
116107
this.#playoutModel = playoutModel
117108
this.#ingestModel = ingestModel
118-
this.#playoutRundownModel = playoutRundownModel
119109
this.#showStyle = showStyle
120110
this.#blueprint = blueprint
121111
}
122112

123-
findInstancesToSync(): PartInstanceToSync[] {
124-
const currentPartInstance = this.#playoutModel.currentPartInstance
125-
const nextPartInstance = this.#playoutModel.nextPartInstance
126-
const previousPartInstance = this.#playoutModel.previousPartInstance
127-
128-
const instancesToSync: PartInstanceToSync[] = []
129-
if (currentPartInstance) {
130-
// If the currentPartInstance is adlibbed we probably also need to find the earliest
131-
// non-adlibbed Part within this segment and check it for updates too. It may have something
132-
// changed (like timing) that will affect what's going on.
133-
// The previous "planned" Part Instance needs to be inserted into the `instances` first, so that
134-
// it's ran first through the blueprints.
135-
if (currentPartInstance.partInstance.orphaned === 'adlib-part') {
136-
const partAndPartInstance = findLastUnorphanedPartInstanceInSegment(
137-
this.#playoutModel,
138-
currentPartInstance.partInstance
139-
)
140-
if (partAndPartInstance) {
141-
const lastPartRundownModel = findPlayoutRundownModel(
142-
this.#playoutRundownModel,
143-
this.#playoutModel,
144-
partAndPartInstance.partInstance.partInstance.part.rundownId
145-
)
146-
147-
insertToSyncedInstanceCandidates(
148-
this.#context,
149-
instancesToSync,
150-
this.#playoutModel,
151-
lastPartRundownModel,
152-
this.#ingestModel,
153-
partAndPartInstance.partInstance,
154-
null,
155-
partAndPartInstance.part,
156-
'previous'
157-
)
158-
}
159-
}
160-
161-
// We can now run the current Part Instance.
162-
const currentPartRundownModel = findPlayoutRundownModel(
163-
this.#playoutRundownModel,
164-
this.#playoutModel,
165-
currentPartInstance.partInstance.part.rundownId
166-
)
167-
findPartAndInsertToSyncedInstanceCandidates(
168-
this.#context,
169-
instancesToSync,
170-
this.#playoutModel,
171-
currentPartRundownModel,
172-
this.#ingestModel,
173-
currentPartInstance,
174-
previousPartInstance,
175-
'current'
176-
)
177-
}
178-
if (nextPartInstance) {
179-
const nextPartRundownModel = findPlayoutRundownModel(
180-
this.#playoutRundownModel,
181-
this.#playoutModel,
182-
nextPartInstance.partInstance.part.rundownId
183-
)
184-
findPartAndInsertToSyncedInstanceCandidates(
185-
this.#context,
186-
instancesToSync,
187-
this.#playoutModel,
188-
nextPartRundownModel,
189-
this.#ingestModel,
190-
nextPartInstance,
191-
currentPartInstance,
192-
currentPartInstance?.isTooCloseToAutonext(false) ? 'current' : 'next'
193-
)
194-
}
195-
196-
return instancesToSync
197-
}
198-
199113
async syncChangesToPartInstance(instanceToSync: PartInstanceToSync): Promise<void> {
200114
const { existingPartInstance } = instanceToSync
201115

@@ -222,7 +136,7 @@ export class SyncChangesToPartInstancesWorker {
222136
},
223137
this.#context.studio,
224138
this.#showStyle,
225-
this.#playoutRundownModel.rundown,
139+
instanceToSync.playoutRundownModel.rundown,
226140
existingPartInstance,
227141
proposedPieceInstances,
228142
instanceToSync.playStatus
@@ -341,6 +255,87 @@ export class SyncChangesToPartInstancesWorker {
341255
}
342256
}
343257

258+
export function findInstancesToSync(
259+
context: JobContext,
260+
playoutModel: PlayoutModel,
261+
ingestModel: IngestModelReadonly,
262+
playoutRundownModel: PlayoutRundownModel
263+
): PartInstanceToSync[] {
264+
const currentPartInstance = playoutModel.currentPartInstance
265+
const nextPartInstance = playoutModel.nextPartInstance
266+
const previousPartInstance = playoutModel.previousPartInstance
267+
268+
const instancesToSync: PartInstanceToSync[] = []
269+
if (currentPartInstance) {
270+
// If the currentPartInstance is adlibbed we probably also need to find the earliest
271+
// non-adlibbed Part within this segment and check it for updates too. It may have something
272+
// changed (like timing) that will affect what's going on.
273+
// The previous "planned" Part Instance needs to be inserted into the `instances` first, so that
274+
// it's ran first through the blueprints.
275+
if (currentPartInstance.partInstance.orphaned === 'adlib-part') {
276+
const partAndPartInstance = findLastUnorphanedPartInstanceInSegment(
277+
playoutModel,
278+
currentPartInstance.partInstance
279+
)
280+
if (partAndPartInstance) {
281+
const lastPartRundownModel = findPlayoutRundownModel(
282+
playoutRundownModel,
283+
playoutModel,
284+
partAndPartInstance.partInstance.partInstance.part.rundownId
285+
)
286+
287+
insertToSyncedInstanceCandidates(
288+
context,
289+
instancesToSync,
290+
playoutModel,
291+
lastPartRundownModel,
292+
ingestModel,
293+
partAndPartInstance.partInstance,
294+
null,
295+
partAndPartInstance.part,
296+
'previous'
297+
)
298+
}
299+
}
300+
301+
// We can now run the current Part Instance.
302+
const currentPartRundownModel = findPlayoutRundownModel(
303+
playoutRundownModel,
304+
playoutModel,
305+
currentPartInstance.partInstance.part.rundownId
306+
)
307+
findPartAndInsertToSyncedInstanceCandidates(
308+
context,
309+
instancesToSync,
310+
playoutModel,
311+
currentPartRundownModel,
312+
ingestModel,
313+
currentPartInstance,
314+
previousPartInstance,
315+
'current'
316+
)
317+
}
318+
if (nextPartInstance) {
319+
const nextPartRundownModel = findPlayoutRundownModel(
320+
playoutRundownModel,
321+
playoutModel,
322+
nextPartInstance.partInstance.part.rundownId
323+
)
324+
findPartAndInsertToSyncedInstanceCandidates(
325+
context,
326+
instancesToSync,
327+
playoutModel,
328+
nextPartRundownModel,
329+
ingestModel,
330+
nextPartInstance,
331+
currentPartInstance,
332+
currentPartInstance?.isTooCloseToAutonext(false) ? 'current' : 'next'
333+
)
334+
}
335+
336+
return instancesToSync
337+
}
338+
344339
/**
345340
* Inserts given PartInstances and underlying Part to the list of PartInstances to be synced
346341
*/

0 commit comments

Comments
 (0)