1- import { PackageContainerOnPackage , Accessor , AccessorOnPackage } from '@sofie-automation/blueprints-integration'
2- import { getExpectedPackageIdForPieceInstance } from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
3- import { PeripheralDeviceId , ExpectedPackageId , PieceInstanceId } from '@sofie-automation/corelib/dist/dataModel/Ids'
1+ import {
2+ PackageContainerOnPackage ,
3+ Accessor ,
4+ AccessorOnPackage ,
5+ ExpectedPackage ,
6+ } from '@sofie-automation/blueprints-integration'
7+ import { PeripheralDeviceId , ExpectedPackageId } from '@sofie-automation/corelib/dist/dataModel/Ids'
48import { protectString , unprotectString } from '@sofie-automation/corelib/dist/protectedString'
59import {
610 PackageManagerExpectedPackage ,
@@ -15,7 +19,7 @@ import { DBStudio, StudioLight, StudioPackageContainer } from '@sofie-automation
1519import { clone , omit } from '../../../lib/tempLib'
1620import { CustomPublishCollection } from '../../../lib/customPublication'
1721import { logger } from '../../../logging'
18- import { ExpectedPackagesContentCache } from './contentCache'
22+ import { ExpectedPackageDBCompact , ExpectedPackagesContentCache } from './contentCache'
1923import type { StudioFields } from './publication'
2024
2125/**
@@ -59,17 +63,7 @@ export async function updateCollectionForExpectedPackageIds(
5963 // Filter, keep only the routed mappings for this device:
6064 if ( filterPlayoutDeviceIds && ! filterPlayoutDeviceIds . includes ( deviceId ) ) continue
6165
62- const routedPackage = generateExpectedPackageForDevice (
63- studio ,
64- {
65- ...packageDoc . package ,
66- _id : unprotectString ( packageDoc . _id ) ,
67- } ,
68- deviceId ,
69- null ,
70- Priorities . OTHER , // low priority
71- packageContainers
72- )
66+ const routedPackage = generateExpectedPackageForDevice ( studio , packageDoc , deviceId , packageContainers )
7367
7468 updatedDocIds . add ( routedPackage . _id )
7569 collection . replace ( routedPackage )
@@ -78,95 +72,15 @@ export async function updateCollectionForExpectedPackageIds(
7872
7973 // Remove all documents for an ExpectedPackage that was regenerated, and no update was issues
8074 collection . remove ( ( doc ) => {
81- if ( doc . pieceInstanceId ) return false
82-
83- if ( missingExpectedPackageIds . has ( protectString ( doc . expectedPackage . _id ) ) ) return true
84-
85- if ( updatedDocIds . has ( doc . _id ) && ! regenerateIds . has ( protectString ( doc . expectedPackage . _id ) ) ) return true
86-
87- return false
88- } )
89- }
90-
91- /**
92- * Regenerate the output for the provided PieceInstance `regenerateIds`, updating the data in `collection` as needed
93- * @param contentCache Cache of the database documents used
94- * @param studio Minimal studio document
95- * @param layerNameToDeviceIds Lookup table of package layers, to PeripheralDeviceIds the layer could be used with
96- * @param collection Output collection of the publication
97- * @param filterPlayoutDeviceIds PeripheralDeviceId filter applied to this publication
98- * @param regenerateIds Ids of PieceInstance documents to be recalculated
99- */
100- export async function updateCollectionForPieceInstanceIds (
101- contentCache : ReadonlyDeep < ExpectedPackagesContentCache > ,
102- studio : Pick < DBStudio , StudioFields > ,
103- layerNameToDeviceIds : Map < string , PeripheralDeviceId [ ] > ,
104- packageContainers : Record < string , StudioPackageContainer > ,
105- collection : CustomPublishCollection < PackageManagerExpectedPackage > ,
106- filterPlayoutDeviceIds : ReadonlyDeep < PeripheralDeviceId [ ] > | undefined ,
107- regenerateIds : Set < PieceInstanceId >
108- ) : Promise < void > {
109- const updatedDocIds = new Set < PackageManagerExpectedPackageId > ( )
110- const missingPieceInstanceIds = new Set < PieceInstanceId > ( )
111-
112- for ( const pieceInstanceId of regenerateIds ) {
113- const pieceInstanceDoc = contentCache . PieceInstances . findOne ( pieceInstanceId )
114- if ( ! pieceInstanceDoc ) {
115- missingPieceInstanceIds . add ( pieceInstanceId )
116- continue
117- }
118- if ( ! pieceInstanceDoc . piece ?. expectedPackages ) continue
119-
120- pieceInstanceDoc . piece . expectedPackages . forEach ( ( expectedPackage , i ) => {
121- const sanitisedPackageId = getExpectedPackageIdForPieceInstance (
122- pieceInstanceId ,
123- expectedPackage . _id || '__unnamed' + i
124- )
125-
126- // Map the expectedPackages onto their specified layer:
127- const allDeviceIds = new Set < PeripheralDeviceId > ( )
128- for ( const layerName of expectedPackage . layers ) {
129- const layerDeviceIds = layerNameToDeviceIds . get ( layerName )
130- for ( const deviceId of layerDeviceIds || [ ] ) {
131- allDeviceIds . add ( deviceId )
132- }
133- }
134-
135- for ( const deviceId of allDeviceIds ) {
136- // Filter, keep only the routed mappings for this device:
137- if ( filterPlayoutDeviceIds && ! filterPlayoutDeviceIds . includes ( deviceId ) ) continue
138-
139- const routedPackage = generateExpectedPackageForDevice (
140- studio ,
141- {
142- ...expectedPackage ,
143- _id : unprotectString ( sanitisedPackageId ) ,
144- } ,
145- deviceId ,
146- pieceInstanceId ,
147- Priorities . OTHER , // low priority
148- packageContainers
149- )
150-
151- updatedDocIds . add ( routedPackage . _id )
152- collection . replace ( routedPackage )
153- }
154- } )
155- }
156-
157- // Remove all documents for an ExpectedPackage that was regenerated, and no update was issues
158- collection . remove ( ( doc ) => {
159- if ( ! doc . pieceInstanceId ) return false
160-
161- if ( missingPieceInstanceIds . has ( doc . pieceInstanceId ) ) return true
75+ if ( missingExpectedPackageIds . has ( doc . expectedPackage . _id ) ) return true
16276
163- if ( updatedDocIds . has ( doc . _id ) && ! regenerateIds . has ( doc . pieceInstanceId ) ) return true
77+ if ( updatedDocIds . has ( doc . _id ) && ! regenerateIds . has ( doc . expectedPackage . _id ) ) return true
16478
16579 return false
16680 } )
16781}
16882
169- enum Priorities {
83+ export enum ExpectedPackagePriorities {
17084 // Lower priorities are done first
17185
17286 /** Highest priority */
@@ -181,16 +95,14 @@ function generateExpectedPackageForDevice(
18195 StudioLight ,
18296 '_id' | 'packageContainersWithOverrides' | 'previewContainerIds' | 'thumbnailContainerIds'
18397 > ,
184- expectedPackage : PackageManagerExpectedPackageBase ,
98+ expectedPackage : ExpectedPackageDBCompact ,
18599 deviceId : PeripheralDeviceId ,
186- pieceInstanceId : PieceInstanceId | null ,
187- priority : Priorities ,
188100 packageContainers : Record < string , StudioPackageContainer >
189101) : PackageManagerExpectedPackage {
190102 // Lookup Package sources:
191103 const combinedSources : PackageContainerOnPackage [ ] = [ ]
192104
193- for ( const packageSource of expectedPackage . sources ) {
105+ for ( const packageSource of expectedPackage . package . sources ) {
194106 const lookedUpSource = packageContainers [ packageSource . containerId ]
195107 if ( lookedUpSource ) {
196108 combinedSources . push ( calculateCombinedSource ( packageSource , lookedUpSource ) )
@@ -208,27 +120,27 @@ function generateExpectedPackageForDevice(
208120 }
209121
210122 // Lookup Package targets:
211- const combinedTargets = calculateCombinedTargets ( expectedPackage , deviceId , packageContainers )
123+ const combinedTargets = calculateCombinedTargets ( expectedPackage . package , deviceId , packageContainers )
212124
213- if ( ! combinedSources . length && expectedPackage . sources . length !== 0 ) {
125+ if ( ! combinedSources . length && expectedPackage . package . sources . length !== 0 ) {
214126 logger . warn ( `Pub.expectedPackagesForDevice: No sources found for "${ expectedPackage . _id } "` )
215127 }
216128 if ( ! combinedTargets . length ) {
217129 logger . warn ( `Pub.expectedPackagesForDevice: No targets found for "${ expectedPackage . _id } "` )
218130 }
219- const packageSideEffect = getSideEffect ( expectedPackage , studio )
131+ const packageSideEffect = getSideEffect ( expectedPackage . package , studio )
220132
221133 return {
222- _id : protectString ( `${ expectedPackage . _id } _${ deviceId } _ ${ pieceInstanceId } ` ) ,
134+ _id : protectString ( `${ expectedPackage . _id } _${ deviceId } ` ) ,
223135 expectedPackage : {
224- ...expectedPackage ,
136+ ...expectedPackage . package ,
137+ _id : expectedPackage . _id ,
225138 sideEffect : packageSideEffect ,
226139 } ,
227140 sources : combinedSources ,
228141 targets : combinedTargets ,
229- priority : priority ,
142+ priority : ExpectedPackagePriorities . OTHER , // This gets overriden later if needed
230143 playoutDeviceId : deviceId ,
231- pieceInstanceId,
232144 }
233145}
234146
@@ -265,7 +177,7 @@ function calculateCombinedSource(
265177 return combinedSource
266178}
267179function calculateCombinedTargets (
268- expectedPackage : PackageManagerExpectedPackageBase ,
180+ expectedPackage : ReadonlyDeep < ExpectedPackage . Base > ,
269181 deviceId : PeripheralDeviceId ,
270182 packageContainers : Record < string , StudioPackageContainer >
271183) : PackageContainerOnPackage [ ] {
0 commit comments