@@ -10,7 +10,10 @@ import throttleToNextTick from '@sofie-automation/shared-lib/dist/lib/throttleTo
10
10
import _ = require( 'underscore' )
11
11
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
12
12
import { PartInstanceId , PieceInstanceId } from '@sofie-automation/corelib/dist/dataModel/Ids'
13
- import { processAndPrunePieceInstanceTimings } from '@sofie-automation/corelib/dist/playout/processAndPrune'
13
+ import {
14
+ processAndPrunePieceInstanceTimings ,
15
+ resolvePrunedPieceInstance ,
16
+ } from '@sofie-automation/corelib/dist/playout/processAndPrune'
14
17
import { ShowStyleBaseExt , ShowStyleBaseHandler } from './showStyleBaseHandler'
15
18
import { PlaylistHandler } from './playlistHandler'
16
19
import { SourceLayers } from '@sofie-automation/corelib/dist/dataModel/ShowStyleBase'
@@ -44,8 +47,7 @@ export class PieceInstancesHandler
44
47
private _partInstances : SelectedPartInstances | undefined
45
48
46
49
private _throttledUpdateAndNotify = throttleToNextTick ( ( ) => {
47
- this . updateCollectionData ( )
48
- this . notify ( this . _collectionData ) . catch ( this . _logger . error )
50
+ this . updateAndNotify ( ) . catch ( this . _logger . error )
49
51
} )
50
52
51
53
constructor ( logger : Logger , coreHandler : CoreHandler ) {
@@ -72,13 +74,33 @@ export class PieceInstancesHandler
72
74
73
75
private processAndPrunePieceInstanceTimings (
74
76
partInstance : DBPartInstance | undefined ,
75
- pieceInstances : PieceInstance [ ]
77
+ pieceInstances : PieceInstance [ ] ,
78
+ filterActive : boolean
76
79
) : ReadonlyDeep < PieceInstance > [ ] {
77
80
// Approximate when 'now' is in the PartInstance, so that any adlibbed Pieces will be timed roughly correctly
78
81
const partStarted = partInstance ?. timings ?. plannedStartedPlayback
79
82
const nowInPart = partStarted === undefined ? 0 : Date . now ( ) - partStarted
80
83
81
- return processAndPrunePieceInstanceTimings ( this . _sourceLayers , pieceInstances , nowInPart , false , false )
84
+ const prunedPieceInstances = processAndPrunePieceInstanceTimings (
85
+ this . _sourceLayers ,
86
+ pieceInstances ,
87
+ nowInPart ,
88
+ false ,
89
+ false
90
+ )
91
+ if ( ! filterActive ) return prunedPieceInstances
92
+
93
+ return prunedPieceInstances . filter ( ( pieceInstance ) => {
94
+ const resolvedPieceInstance = resolvePrunedPieceInstance ( nowInPart , pieceInstance )
95
+
96
+ return (
97
+ resolvedPieceInstance . resolvedStart <= nowInPart &&
98
+ ( resolvedPieceInstance . resolvedDuration == null ||
99
+ resolvedPieceInstance . resolvedStart + resolvedPieceInstance . resolvedDuration > nowInPart ) &&
100
+ pieceInstance . piece . virtual !== true &&
101
+ pieceInstance . disabled !== true
102
+ )
103
+ } )
82
104
}
83
105
84
106
private updateCollectionData ( ) : boolean {
@@ -89,36 +111,37 @@ export class PieceInstancesHandler
89
111
const inPreviousPartInstance = this . _currentPlaylist ?. previousPartInfo ?. partInstanceId
90
112
? this . processAndPrunePieceInstanceTimings (
91
113
this . _partInstances ?. previous ,
92
- collection . find ( { partInstanceId : this . _currentPlaylist . previousPartInfo . partInstanceId } )
114
+ collection . find ( { partInstanceId : this . _currentPlaylist . previousPartInfo . partInstanceId } ) ,
115
+ true
93
116
)
94
117
: [ ]
95
118
const inCurrentPartInstance = this . _currentPlaylist ?. currentPartInfo ?. partInstanceId
96
119
? this . processAndPrunePieceInstanceTimings (
97
120
this . _partInstances ?. current ,
98
- collection . find ( { partInstanceId : this . _currentPlaylist . currentPartInfo . partInstanceId } )
121
+ collection . find ( { partInstanceId : this . _currentPlaylist . currentPartInfo . partInstanceId } ) ,
122
+ true
99
123
)
100
124
: [ ]
101
125
const inNextPartInstance = this . _currentPlaylist ?. nextPartInfo ?. partInstanceId
102
126
? this . processAndPrunePieceInstanceTimings (
103
127
undefined ,
104
- collection . find ( { partInstanceId : this . _currentPlaylist . nextPartInfo . partInstanceId } )
128
+ collection . find ( { partInstanceId : this . _currentPlaylist . nextPartInfo . partInstanceId } ) ,
129
+ false
105
130
)
106
131
: [ ]
107
132
108
- const active = [ ...inPreviousPartInstance , ...inCurrentPartInstance ] . filter ( ( pieceInstance ) =>
109
- this . isPieceInstanceActive ( pieceInstance )
110
- )
133
+ const active = [ ...inPreviousPartInstance , ...inCurrentPartInstance ]
111
134
112
135
let hasAnythingChanged = false
113
- if ( ! areElementsShallowEqual ( this . _collectionData . active , active ) ) {
136
+ if ( ! _ . isEqual ( this . _collectionData . active , active ) ) {
114
137
this . _collectionData . active = active
115
138
hasAnythingChanged = true
116
139
}
117
140
if (
118
- ! areElementsShallowEqual ( this . _collectionData . currentPartInstance , inCurrentPartInstance ) &&
141
+ ! _ . isEqual ( this . _collectionData . currentPartInstance , inCurrentPartInstance ) &&
119
142
( this . _collectionData . currentPartInstance . length !== inCurrentPartInstance . length ||
120
143
this . _collectionData . currentPartInstance . some ( ( pieceInstance , index ) => {
121
- return ! arePropertiesShallowEqual < ReadonlyDeep < PieceInstance > > (
144
+ return ! arePropertiesDeepEqual < ReadonlyDeep < PieceInstance > > (
122
145
inCurrentPartInstance [ index ] ,
123
146
pieceInstance ,
124
147
[ 'reportedStartedPlayback' , 'reportedStoppedPlayback' ]
@@ -128,7 +151,7 @@ export class PieceInstancesHandler
128
151
this . _collectionData . currentPartInstance = inCurrentPartInstance
129
152
hasAnythingChanged = true
130
153
}
131
- if ( ! areElementsShallowEqual ( this . _collectionData . nextPartInstance , inNextPartInstance ) ) {
154
+ if ( ! _ . isEqual ( this . _collectionData . nextPartInstance , inNextPartInstance ) ) {
132
155
this . _collectionData . nextPartInstance = inNextPartInstance
133
156
hasAnythingChanged = true
134
157
}
@@ -235,28 +258,9 @@ export class PieceInstancesHandler
235
258
await this . notify ( this . _collectionData )
236
259
}
237
260
}
238
-
239
- private isPieceInstanceActive ( pieceInstance : ReadonlyDeep < PieceInstance > ) {
240
- return (
241
- pieceInstance . reportedStoppedPlayback == null &&
242
- pieceInstance . piece . virtual !== true &&
243
- pieceInstance . disabled !== true &&
244
- ( pieceInstance . partInstanceId === this . _currentPlaylist ?. previousPartInfo ?. partInstanceId || // a piece from previous part instance may be active during transition
245
- pieceInstance . partInstanceId === this . _currentPlaylist ?. currentPartInfo ?. partInstanceId ) &&
246
- ( pieceInstance . reportedStartedPlayback != null || // has been reported to have started by the Playout Gateway
247
- pieceInstance . plannedStartedPlayback != null || // a time to start playing has been set by Core
248
- ( pieceInstance . partInstanceId === this . _currentPlaylist ?. currentPartInfo ?. partInstanceId &&
249
- pieceInstance . piece . enable . start === 0 ) || // this is to speed things up immediately after a part instance is taken when not yet reported by the Playout Gateway
250
- pieceInstance . infinite ?. fromPreviousPart ) // infinites from previous part also are on air from the start of the current part
251
- )
252
- }
253
261
}
254
262
255
- export function arePropertiesShallowEqual < T extends Record < string , any > > (
256
- a : T ,
257
- b : T ,
258
- omitProperties : Array < keyof T >
259
- ) : boolean {
263
+ function arePropertiesDeepEqual < T extends Record < string , any > > ( a : T , b : T , omitProperties : Array < keyof T > ) : boolean {
260
264
if ( typeof a !== 'object' || a == null || typeof b !== 'object' || b == null ) {
261
265
return false
262
266
}
@@ -267,7 +271,7 @@ export function arePropertiesShallowEqual<T extends Record<string, any>>(
267
271
if ( keysA . length !== keysB . length ) return false
268
272
269
273
for ( const key of keysA ) {
270
- if ( ! keysB . includes ( key ) || a [ key ] !== b [ key ] ) {
274
+ if ( ! keysB . includes ( key ) || ! _ . isEqual ( a [ key ] , b [ key ] ) ) {
271
275
return false
272
276
}
273
277
}
0 commit comments