@@ -9,7 +9,6 @@ import { DBSegment } from '@sofie-automation/corelib/dist/dataModel/Segment'
99import { normalizeArray } from '@sofie-automation/corelib/dist/lib'
1010
1111import { CollectionHandlers } from '../liveStatusServer.js'
12- import areElementsShallowEqual from '@sofie-automation/shared-lib/dist/lib/isShallowEqual'
1312import {
1413 ExtendedPlaylistStatusCache ,
1514 PART_INSTANCES_KEYS ,
@@ -25,7 +24,7 @@ import { toExtendedPlaylistStatus } from './helpers/playlist/extendedPlaylistSta
2524import { Piece } from '@sofie-automation/corelib/dist/dataModel/Piece'
2625import { DBRundown } from '@sofie-automation/corelib/dist/dataModel/Rundown'
2726
28- const THROTTLE_PERIOD_MS = 100
27+ const THROTTLE_PERIOD_MS = 200
2928
3029export class ExtendedActivePlaylistTopic extends WebSocketTopicBase implements WebSocketTopic {
3130 private _playlistStatusCache : ExtendedPlaylistStatusCache = {
@@ -85,86 +84,103 @@ export class ExtendedActivePlaylistTopic extends WebSocketTopicBase implements W
8584 'playlist' ,
8685 `rundownPlaylistId ${ rundownPlaylist ?. _id } , activationId ${ rundownPlaylist ?. activationId } `
8786 )
88- this . _playlistStatusCache . activePlaylist = unprotectString ( rundownPlaylist ?. activationId )
89- ? rundownPlaylist
90- : undefined
9187
92- this . throttledSendStatusToAll ( )
88+ this . updateAndNotify ( {
89+ activePlaylist : unprotectString ( rundownPlaylist ?. activationId ) ? rundownPlaylist : undefined ,
90+ } )
9391 }
9492
9593 private onRundownsUpdapte = ( rundowns : DBRundown [ ] | undefined ) : void => {
9694 this . logUpdateReceived ( 'rundowns' )
97- if ( ! rundowns ) return
98-
99- this . _playlistStatusCache . rundownsInCurrentPlaylist = this . _playlistStatusCache . activePlaylist
100- ? this . _playlistStatusCache . activePlaylist ?. rundownIdsInOrder
101- . map ( ( rundownId ) => rundowns . find ( ( rundown ) => rundown . _id === rundownId ) )
102- . filter ( ( rundown ) => rundown !== undefined )
103- : [ ]
104- this . throttledSendStatusToAll ( )
95+
96+ const newRundownsInCurrentPlaylist =
97+ rundowns && this . _playlistStatusCache . activePlaylist
98+ ? this . _playlistStatusCache . activePlaylist ?. rundownIdsInOrder
99+ . map ( ( rundownId ) => rundowns . find ( ( rundown ) => rundown . _id === rundownId ) )
100+ . filter ( ( rundown ) => rundown !== undefined )
101+ : [ ]
102+
103+ this . updateAndNotify ( {
104+ rundownsInCurrentPlaylist : newRundownsInCurrentPlaylist ,
105+ } )
105106 }
106107
107108 private onPartsUpdate = ( parts : DBPart [ ] | undefined ) : void => {
108- const previousParts = this . _playlistStatusCache . partsBySegmentId
109- this . _playlistStatusCache . partsBySegmentId = _ . groupBy ( parts ?? [ ] , 'segmentId' )
110109 this . logUpdateReceived ( 'parts' )
111110
112- const currentSegmentId = unprotectString ( this . _playlistStatusCache . currentPartInstance ?. segmentId )
113- if (
114- currentSegmentId &&
115- ! areElementsShallowEqual (
116- previousParts [ currentSegmentId ] ?? [ ] ,
117- this . _playlistStatusCache . partsBySegmentId [ currentSegmentId ] ?? [ ]
118- )
119- ) {
120- // we have to collect all the parts, but only when those from the current segment change, we should update status
121- this . throttledSendStatusToAll ( )
122- }
111+ this . updateAndNotify ( {
112+ partsBySegmentId : _ . groupBy ( parts ?? [ ] , 'segmentId' ) ,
113+ } )
123114 }
124115
125116 private onPartInstancesUpdate = ( partInstances : PartInstances | undefined ) : void => {
126117 this . logUpdateReceived ( 'partInstances' , `${ partInstances ?. inCurrentSegment . length } instances in segment` )
127-
128- if ( ! partInstances ) return
129- this . _playlistStatusCache . currentPartInstance = partInstances . current
130- this . _playlistStatusCache . nextPartInstance = partInstances . next
131- this . _playlistStatusCache . firstInstanceInSegmentPlayout = partInstances . firstInSegmentPlayout
132- this . _playlistStatusCache . partInstancesInCurrentSegment = partInstances . inCurrentSegment
133- this . throttledSendStatusToAll ( )
118+ if ( ! partInstances )
119+ this . updateAndNotify ( {
120+ currentPartInstance : undefined ,
121+ nextPartInstance : undefined ,
122+ firstInstanceInSegmentPlayout : undefined ,
123+ partInstancesInCurrentSegment : undefined ,
124+ } )
125+ else
126+ this . updateAndNotify ( {
127+ currentPartInstance : partInstances . current ,
128+ nextPartInstance : partInstances . next ,
129+ firstInstanceInSegmentPlayout : partInstances . firstInSegmentPlayout ,
130+ partInstancesInCurrentSegment : partInstances . inCurrentSegment ,
131+ } )
134132 }
135133
136134 private onPieceInstancesUpdate = ( pieceInstances : PieceInstances | undefined ) : void => {
137135 this . logUpdateReceived ( 'pieceInstances' )
138- if ( ! pieceInstances ) return
139-
140- this . _playlistStatusCache . pieceInstancesInCurrentPartInstance = pieceInstances . currentPartInstance
141- this . _playlistStatusCache . pieceInstancesInNextPartInstance = pieceInstances . nextPartInstance
142- this . throttledSendStatusToAll ( )
136+ if ( ! pieceInstances )
137+ this . updateAndNotify ( {
138+ pieceInstancesInCurrentPartInstance : undefined ,
139+ pieceInstancesInNextPartInstance : undefined ,
140+ } )
141+ else
142+ this . updateAndNotify ( {
143+ pieceInstancesInCurrentPartInstance : pieceInstances . currentPartInstance ,
144+ pieceInstancesInNextPartInstance : pieceInstances . nextPartInstance ,
145+ } )
143146 }
144147
145148 private onPiecesUpdate = ( pieces : Piece [ ] | undefined ) : void => {
146149 this . logUpdateReceived ( 'pieces' )
147- if ( ! pieces ) return
148-
149- this . _playlistStatusCache . piecesByPartId = _ . groupBy ( pieces , 'startPartId' )
150- this . throttledSendStatusToAll ( )
150+ this . updateAndNotify ( {
151+ piecesByPartId : pieces ? _ . groupBy ( pieces , 'startPartId' ) : undefined ,
152+ } )
151153 }
152154
153155 private onShowStyleBaseUpdate = ( showStyleBase : ShowStyleBaseExt | undefined ) : void => {
154156 this . logUpdateReceived ( 'showStyleBase' )
155- this . _playlistStatusCache . showStyleBaseExt = showStyleBase
156- this . throttledSendStatusToAll ( )
157+
158+ this . updateAndNotify ( {
159+ showStyleBaseExt : showStyleBase ,
160+ } )
157161 }
158162
159163 private onSegmentUpdate = ( segment : Segment | undefined ) : void => {
160164 this . logUpdateReceived ( 'segment' )
161- this . _playlistStatusCache . currentSegment = segment
162- this . throttledSendStatusToAll ( )
165+
166+ this . updateAndNotify ( {
167+ currentSegment : segment ,
168+ } )
163169 }
164170
165171 private onSegmentsUpdate = ( segments : DBSegment [ ] | undefined ) : void => {
166172 this . logUpdateReceived ( 'segments' )
167- this . _playlistStatusCache . segmentsById = segments ? normalizeArray ( segments , '_id' ) : { }
168- this . throttledSendStatusToAll ( ) // TODO: can this be smarter?
173+ this . updateAndNotify ( {
174+ segmentsById : segments ? normalizeArray ( segments , '_id' ) : { } ,
175+ } )
176+ }
177+
178+ private updateAndNotify ( newCacheContent : Partial < ExtendedPlaylistStatusCache > ) {
179+ const updatedCacheContent = { ...this . _playlistStatusCache , ...newCacheContent }
180+ const hasAnythingChanged = ! _ . isEqual ( this . _playlistStatusCache , updatedCacheContent )
181+ if ( hasAnythingChanged ) {
182+ this . _playlistStatusCache = updatedCacheContent
183+ this . throttledSendStatusToAll ( )
184+ }
169185 }
170186}
0 commit comments