@@ -14,39 +14,25 @@ import { PartId, StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
1414import { DummyReactiveVar } from '@sofie-automation/meteor-lib/dist/triggers/reactive-var'
1515import { ReactivePlaylistActionContext } from '@sofie-automation/meteor-lib/dist/triggers/actionFactory'
1616import { MongoQuery } from '@sofie-automation/corelib/dist/mongo'
17- import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
18- import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
19- import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
20- import { PartInstance } from '@sofie-automation/meteor-lib/dist/collections/PartInstances'
17+ import { DBRundownPlaylist , SelectedPartInstance } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
18+ import {
19+ AdLibActions ,
20+ AdLibPieces ,
21+ PartInstances ,
22+ Parts ,
23+ RundownBaselineAdLibActions ,
24+ RundownBaselineAdLibPieces ,
25+ RundownPlaylists ,
26+ Rundowns ,
27+ Segments ,
28+ } from '../../collections'
2129import { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
22- import { RundownBaselineAdLibAction } from '@sofie-automation/corelib/dist/dataModel/RundownBaselineAdLibAction'
23- import { RundownBaselineAdLibItem } from '@sofie-automation/corelib/dist/dataModel/RundownBaselineAdLibPiece'
24- import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
25- import { DBRundown } from '@sofie-automation/corelib/dist/dataModel/Rundown'
26- import { DBSegment } from '@sofie-automation/corelib/dist/dataModel/Segment'
27- import { createSyncReadOnlyMongoCollection } from './triggersContextCollection'
30+ import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
2831
2932export function hashSingleUseToken ( token : string ) : string {
3033 return getHash ( SINGLE_USE_TOKEN_SALT + token )
3134}
3235
33- /**
34- * Some synchronous read-only collections to satisfy the TriggersContext interface
35- */
36- const AdLibActions = createSyncReadOnlyMongoCollection < AdLibAction > ( CollectionName . AdLibActions )
37- const AdLibPieces = createSyncReadOnlyMongoCollection < AdLibPiece > ( CollectionName . AdLibPieces )
38- const PartInstances = createSyncReadOnlyMongoCollection < PartInstance > ( CollectionName . PartInstances )
39- const Parts = createSyncReadOnlyMongoCollection < DBPart > ( CollectionName . Parts )
40- const RundownBaselineAdLibActions = createSyncReadOnlyMongoCollection < RundownBaselineAdLibAction > (
41- CollectionName . RundownBaselineAdLibActions
42- )
43- const RundownBaselineAdLibPieces = createSyncReadOnlyMongoCollection < RundownBaselineAdLibItem > (
44- CollectionName . RundownBaselineAdLibPieces
45- )
46- const RundownPlaylists = createSyncReadOnlyMongoCollection < DBRundownPlaylist > ( CollectionName . RundownPlaylists )
47- const Rundowns = createSyncReadOnlyMongoCollection < DBRundown > ( CollectionName . Rundowns )
48- const Segments = createSyncReadOnlyMongoCollection < DBSegment > ( CollectionName . Segments )
49-
5036export const MeteorTriggersContext : TriggersContext = {
5137 MeteorCall,
5238
@@ -83,71 +69,86 @@ export const MeteorTriggersContext: TriggersContext = {
8369
8470 nonreactiveTracker : Tracker . nonreactive ,
8571
86- memoizedIsolatedAutorun : < T extends ( ... args : any ) => any > (
87- fnc : T ,
72+ memoizedIsolatedAutorun : async < TArgs extends any [ ] , TRes > (
73+ fnc : ( ... args : TArgs ) => Promise < TRes > ,
8874 _functionName : string ,
89- ...params : Parameters < T >
90- ) : ReturnType < T > => {
91- return fnc ( ...( params as any ) )
75+ ...params : TArgs
76+ ) : Promise < TRes > => {
77+ return fnc ( ...params )
9278 } ,
9379
9480 createContextForRundownPlaylistChain,
9581}
9682
97- function createContextForRundownPlaylistChain (
83+ async function createContextForRundownPlaylistChain (
9884 studioId : StudioId ,
9985 filterChain : IBaseFilterLink [ ]
100- ) : ReactivePlaylistActionContext | undefined {
101- const playlist = rundownPlaylistFilter (
86+ ) : Promise < ReactivePlaylistActionContext | undefined > {
87+ const playlist = await rundownPlaylistFilter (
10288 studioId ,
10389 filterChain . filter ( ( link ) => link . object === 'rundownPlaylist' ) as IRundownPlaylistFilterLink [ ]
10490 )
10591
10692 if ( ! playlist ) return undefined
10793
108- let currentPartId : PartId | null = null ,
109- nextPartId : PartId | null = null ,
110- currentPartInstance : PartInstance | null = null ,
111- currentSegmentPartIds : PartId [ ] = [ ] ,
112- nextSegmentPartIds : PartId [ ] = [ ]
113-
114- if ( playlist . currentPartInfo ) {
115- currentPartInstance = PartInstances . findOne ( playlist . currentPartInfo . partInstanceId ) ?? null
116- const currentPart = currentPartInstance ?. part ?? null
117- if ( currentPart ) {
118- currentPartId = currentPart . _id
119- currentSegmentPartIds = Parts . find ( {
120- segmentId : currentPart . segmentId ,
121- } ) . map ( ( part ) => part . _id )
122- }
123- }
124- if ( playlist . nextPartInfo ) {
125- const nextPart = PartInstances . findOne ( playlist . nextPartInfo . partInstanceId ) ?. part ?? null
126- if ( nextPart ) {
127- nextPartId = nextPart . _id
128- nextSegmentPartIds = Parts . find ( {
129- segmentId : nextPart . segmentId ,
130- } ) . map ( ( part ) => part . _id )
131- }
132- }
94+ const [ currentPartInfo , nextPartInfo ] = await Promise . all ( [
95+ fetchInfoForSelectedPart ( playlist . currentPartInfo ) ,
96+ fetchInfoForSelectedPart ( playlist . nextPartInfo ) ,
97+ ] )
13398
13499 return {
135100 studioId : new DummyReactiveVar ( studioId ) ,
136101 rundownPlaylistId : new DummyReactiveVar ( playlist ?. _id ) ,
137102 rundownPlaylist : new DummyReactiveVar ( playlist ) ,
138- currentRundownId : new DummyReactiveVar ( currentPartInstance ?. rundownId ?? playlist . rundownIdsInOrder [ 0 ] ?? null ) ,
139- currentPartId : new DummyReactiveVar ( currentPartId ) ,
140- currentSegmentPartIds : new DummyReactiveVar ( currentSegmentPartIds ) ,
141- nextPartId : new DummyReactiveVar ( nextPartId ) ,
142- nextSegmentPartIds : new DummyReactiveVar ( nextSegmentPartIds ) ,
103+ currentRundownId : new DummyReactiveVar (
104+ playlist . currentPartInfo ?. rundownId ?? playlist . rundownIdsInOrder [ 0 ] ?? null
105+ ) ,
106+ currentPartId : new DummyReactiveVar ( currentPartInfo ?. partId ?? null ) ,
107+ currentSegmentPartIds : new DummyReactiveVar ( currentPartInfo ?. segmentPartIds ?? [ ] ) ,
108+ nextPartId : new DummyReactiveVar ( nextPartInfo ?. partId ?? null ) ,
109+ nextSegmentPartIds : new DummyReactiveVar ( nextPartInfo ?. segmentPartIds ?? [ ] ) ,
143110 currentPartInstanceId : new DummyReactiveVar ( playlist . currentPartInfo ?. partInstanceId ?? null ) ,
144111 }
145112}
146113
147- function rundownPlaylistFilter (
114+ async function fetchInfoForSelectedPart ( partInfo : SelectedPartInstance | null ) : Promise < {
115+ partId : PartId
116+ segmentPartIds : PartId [ ]
117+ } | null > {
118+ if ( ! partInfo ) return null
119+
120+ const partInstance = ( await PartInstances . findOneAsync ( partInfo . partInstanceId , {
121+ projection : {
122+ // @ts -expect-error deep property
123+ 'part._id' : 1 ,
124+ segmentId : 1 ,
125+ } ,
126+ } ) ) as ( Pick < DBPartInstance , 'segmentId' > & { part : Pick < DBPart , '_id' > } ) | null
127+
128+ if ( ! partInstance ) return null
129+
130+ const partId = partInstance . part . _id
131+ const segmentPartIds = await Parts . findFetchAsync (
132+ {
133+ segmentId : partInstance . segmentId ,
134+ } ,
135+ {
136+ projection : {
137+ _id : 1 ,
138+ } ,
139+ }
140+ ) . then ( ( parts ) => parts . map ( ( part ) => part . _id ) )
141+
142+ return {
143+ partId,
144+ segmentPartIds,
145+ }
146+ }
147+
148+ async function rundownPlaylistFilter (
148149 studioId : StudioId ,
149150 filterChain : IRundownPlaylistFilterLink [ ]
150- ) : DBRundownPlaylist | undefined {
151+ ) : Promise < DBRundownPlaylist | undefined > {
151152 const selector : MongoQuery < DBRundownPlaylist > = {
152153 $and : [
153154 {
@@ -181,5 +182,5 @@ function rundownPlaylistFilter(
181182 }
182183 } )
183184
184- return RundownPlaylists . findOne ( selector )
185+ return RundownPlaylists . findOneAsync ( selector )
185186}
0 commit comments