@@ -13,6 +13,8 @@ export interface SourceOverrideOptions {
1313 options : IKPlayerOptions ;
1414 /** Callback to get the current source */
1515 getCurrentSource : ( ) => SourceOptions | null ;
16+ /** Callback to get the original current source */
17+ getOriginalCurrentSource : ( ) => SourceOptions | null ;
1618 /** Callback to update the current source */
1719 onSourceUpdate : ( source : SourceOptions ) => void ;
1820 /** Callback to update the original source */
@@ -161,33 +163,41 @@ export function createSourceOverride(
161163 player : Player ,
162164 overrideOptions : SourceOverrideOptions
163165) : any {
164- const { options, getCurrentSource, onSourceUpdate, onOriginalSourceUpdate, hasPreparedSrc } = overrideOptions ;
166+ const { options, getCurrentSource, getOriginalCurrentSource , onSourceUpdate, onOriginalSourceUpdate, hasPreparedSrc } = overrideOptions ;
165167 const nativeSrc = player . src . bind ( player ) ;
166168 let srcCallVersion = 0 ;
167169
168170 return ( source ?: SourceOptions ) => {
169171 if ( source === undefined ) {
170- return getCurrentSource ( ) ;
172+ return getOriginalCurrentSource ( ) ;
171173 }
172174
173175 validateSourceOptions ( source ) ;
174176
177+ // Clone the source immediately after validation to prevent mutating the original
178+ const sourceClone = { ...source } ;
179+ onOriginalSourceUpdate ( { ...sourceClone } ) ;
180+ onSourceUpdate ( { ...sourceClone } ) ;
181+
175182 const myCallId = ++ srcCallVersion ;
176183
177184 showLoadingState ( player ) ;
178185
179- onOriginalSourceUpdate ( { ...source } ) ;
186+ const currentSource = getCurrentSource ( ) ;
187+ if ( ! currentSource ) {
188+ return ;
189+ }
180190
181- prepareSourceIfNeeded ( source , options )
191+ prepareSourceIfNeeded ( currentSource , options )
182192 . then ( async ( prepared : SourceOptions ) => {
183193 if ( myCallId !== srcCallVersion ) {
184194 return ;
185195 }
186196
187197 const { maxTries, videoTimeoutInMS, delayInMS } = options ;
188198
189- if ( ! hasPreparedSrc ( source ) ) {
190- ensurePrepared ( source as AugmentedSourceOptions ) . src = prepared . src ;
199+ if ( ! hasPreparedSrc ( currentSource ) ) {
200+ ensurePrepared ( currentSource as AugmentedSourceOptions ) . src = prepared . src ;
191201 }
192202
193203 onSourceUpdate ( prepared ) ;
0 commit comments