@@ -11,8 +11,25 @@ function Aura(node, childNodes) {
1111 this . childNodes = childNodes ;
1212}
1313
14+ Aura . prototype . empty = function empty ( value ) {
15+ const node = this . node ;
16+ const childNodes = this . childNodes ;
17+ const pn = node . parentNode ;
18+ let length = childNodes . length ;
19+ if ( length ) {
20+ const remove = childNodes . splice ( 0 , length ) ;
21+ while ( length -- ) pn . removeChild ( asNode ( remove [ length ] ) ) ;
22+ }
23+ if ( value ) {
24+ childNodes . push ( value ) ;
25+ pn . insertBefore ( asNode ( value ) , node ) ;
26+ }
27+ } ;
28+
1429Aura . prototype . become = function become ( virtual ) {
30+ const node = this . node ;
1531 const live = this . childNodes ;
32+ const pn = node . parentNode ;
1633 const vlength = virtual . length ;
1734 let llength = live . length ;
1835 let l = 0 ;
@@ -22,53 +39,45 @@ Aura.prototype.become = function become(virtual) {
2239 const vv = virtual [ v ] ;
2340 const status = lv === vv ? 0 : ( live . indexOf ( vv ) < 0 ? 1 : - 1 ) ;
2441 if ( status < 0 ) {
25- this . splice ( l , 1 ) ;
42+ live . splice ( l , 1 ) ;
43+ pn . removeChild ( asNode ( lv ) ) ;
2644 llength -- ;
2745 } else if ( 0 < status ) {
28- this . splice ( l ++ , 0 , virtual [ v ++ ] ) ;
46+ live . splice ( l ++ , 0 , vv ) ;
47+ pn . insertBefore ( asNode ( vv ) , l < llength ? asNode ( live [ l ] ) : node ) ;
2948 llength ++ ;
49+ v ++ ;
3050 } else {
3151 l ++ ;
3252 v ++ ;
3353 }
3454 }
3555 if ( l < llength ) {
36- this . splice ( l , llength - l ) ;
56+ const remove = live . splice ( l , llength - l ) ;
57+ l = remove . length ;
58+ while ( l -- ) pn . removeChild ( asNode ( remove [ l ] ) ) ;
3759 }
3860 if ( v < vlength ) {
39- this . splice . apply ( this , [ llength , 0 ] . concat ( virtual . slice ( v ) ) ) ;
40- }
41- } ;
42-
43- // the splice is in charge of removing or adding nodes
44- Aura . prototype . splice = function splice ( start , end ) {
45- const values = new Map ;
46- const ph = this . node ;
47- const cn = this . childNodes ;
48- const target = get ( values , cn [ start + ( end || 0 ) ] || ph ) ;
49- const pn = ph . parentNode ;
50- const result = cn . splice . apply ( cn , arguments ) ;
51- const reLength = result . length ;
52- for ( let i = 0 ; i < reLength ; i ++ ) {
53- pn . removeChild ( get ( values , result [ i ] ) ) ;
54- }
55- const arLength = arguments . length ;
56- if ( 3 === arLength ) {
57- pn . insertBefore ( get ( values , arguments [ 2 ] ) , target ) ;
58- } else if ( 2 < arLength ) {
59- const tmp = fragment ( pn ) ;
60- for ( let i = 2 ; i < arLength ; i ++ ) {
61- tmp . appendChild ( get ( values , arguments [ i ] ) ) ;
61+ const append = virtual . slice ( v ) ;
62+ l = 0 ;
63+ llength = append . length ;
64+ if ( llength === 1 ) {
65+ pn . insertBefore ( asNode ( append [ l ] ) , node ) ;
66+ } else {
67+ const tmp = fragment ( pn ) ;
68+ while ( l < llength )
69+ tmp . appendChild ( asNode ( append [ l ++ ] ) ) ;
70+ pn . insertBefore ( tmp , node ) ;
6271 }
63- pn . insertBefore ( tmp , target ) ;
72+ live . push . apply ( live , append ) ;
6473 }
65- return result ;
6674} ;
6775
6876// an item could be an hyperHTML.Component and, in such case,
6977// it should be rendered as node
7078const asNode = node => node instanceof Component ? node . render ( ) : node ;
7179
80+ /* TODO: benchmark this is needed at all
7281// instead of checking instanceof each time and render potentially twice
7382// use a map to retrieve nodes from a generic item
7483const get = (map, node) => map.get(node) || set(map, node);
@@ -77,5 +86,6 @@ const set = (map, node) => {
7786 map.set(node, value);
7887 return value;
7988};
89+ */
8090
8191Object . defineProperty ( exports , '__esModule' , { value : true } ) . default = Aura ;
0 commit comments