@@ -14,12 +14,13 @@ test("coalesces seeks to the latest pending position", async () => {
1414 const controller = new SeekController ( ) ;
1515 const first = deferred ( ) ;
1616 const positions : number [ ] = [ ] ;
17- const running = controller . seek ( 10 , async ( position ) => {
17+ const execute = async ( position : number ) => {
1818 positions . push ( position ) ;
1919 if ( position === 10 ) await first . promise ;
20- } ) ;
21- controller . seek ( 20 , async ( ) => undefined ) ;
22- controller . seek ( 30 , async ( ) => undefined ) ;
20+ } ;
21+ const running = controller . seek ( 10 , "10" , execute , ( ) => undefined ) ;
22+ controller . seek ( 20 , "20" , execute , ( ) => undefined ) ;
23+ controller . seek ( 30 , "30" , execute , ( ) => undefined ) ;
2324 first . resolve ( ) ;
2425 await running ;
2526 expect ( positions ) . toEqual ( [ 10 , 30 ] ) ;
@@ -28,18 +29,72 @@ test("coalesces seeks to the latest pending position", async () => {
2829test ( "continues to the latest seek after an abort" , async ( ) => {
2930 const controller = new SeekController ( ) ;
3031 const positions : number [ ] = [ ] ;
31- const running = controller . seek ( 10 , async ( position ) => {
32+ const execute = async ( position : number ) => {
3233 positions . push ( position ) ;
3334 if ( position === 10 ) throw new DOMException ( "aborted" , "AbortError" ) ;
34- } ) ;
35- controller . seek ( 40 , async ( ) => undefined ) ;
35+ } ;
36+ const running = controller . seek ( 10 , "10" , execute , ( ) => undefined ) ;
37+ controller . seek ( 40 , "40" , execute , ( ) => undefined ) ;
3638 await running ;
3739 expect ( positions ) . toEqual ( [ 10 , 40 ] ) ;
3840} ) ;
3941
4042test ( "throws non-abort seek errors" , async ( ) => {
4143 const controller = new SeekController ( ) ;
4244 await expect (
43- controller . seek ( 10 , async ( ) => Promise . reject ( new Error ( "failed" ) ) ) ,
45+ controller . seek (
46+ 10 ,
47+ "10" ,
48+ async ( ) => Promise . reject ( new Error ( "failed" ) ) ,
49+ ( ) => undefined ,
50+ ) ,
4451 ) . rejects . toThrow ( "failed" ) ;
4552} ) ;
53+
54+ test ( "uses the latest pending executor" , async ( ) => {
55+ const controller = new SeekController ( ) ;
56+ const first = deferred ( ) ;
57+ const executions : string [ ] = [ ] ;
58+ const running = controller . seek (
59+ 10 ,
60+ "seek:10" ,
61+ async ( ) => {
62+ executions . push ( "seek" ) ;
63+ await first . promise ;
64+ } ,
65+ ( ) => undefined ,
66+ ) ;
67+ controller . seek (
68+ 20 ,
69+ "quality:20:299" ,
70+ async ( ) => {
71+ executions . push ( "quality" ) ;
72+ } ,
73+ ( ) => undefined ,
74+ ) ;
75+ first . resolve ( ) ;
76+ await running ;
77+ expect ( executions ) . toEqual ( [ "seek" , "quality" ] ) ;
78+ } ) ;
79+
80+ test ( "deduplicates the active request" , async ( ) => {
81+ const controller = new SeekController ( ) ;
82+ const first = deferred ( ) ;
83+ let executions = 0 ;
84+ let cancellations = 0 ;
85+ const execute = async ( ) => {
86+ executions += 1 ;
87+ await first . promise ;
88+ } ;
89+ const running = controller . seek ( 10 , "seek:10" , execute , ( ) => {
90+ cancellations += 1 ;
91+ } ) ;
92+ const duplicate = controller . seek ( 10 , "seek:10" , execute , ( ) => {
93+ cancellations += 1 ;
94+ } ) ;
95+ first . resolve ( ) ;
96+ await running ;
97+ expect ( duplicate ) . toBe ( running ) ;
98+ expect ( executions ) . toBe ( 1 ) ;
99+ expect ( cancellations ) . toBe ( 0 ) ;
100+ } ) ;
0 commit comments