11import { BakeQueue , nextBake } from "@canvas/players/bake-queue" ;
22
3- const at = ( start : number , end : number , playhead : ( ) => number = ( ) => 0 ) => ( { start : ( ) => start , end : ( ) => end , playhead } ) ;
3+ const at = ( next : number , playhead : ( ) => number = ( ) => 0 ) => ( { nextFrameEnd : ( ) => next , playhead } ) ;
44
55const settle = ( ) =>
66 new Promise ( resolve => {
77 setTimeout ( resolve , 0 ) ;
88 } ) ;
99
1010describe ( "nextBake" , ( ) => {
11- it ( "takes the clip under the playhead, then those ahead in start order , then those it has passed" , ( ) => {
11+ it ( "takes the earliest next frame still ahead of the playhead , then frames it has passed" , ( ) => {
1212 const playhead = ( ) => 30 ;
13- const waiting = [ at ( 54.4 , 80.37 , playhead ) , at ( 0 , 24.4 , playhead ) , at ( 24.4 , 54.4 , playhead ) , at ( 40 , 45 , playhead ) ] ;
13+ const waiting = [ at ( 54.4 , playhead ) , at ( 24.4 , playhead ) , at ( 30.1 , playhead ) , at ( 40 , playhead ) ] ;
1414 const order : number [ ] = [ ] ;
15- while ( waiting . length > 0 ) order . push ( waiting . splice ( nextBake ( waiting ) , 1 ) [ 0 ] . start ( ) ) ;
15+ while ( waiting . length > 0 ) order . push ( waiting . splice ( nextBake ( waiting ) , 1 ) [ 0 ] . nextFrameEnd ( ) ) ;
1616
17- expect ( order ) . toEqual ( [ 24.4 , 40 , 54.4 , 0 ] ) ;
17+ expect ( order ) . toEqual ( [ 30.1 , 40 , 54.4 , 24.4 ] ) ;
1818 } ) ;
1919
20- it ( "keeps arrival order between clips that start together" , ( ) => {
21- expect ( nextBake ( [ at ( 10 , 20 ) , at ( 10 , 15 ) ] ) ) . toBe ( 0 ) ;
20+ it ( "counts a frame that leaves the screen as the playhead reaches it as passed" , ( ) => {
21+ expect ( nextBake ( [ at ( 30 , ( ) => 30 ) , at ( 31 , ( ) => 30 ) ] ) ) . toBe ( 1 ) ;
22+ } ) ;
23+
24+ it ( "keeps arrival order between bakes whose next frames coincide" , ( ) => {
25+ expect ( nextBake ( [ at ( 10 ) , at ( 10 ) ] ) ) . toBe ( 0 ) ;
2226 } ) ;
2327} ) ;
2428
2529describe ( "BakeQueue" , ( ) => {
2630 it ( "bakes clips that arrive together from the playhead, whatever their arrival order" , async ( ) => {
2731 const queue = new BakeQueue ( ) ;
2832 const ran : number [ ] = [ ] ;
29- await Promise . all ( [ 54.4 , 24.4 , 0 ] . map ( start => queue . add ( at ( start , start + 20 ) , async ( ) => ran . push ( start ) ) ) ) ;
33+ await Promise . all ( [ 54.4 , 24.4 , 0.1 ] . map ( next => queue . add ( at ( next ) , async ( ) => ran . push ( next ) ) ) ) ;
3034
31- expect ( ran ) . toEqual ( [ 0 , 24.4 , 54.4 ] ) ;
35+ expect ( ran ) . toEqual ( [ 0.1 , 24.4 , 54.4 ] ) ;
3236 } ) ;
3337
34- it ( "runs one bake at a time" , async ( ) => {
38+ it ( "runs one bake at a time, between turns too " , async ( ) => {
3539 const queue = new BakeQueue ( ) ;
3640 let running = 0 ;
3741 let most = 0 ;
38- const bake = async ( ) => {
42+ const bake = async ( turn : ( ) => Promise < void > ) => {
43+ running += 1 ;
44+ most = Math . max ( most , running ) ;
45+ await settle ( ) ;
46+ running -= 1 ;
47+ await turn ( ) ;
3948 running += 1 ;
4049 most = Math . max ( most , running ) ;
4150 await settle ( ) ;
4251 running -= 1 ;
4352 } ;
44- await Promise . all ( [ 0 , 10 , 20 ] . map ( start => queue . add ( at ( start , start + 10 ) , bake ) ) ) ;
53+ await Promise . all ( [ 1 , 11 , 21 ] . map ( next => queue . add ( at ( next ) , bake ) ) ) ;
4554
4655 expect ( most ) . toBe ( 1 ) ;
4756 } ) ;
4857
58+ it ( "alternates bakes whose frames overlap, turn by turn" , async ( ) => {
59+ const queue = new BakeQueue ( ) ;
60+ const ran : string [ ] = [ ] ;
61+ const bake = ( name : string ) => {
62+ let next = 1 ;
63+ return queue . add ( { nextFrameEnd : ( ) => next , playhead : ( ) => 0 } , async turn => {
64+ const step = async ( ) => {
65+ ran . push ( name ) ;
66+ next += 1 ;
67+ await turn ( ) ;
68+ } ;
69+ await step ( ) ;
70+ await step ( ) ;
71+ await step ( ) ;
72+ } ) ;
73+ } ;
74+ await Promise . all ( [ bake ( "A" ) , bake ( "B" ) ] ) ;
75+
76+ expect ( ran ) . toEqual ( [ "A" , "B" , "A" , "B" , "A" , "B" ] ) ;
77+ } ) ;
78+
4979 it ( "re-sorts what's waiting when the playhead moves during a bake" , async ( ) => {
5080 const queue = new BakeQueue ( ) ;
5181 let playhead = 0 ;
5282 const ran : string [ ] = [ ] ;
5383 let finishFirst = ( ) => { } ;
5484 const first = queue . add (
55- at ( 0 , 10 , ( ) => playhead ) ,
85+ at ( 0.1 , ( ) => playhead ) ,
5686 ( ) => {
5787 ran . push ( "A" ) ;
5888 return new Promise < void > ( resolve => {
@@ -63,11 +93,11 @@ describe("BakeQueue", () => {
6393 await settle ( ) ;
6494 const rest = [
6595 queue . add (
66- at ( 10 , 20 , ( ) => playhead ) ,
96+ at ( 10.1 , ( ) => playhead ) ,
6797 async ( ) => ran . push ( "B" )
6898 ) ,
6999 queue . add (
70- at ( 20 , 30 , ( ) => playhead ) ,
100+ at ( 25.1 , ( ) => playhead ) ,
71101 async ( ) => ran . push ( "C" )
72102 )
73103 ] ;
@@ -80,10 +110,10 @@ describe("BakeQueue", () => {
80110
81111 it ( "moves on after a bake fails, and hands the failure to its caller" , async ( ) => {
82112 const queue = new BakeQueue ( ) ;
83- const failed = queue . add ( at ( 0 , 10 ) , async ( ) => {
113+ const failed = queue . add ( at ( 0.1 ) , async ( ) => {
84114 throw new Error ( "bake failed" ) ;
85115 } ) ;
86- const next = queue . add ( at ( 10 , 20 ) , async ( ) => "baked" ) ;
116+ const next = queue . add ( at ( 10.1 ) , async ( ) => "baked" ) ;
87117
88118 await expect ( failed ) . rejects . toThrow ( "bake failed" ) ;
89119 await expect ( next ) . resolves . toBe ( "baked" ) ;
0 commit comments