@@ -11,54 +11,66 @@ export interface LookaheadResult {
1111 future : Array < LookaheadTimelineObject >
1212}
1313
14+ export interface PartInstanceAndPieceInstancesInfos {
15+ previous ?: PartInstanceAndPieceInstances
16+ current ?: PartInstanceAndPieceInstances
17+ next ?: PartInstanceAndPieceInstances
18+ }
19+
1420export function findLookaheadForLayer (
1521 context : JobContext ,
16- currentPartInstanceId : PartInstanceId | null ,
17- partInstancesInfo : PartInstanceAndPieceInstances [ ] ,
18- previousPartInstanceInfo : PartInstanceAndPieceInstances | undefined ,
22+ partInstancesInfo : PartInstanceAndPieceInstancesInfos ,
1923 orderedPartInfos : Array < PartAndPieces > ,
2024 layer : string ,
2125 lookaheadTargetFutureObjects : number ,
22- lookaheadMaxSearchDistance : number
26+ lookaheadMaxSearchDistance : number ,
27+ nextTimeOffset ?: number | null
2328) : LookaheadResult {
2429 const span = context . startSpan ( `findLookaheadForlayer.${ layer } ` )
30+ const currentPartId = partInstancesInfo . current ?. part . _id ?? null
2531 const res : LookaheadResult = {
2632 timed : [ ] ,
2733 future : [ ] ,
2834 }
2935
3036 // Track the previous info for checking how the timeline will be built
3137 let previousPart : ReadonlyDeep < DBPart > | undefined
32- if ( previousPartInstanceInfo ) {
33- previousPart = previousPartInstanceInfo . part . part
38+ if ( partInstancesInfo . previous ?. part . part ) {
39+ previousPart = partInstancesInfo . previous . part . part
3440 }
3541
3642 // Generate timed/future objects for the partInstances
37- for ( const partInstanceInfo of partInstancesInfo ) {
38- if ( ! partInstanceInfo . onTimeline && lookaheadMaxSearchDistance <= 0 ) break
43+ if ( partInstancesInfo . current ) {
44+ const { objs : currentObjs , partInfo : currentPartInfo } = generatePartInstanceLookaheads (
45+ context ,
46+ partInstancesInfo . current ,
47+ partInstancesInfo . current . part . _id ,
48+ layer ,
49+ previousPart
50+ )
3951
40- const partInfo : PartAndPieces = {
41- part : partInstanceInfo . part . part ,
42- usesInTransition : partInstanceInfo . calculatedTimings . inTransitionStart !== null ,
43- pieces : sortPieceInstancesByStart ( partInstanceInfo . allPieces , partInstanceInfo . nowInPart ) ,
52+ if ( partInstancesInfo . current . onTimeline ) {
53+ res . timed . push ( ...currentObjs )
4454 }
55+ previousPart = currentPartInfo . part
56+ }
4557
46- const objs = findLookaheadObjectsForPart (
58+ // for Lookaheads in the next part we need to take the nextTimeOffset into account.
59+ // TODO: Check if having two pieces after eachother on the same layer is handled correctly
60+ if ( partInstancesInfo . next ) {
61+ const { objs : nextObjs , partInfo : nextPartInfo } = generatePartInstanceLookaheads (
4762 context ,
48- currentPartInstanceId ,
63+ partInstancesInfo . next ,
64+ currentPartId ,
4965 layer ,
5066 previousPart ,
51- partInfo ,
52- partInstanceInfo . part . _id
67+ nextTimeOffset
5368 )
5469
55- if ( partInstanceInfo . onTimeline ) {
56- res . timed . push ( ...objs )
57- } else {
58- res . future . push ( ...objs )
70+ if ( partInstancesInfo . next ?. onTimeline ) {
71+ res . future . push ( ...nextObjs )
5972 }
60-
61- previousPart = partInfo . part
73+ previousPart = nextPartInfo . part
6274 }
6375
6476 if ( lookaheadMaxSearchDistance > 1 && lookaheadTargetFutureObjects > 0 ) {
@@ -69,14 +81,7 @@ export function findLookaheadForLayer(
6981 }
7082
7183 if ( partInfo . pieces . length > 0 && isPartPlayable ( partInfo . part ) ) {
72- const objs = findLookaheadObjectsForPart (
73- context ,
74- currentPartInstanceId ,
75- layer ,
76- previousPart ,
77- partInfo ,
78- null
79- )
84+ const objs = findLookaheadObjectsForPart ( context , currentPartId , layer , previousPart , partInfo , null )
8085 res . future . push ( ...objs )
8186 previousPart = partInfo . part
8287 }
@@ -86,3 +91,28 @@ export function findLookaheadForLayer(
8691 if ( span ) span . end ( )
8792 return res
8893}
94+ function generatePartInstanceLookaheads (
95+ context : JobContext ,
96+ partInstanceInfo : PartInstanceAndPieceInstances ,
97+ currentPartInstanceId : PartInstanceId | null ,
98+ layer : string ,
99+ previousPart : ReadonlyDeep < DBPart > | undefined ,
100+ nextTimeOffset ?: number | null
101+ ) : { objs : LookaheadTimelineObject [ ] ; partInfo : PartAndPieces } {
102+ const partInfo : PartAndPieces = {
103+ part : partInstanceInfo . part . part ,
104+ usesInTransition : partInstanceInfo . calculatedTimings . inTransitionStart !== null ,
105+ pieces : sortPieceInstancesByStart ( partInstanceInfo . allPieces , partInstanceInfo . nowInPart ) ,
106+ }
107+
108+ const objs = findLookaheadObjectsForPart (
109+ context ,
110+ currentPartInstanceId ,
111+ layer ,
112+ previousPart ,
113+ partInfo ,
114+ partInstanceInfo . part . _id ,
115+ nextTimeOffset ?? undefined
116+ )
117+ return { objs, partInfo }
118+ }
0 commit comments