license import { AttrMutatorConfig } from "./types" ; import { createMap , truncateArray } from "./util" ; import { flush , queueChange } from "./changes" ; /**
 * Used to keep track of the previous values when a 2-way diff is necessary.
 * This object is cleared out and reused.
 */ const prevValuesMap = createMap ( ) ; param prev param next param updateCtx param updateFn param attrs param alwaysDiffAttributes function calculateDiff < T > ( prev , next , updateCtx , updateFn , attrs , alwaysDiffAttributes = false ) { const isNew = ! prev . length || alwaysDiffAttributes ; let i = 0 ; for ( ; i < next . length ; i += 2 ) { const name = next [ i ] ; if ( isNew ) { prev [ i ] = name ; } else if ( prev [ i ] !== name ) { break ; } const value = next [ i + 1 ] ; if ( isNew || prev [ i + 1 ] !== value ) { prev [ i + 1 ] = value ; queueChange ( updateFn , updateCtx , name , value , attrs ) ; } } if ( i < next . length || i < prev . length ) { const startIndex = i ; for ( i = startIndex ; i < prev . length ; i += 2 ) { prevValuesMap [ prev [ i ] ] = prev [ i + 1 ] ; } for ( i = startIndex ; i < next . length ; i += 2 ) { const name = next [ i ] as string ; const value = next [ i + 1 ] ; if ( prevValuesMap [ name ] !== value ) { queueChange ( updateFn , updateCtx , name , value , attrs ) ; } prev [ i ] = name ; prev [ i + 1 ] = value ; delete prevValuesMap [ name ] ; } truncateArray ( prev , next . length ) ; for ( const name in prevValuesMap ) { queueChange ( updateFn , updateCtx , name , undefined , attrs ) ; delete prevValuesMap [ name ] ; } } flush ( ) ; } export { calculateDiff } ;