import { IFiber , IRef } from './type' ; import { updateElement } from './dom' ; import { isFn , LANE } from './reconcile' ; export const commit = ( fiber ) : void => { let d = fiber ; let e = d . e ; fiber . e = null ; do { insert ( e ) ; } while ( ( e = e . e ) ) ; while ( ( d = d . d ) ) remove ( d ) ; fiber . d = null ; } ; const insert = ( fiber ) : void => { if ( fiber . lane & LANE . UPDATE ) { updateElement ( fiber . node , fiber . oldProps || { } , fiber . props ) ; } if ( fiber . lane & LANE . INSERT ) { fiber . parentNode . insertBefore ( fiber . node , fiber . after ) ; } fiber . parentNode [ 'prev' ] = fiber . node [ 'prev' ] = null ; refer ( fiber . ref , fiber . node ) ; } ; const refer = ( ref , dom ? ) : void => { if ( ref ) isFn ( ref ) ? ref ( dom ) : ( ( ref as { current ? : HTMLElement ; } ) ! . current = dom ) ; } ; const kidsRefer = ( kids ) : void => { kids . forEach ( kid => { kid . kids && kidsRefer ( kid . kids ) ; refer ( kid . ref , null ) ; } ) ; } ; const remove = d => { if ( d . isComp ) { if ( d . lane & LANE . REMOVE ) { d . hooks && d . hooks . list . forEach ( e => e [ 2 ] && e [ 2 ] ( ) ) ; } d . kids . forEach ( remove ) ; } else { kidsRefer ( d . kids ) ; d . parentNode . removeChild ( d . node ) ; refer ( d . ref , null ) ; } } ;