1+ import { assert } from "shared/assert" ;
12import { preciseFloat } from "./helpers" ;
23import { Events } from "./types" ;
34import type {
45 AudioTrack ,
6+ Interstitial ,
57 Playhead ,
68 Quality ,
79 SubtitleTrack ,
@@ -16,7 +18,8 @@ interface Timing {
1618
1719interface StateParams {
1820 onEvent ( event : Events ) : void ;
19- getTiming ( ) : undefined | Timing ;
21+ getTiming ( ) : undefined | Timing | null ;
22+ getInterstitialTiming ( ) : undefined | Omit < Timing , "seekableStart" > | null ;
2023}
2124
2225interface StateProperties {
@@ -34,6 +37,7 @@ interface StateProperties {
3437 seeking : boolean ;
3538 live : boolean ;
3639 timeline : TimelineItem [ ] ;
40+ interstitial : Interstitial | null ;
3741}
3842
3943const noState : StateProperties = {
@@ -51,6 +55,7 @@ const noState: StateProperties = {
5155 seeking : false ,
5256 live : false ,
5357 timeline : [ ] ,
58+ interstitial : null ,
5459} ;
5560
5661export class State implements StateProperties {
@@ -121,7 +126,8 @@ export class State implements StateProperties {
121126
122127 if (
123128 // TODO: Come up with a generic logical check.
124- ( ! this . subtitleTracks . length && subtitleTracks . length ) ||
129+ ! this . subtitleTracks . length ||
130+ subtitleTracks . length ||
125131 diff ( this . subtitleTracks ) !== diff ( subtitleTracks )
126132 ) {
127133 this . subtitleTracks = subtitleTracks ;
@@ -151,34 +157,73 @@ export class State implements StateProperties {
151157 this . params_ . onEvent ( Events . TIMELINE_CHANGE ) ;
152158 }
153159
154- requestTimingSync ( ) {
160+ setInterstitial (
161+ interstitial : Omit < Interstitial , "currentTime" | "duration" > | null ,
162+ ) {
163+ if ( interstitial ) {
164+ this . setSeeking ( false ) ;
165+ }
166+
167+ this . interstitial = interstitial
168+ ? {
169+ ...interstitial ,
170+ currentTime : 0 ,
171+ duration : NaN ,
172+ }
173+ : null ;
174+
175+ this . requestTimingSync ( /* skipEvent= */ true ) ;
176+
177+ this . params_ . onEvent ( Events . INTERSTITIAL_CHANGE ) ;
178+ }
179+
180+ requestTimingSync ( skipEvent ?: boolean ) {
155181 clearTimeout ( this . timerId_ ) ;
156182 this . timerId_ = window . setTimeout ( ( ) => {
157183 this . requestTimingSync ( ) ;
158184 } , 250 ) ;
185+ let emitEvent = false ;
159186
160187 const timing = this . params_ . getTiming ( ) ;
161- if ( ! timing ) {
162- return ;
188+ if ( timing ) {
189+ const currentTime = preciseFloat ( timing . currentTime ) ;
190+ const duration = preciseFloat ( timing . duration ) ;
191+ const seekableStart = preciseFloat ( timing . seekableStart ) ;
192+
193+ if (
194+ currentTime !== this . currentTime ||
195+ duration !== this . duration ||
196+ seekableStart !== this . seekableStart
197+ ) {
198+ emitEvent = true ;
199+ }
200+
201+ this . currentTime = currentTime ;
202+ this . duration = duration ;
203+ this . seekableStart = seekableStart ;
163204 }
164205
165- const currentTime = preciseFloat ( timing . currentTime ) ;
166- const duration = preciseFloat ( timing . duration ) ;
167- const seekableStart = preciseFloat ( timing . seekableStart ) ;
206+ const interstitialTiming = this . params_ . getInterstitialTiming ( ) ;
207+ if ( interstitialTiming ) {
208+ assert ( this . interstitial ) ;
168209
169- if (
170- currentTime === this . currentTime &&
171- duration === this . duration &&
172- seekableStart === this . seekableStart
173- ) {
174- return ;
175- }
210+ const currentTime = preciseFloat ( interstitialTiming . currentTime ) ;
211+ const duration = preciseFloat ( interstitialTiming . duration ) ;
176212
177- this . currentTime = currentTime ;
178- this . duration = duration ;
179- this . seekableStart = seekableStart ;
213+ if (
214+ currentTime !== this . interstitial . currentTime ||
215+ duration !== this . interstitial . duration
216+ ) {
217+ emitEvent = true ;
218+ }
180219
181- this . params_ . onEvent ( Events . TIME_CHANGE ) ;
220+ this . interstitial . currentTime = currentTime ;
221+ this . interstitial . duration = duration ;
222+ }
223+
224+ if ( ! skipEvent && emitEvent ) {
225+ this . params_ . onEvent ( Events . TIME_CHANGE ) ;
226+ }
182227 }
183228
184229 ready = noState . ready ;
@@ -195,6 +240,7 @@ export class State implements StateProperties {
195240 seeking = noState . seeking ;
196241 live = noState . live ;
197242 timeline = noState . timeline ;
243+ interstitial = noState . interstitial ;
198244}
199245
200246export function getState < N extends keyof StateProperties > (
0 commit comments