1- import { PieceId , PieceInstanceId , RundownPlaylistActivationId } from '@sofie-automation/corelib/dist/dataModel/Ids'
1+ import {
2+ PartInstanceId ,
3+ PieceId ,
4+ PieceInstanceId ,
5+ RundownPlaylistActivationId ,
6+ } from '@sofie-automation/corelib/dist/dataModel/Ids'
27import { ReadonlyDeep } from 'type-fest'
38import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
49import {
@@ -39,9 +44,11 @@ interface PlayoutPieceInstanceModelSnapshotImpl {
3944 PieceInstance : PieceInstance
4045 HasChanges : boolean
4146}
47+
48+ const recueNextPartSnapshots = new Map < PartInstanceId , PlayoutPartInstanceModelSnapshot > ( )
49+
4250class PlayoutPartInstanceModelSnapshotImpl implements PlayoutPartInstanceModelSnapshot {
4351 readonly __isPlayoutPartInstanceModelBackup = true
44-
4552 isRestored = false
4653
4754 readonly partInstance : DBPartInstance
@@ -70,6 +77,21 @@ export class PlayoutPartInstanceModelImpl implements PlayoutPartInstanceModel {
7077 partInstanceImpl : DBPartInstance
7178 pieceInstancesImpl : Map < PieceInstanceId , PlayoutPieceInstanceModelImpl | null >
7279 quickLoopService : QuickLoopService
80+ #recueNextPartSnapshot: PlayoutPartInstanceModelSnapshot | undefined
81+
82+ get recueNextPartSnapshot ( ) : PlayoutPartInstanceModelSnapshot | undefined {
83+ return this . #recueNextPartSnapshot
84+ }
85+
86+ set recueNextPartSnapshot ( snapshot : PlayoutPartInstanceModelSnapshot | undefined ) {
87+ this . #recueNextPartSnapshot = snapshot
88+ const partInstanceId = this . partInstanceImpl ?. _id
89+ if ( partInstanceId ) {
90+ if ( snapshot ) recueNextPartSnapshots . set ( partInstanceId , snapshot )
91+ else recueNextPartSnapshots . delete ( partInstanceId )
92+ }
93+ this . #partInstanceHasChanges = true
94+ }
7395
7496 #setPartInstanceValue< T extends keyof DBPartInstance > ( key : T , newValue : DBPartInstance [ T ] ) : void {
7597 if ( newValue === undefined ) {
@@ -169,6 +191,8 @@ export class PlayoutPartInstanceModelImpl implements PlayoutPartInstanceModel {
169191 this . partInstanceImpl = partInstance
170192 this . #partInstanceHasChanges = hasChanges
171193 this . quickLoopService = quickLoopService
194+ const partInstanceId = partInstance ?. _id
195+ this . #recueNextPartSnapshot = partInstanceId ? recueNextPartSnapshots . get ( partInstanceId ) : undefined
172196
173197 this . pieceInstancesImpl = new Map ( )
174198 for ( const pieceInstance of pieceInstances ) {
@@ -188,29 +212,54 @@ export class PlayoutPartInstanceModelImpl implements PlayoutPartInstanceModel {
188212 return new PlayoutPartInstanceModelSnapshotImpl ( this )
189213 }
190214
215+ recueNextPart ( ) : void {
216+ if ( ! this . recueNextPartSnapshot ) {
217+ throw new Error ( 'Cannot recue next part when no snapshot is available' )
218+ }
219+
220+ this . snapshotRestore ( this . recueNextPartSnapshot )
221+ this . recueNextPartSnapshot = this . snapshotMakeCopy ( )
222+ }
223+
191224 snapshotRestore ( snapshot : PlayoutPartInstanceModelSnapshot ) : void {
192- if ( ! ( snapshot instanceof PlayoutPartInstanceModelSnapshotImpl ) )
225+ if ( ! snapshot . __isPlayoutPartInstanceModelBackup )
193226 throw new Error ( `Cannot restore a Snapshot from an different Model` )
194227
195- if ( snapshot . partInstance . _id !== this . partInstance . _id )
228+ const snapshotImpl = snapshot as PlayoutPartInstanceModelSnapshotImpl & {
229+ pieceInstances :
230+ | ReadonlyMap < PieceInstanceId , PlayoutPieceInstanceModelSnapshotImpl | null >
231+ | Record < string , PlayoutPieceInstanceModelSnapshotImpl | null >
232+ }
233+
234+ if ( snapshotImpl . partInstance . _id !== this . partInstance . _id )
196235 throw new Error ( `Cannot restore a Snapshot from an different PartInstance` )
197236
198- if ( snapshot . isRestored ) throw new Error ( `Cannot restore a Snapshot which has already been restored` )
199- snapshot . isRestored = true
237+ if ( snapshotImpl . isRestored ) throw new Error ( `Cannot restore a Snapshot which has already been restored` )
238+ snapshotImpl . isRestored = true
200239
201- this . partInstanceImpl = snapshot . partInstance
202- this . #partInstanceHasChanges = snapshot . partInstanceHasChanges
203- this . pieceInstancesImpl . clear ( )
204- for ( const [ pieceInstanceId , pieceInstance ] of snapshot . pieceInstances ) {
240+ this . partInstanceImpl = snapshotImpl . partInstance
241+ for ( const pieceInstanceId of this . pieceInstancesImpl . keys ( ) ) {
242+ this . pieceInstancesImpl . set ( pieceInstanceId , null )
243+ }
244+ const pieceInstancesEntries =
245+ snapshotImpl . pieceInstances instanceof Map
246+ ? snapshotImpl . pieceInstances . entries ( )
247+ : Object . entries < PlayoutPieceInstanceModelSnapshotImpl | null > (
248+ snapshotImpl . pieceInstances as Record < string , PlayoutPieceInstanceModelSnapshotImpl | null >
249+ )
250+
251+ for ( const [ pieceInstanceId , pieceInstance ] of pieceInstancesEntries ) {
205252 if ( pieceInstance ) {
206253 this . pieceInstancesImpl . set (
207- pieceInstanceId ,
254+ pieceInstanceId as PieceInstanceId ,
208255 new PlayoutPieceInstanceModelImpl ( pieceInstance . PieceInstance , pieceInstance . HasChanges )
209256 )
210257 } else {
211- this . pieceInstancesImpl . set ( pieceInstanceId , null )
258+ this . pieceInstancesImpl . set ( pieceInstanceId as PieceInstanceId , null )
212259 }
213260 }
261+
262+ this . #partInstanceHasChanges = true
214263 }
215264
216265 blockTakeUntil ( timestamp : Time | null ) : void {
0 commit comments