@@ -10,16 +10,43 @@ export function render(element, container) {
1010
1111function reconcile ( parentDom , instance , element ) {
1212 if ( instance == null ) {
13+ // Create instance
1314 const newInstance = instantiate ( element ) ;
1415 parentDom . appendChild ( newInstance . dom ) ;
1516 return newInstance ;
17+ } else if ( element == null ) {
18+ // Remove instance
19+ parentDom . removeChild ( instance . dom ) ;
20+ return null ;
21+ } else if ( instance . element . type === element . type ) {
22+ // Update instance
23+ updateDomProperties ( instance . dom , instance . element . props , element . props ) ;
24+ instance . childInstances = reconcileChildren ( instance , element ) ;
25+ instance . element = element ;
26+ return instance ;
1627 } else {
28+ // Replace instance
1729 const newInstance = instantiate ( element ) ;
1830 parentDom . replaceChild ( newInstance . dom , instance . dom ) ;
1931 return newInstance ;
2032 }
2133}
2234
35+ function reconcileChildren ( instance , element ) {
36+ const dom = instance . dom ;
37+ const childInstances = instance . childInstances ;
38+ const nextChildElements = element . props . children || [ ] ;
39+ const newChildInstances = [ ] ;
40+ const count = Math . max ( childInstances . length , nextChildElements . length ) ;
41+ for ( let i = 0 ; i < count ; i ++ ) {
42+ const childInstance = childInstances [ i ] ;
43+ const childElement = nextChildElements [ i ] ;
44+ const newChildInstance = reconcile ( dom , childInstance , childElement ) ;
45+ newChildInstances . push ( newChildInstance ) ;
46+ }
47+ return newChildInstances . filter ( instance => instance != null ) ;
48+ }
49+
2350function instantiate ( element ) {
2451 const { type, props } = element ;
2552
0 commit comments