export const Scanner = { scan ( node , callback ) { if ( node . nodeType === 1 ) { const list = Array . from ( node . querySelectorAll ( '[data-component]' ) ) ; const elements = node . dataset . component ? [ node ] . concat ( list ) : list ; if ( elements . length ) { elements . reverse ( ) . forEach ( callback ) ; } } } , observe ( target , onAdd , onRemove ) { const observer = new MutationObserver ( mutations => mutations . forEach ( mutation => { if ( mutation . type === 'childList' ) { if ( mutation . addedNodes . length ) { Array . from ( mutation . addedNodes ) . forEach ( node => Scanner . scan ( node , onAdd ) ) ; } else if ( mutation . removedNodes . length ) { Array . from ( mutation . removedNodes ) . forEach ( node => Scanner . scan ( node , onRemove ) ) ; } } } ) ) ; observer . observe ( target , { childList : true , subtree : true } ) ; } } ;