/**
 * Use this to create a "patch" object in the worker thread.
 * patch = diff(newObject, oldObject);
 */ export function diff ( obj , old ) { if ( typeof obj === 'object' ) { const isArray = Array . isArray ( obj ) ; if ( ! old || typeof old !== 'object' || ( isArray !== Array . isArray ( old ) ) ) return obj ; if ( isArray ) { let out ; let i = 0 ; const max = Math . min ( obj . length , old . length ) ; for ( ; i < max ; i ++ ) { const differs = different ( obj [ i ] , old [ i ] ) ; if ( differs ) break ; } const useArray = old . length === 0 ; const offset = obj . length - old . length ; for ( let j = obj . length ; j -- > i ; ) { const oldJ = j - offset ; if ( oldJ >= 0 ) { const differs = different ( obj [ j ] , old [ oldJ ] ) ; if ( differs ) { if ( ! out ) out = useArray ? [ ] : { } ; out [ j ] = diff ( obj [ j ] , old [ oldJ ] ) ; } } else { if ( ! out ) out = useArray ? [ ] : { } ; out [ j ] = obj [ j ] ; } } return out ; } let out ; for ( const key in obj ) { if ( ! ( key in old ) || obj [ key ] !== old [ key ] ) { if ( ! out ) out = { } ; const r = diff ( obj [ key ] , old [ key ] ) ; if ( r !== undefined ) out [ key ] = r ; } } for ( const key in old ) { if ( obj == null || ! ( key in obj ) ) { if ( ! out ) out = { } ; out [ key ] = undefined ; } } return out ; } else if ( obj !== old ) { return obj ; } } function different ( obj , old ) { for ( const key in obj ) { if ( old == null || ! ( key in old ) || obj [ key ] !== old [ key ] ) return true ; } for ( const key in old ) { if ( obj == null || ! ( key in obj ) ) return true ; } return false ; }