@@ -10,16 +10,43 @@ export function render(element, container) {
10
10
11
11
function reconcile ( parentDom , instance , element ) {
12
12
if ( instance == null ) {
13
+ // Create instance
13
14
const newInstance = instantiate ( element ) ;
14
15
parentDom . appendChild ( newInstance . dom ) ;
15
16
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 ;
16
27
} else {
28
+ // Replace instance
17
29
const newInstance = instantiate ( element ) ;
18
30
parentDom . replaceChild ( newInstance . dom , instance . dom ) ;
19
31
return newInstance ;
20
32
}
21
33
}
22
34
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
+
23
50
function instantiate ( element ) {
24
51
const { type, props } = element ;
25
52
0 commit comments