Skip to content

Commit bf48f17

Browse files
authored
fix: use same piece timing as timeline in LSG SOFIE-3305 (#1219)
1 parent cf9fa15 commit bf48f17

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

packages/live-status-gateway/src/collections/pieceInstancesHandler.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import throttleToNextTick from '@sofie-automation/shared-lib/dist/lib/throttleTo
1010
import _ = require('underscore')
1111
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
1212
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'
1417
import { ShowStyleBaseExt, ShowStyleBaseHandler } from './showStyleBaseHandler'
1518
import { PlaylistHandler } from './playlistHandler'
1619
import { SourceLayers } from '@sofie-automation/corelib/dist/dataModel/ShowStyleBase'
@@ -44,8 +47,7 @@ export class PieceInstancesHandler
4447
private _partInstances: SelectedPartInstances | undefined
4548

4649
private _throttledUpdateAndNotify = throttleToNextTick(() => {
47-
this.updateCollectionData()
48-
this.notify(this._collectionData).catch(this._logger.error)
50+
this.updateAndNotify().catch(this._logger.error)
4951
})
5052

5153
constructor(logger: Logger, coreHandler: CoreHandler) {
@@ -72,13 +74,33 @@ export class PieceInstancesHandler
7274

7375
private processAndPrunePieceInstanceTimings(
7476
partInstance: DBPartInstance | undefined,
75-
pieceInstances: PieceInstance[]
77+
pieceInstances: PieceInstance[],
78+
filterActive: boolean
7679
): ReadonlyDeep<PieceInstance>[] {
7780
// Approximate when 'now' is in the PartInstance, so that any adlibbed Pieces will be timed roughly correctly
7881
const partStarted = partInstance?.timings?.plannedStartedPlayback
7982
const nowInPart = partStarted === undefined ? 0 : Date.now() - partStarted
8083

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+
})
82104
}
83105

84106
private updateCollectionData(): boolean {
@@ -89,36 +111,37 @@ export class PieceInstancesHandler
89111
const inPreviousPartInstance = this._currentPlaylist?.previousPartInfo?.partInstanceId
90112
? this.processAndPrunePieceInstanceTimings(
91113
this._partInstances?.previous,
92-
collection.find({ partInstanceId: this._currentPlaylist.previousPartInfo.partInstanceId })
114+
collection.find({ partInstanceId: this._currentPlaylist.previousPartInfo.partInstanceId }),
115+
true
93116
)
94117
: []
95118
const inCurrentPartInstance = this._currentPlaylist?.currentPartInfo?.partInstanceId
96119
? this.processAndPrunePieceInstanceTimings(
97120
this._partInstances?.current,
98-
collection.find({ partInstanceId: this._currentPlaylist.currentPartInfo.partInstanceId })
121+
collection.find({ partInstanceId: this._currentPlaylist.currentPartInfo.partInstanceId }),
122+
true
99123
)
100124
: []
101125
const inNextPartInstance = this._currentPlaylist?.nextPartInfo?.partInstanceId
102126
? this.processAndPrunePieceInstanceTimings(
103127
undefined,
104-
collection.find({ partInstanceId: this._currentPlaylist.nextPartInfo.partInstanceId })
128+
collection.find({ partInstanceId: this._currentPlaylist.nextPartInfo.partInstanceId }),
129+
false
105130
)
106131
: []
107132

108-
const active = [...inPreviousPartInstance, ...inCurrentPartInstance].filter((pieceInstance) =>
109-
this.isPieceInstanceActive(pieceInstance)
110-
)
133+
const active = [...inPreviousPartInstance, ...inCurrentPartInstance]
111134

112135
let hasAnythingChanged = false
113-
if (!areElementsShallowEqual(this._collectionData.active, active)) {
136+
if (!_.isEqual(this._collectionData.active, active)) {
114137
this._collectionData.active = active
115138
hasAnythingChanged = true
116139
}
117140
if (
118-
!areElementsShallowEqual(this._collectionData.currentPartInstance, inCurrentPartInstance) &&
141+
!_.isEqual(this._collectionData.currentPartInstance, inCurrentPartInstance) &&
119142
(this._collectionData.currentPartInstance.length !== inCurrentPartInstance.length ||
120143
this._collectionData.currentPartInstance.some((pieceInstance, index) => {
121-
return !arePropertiesShallowEqual<ReadonlyDeep<PieceInstance>>(
144+
return !arePropertiesDeepEqual<ReadonlyDeep<PieceInstance>>(
122145
inCurrentPartInstance[index],
123146
pieceInstance,
124147
['reportedStartedPlayback', 'reportedStoppedPlayback']
@@ -128,7 +151,7 @@ export class PieceInstancesHandler
128151
this._collectionData.currentPartInstance = inCurrentPartInstance
129152
hasAnythingChanged = true
130153
}
131-
if (!areElementsShallowEqual(this._collectionData.nextPartInstance, inNextPartInstance)) {
154+
if (!_.isEqual(this._collectionData.nextPartInstance, inNextPartInstance)) {
132155
this._collectionData.nextPartInstance = inNextPartInstance
133156
hasAnythingChanged = true
134157
}
@@ -235,28 +258,9 @@ export class PieceInstancesHandler
235258
await this.notify(this._collectionData)
236259
}
237260
}
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-
}
253261
}
254262

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 {
260264
if (typeof a !== 'object' || a == null || typeof b !== 'object' || b == null) {
261265
return false
262266
}
@@ -267,7 +271,7 @@ export function arePropertiesShallowEqual<T extends Record<string, any>>(
267271
if (keysA.length !== keysB.length) return false
268272

269273
for (const key of keysA) {
270-
if (!keysB.includes(key) || a[key] !== b[key]) {
274+
if (!keysB.includes(key) || !_.isEqual(a[key], b[key])) {
271275
return false
272276
}
273277
}

0 commit comments

Comments
 (0)