11// @flow
22
3- import type { Node , ComponentType } from 'react'
3+ import { type Node , type ComponentType , createElement } from 'react'
44
55import {
66 typeOf ,
@@ -75,7 +75,7 @@ const render = (
7575 props : DefaultProps ,
7676 queue : Frame [ ] ,
7777 visitor : Visitor ,
78- element ? : UserElement
78+ element : UserElement
7979) => {
8080 return shouldConstruct ( type )
8181 ? mountClassComponent ( type , props , queue , visitor , element )
@@ -134,7 +134,8 @@ export const visitElement = (
134134 case REACT_MEMO_TYPE: {
135135 const memoElement = ( ( element : any ) : MemoElement )
136136 const type = memoElement . type . type
137- const child = render ( type , memoElement . props , queue , visitor )
137+ const fauxElement = ( createElement ( ( type : any ) , memoElement . props ) : any )
138+ const child = render ( type , memoElement . props , queue , visitor , fauxElement )
138139 return getChildrenArray ( child )
139140 }
140141
@@ -143,15 +144,14 @@ export const visitElement = (
143144
144145 // If we find a StyledComponent, we trigger a specific optimisation
145146 // that allows quick rendering of them without computing styles
146- let child = null
147147 if ( isStyledElement ( refElement ) ) {
148- child = mountStyledComponent ( refElement )
149- } else {
150- const { render, defaultProps } = refElement . type
151- const props = computeProps ( refElement . props , defaultProps )
152- child = mountFunctionComponent ( render , props , queue , visitor )
148+ return mountStyledComponent ( refElement )
153149 }
154150
151+ const { render : type , defaultProps } = refElement . type
152+ const props = computeProps ( refElement . props , defaultProps )
153+ const fauxElement = ( createElement ( ( render : any ) , props ) : any )
154+ const child = render ( type , props , queue , visitor , fauxElement )
155155 return getChildrenArray ( child )
156156 }
157157
@@ -177,43 +177,29 @@ export const visitElement = (
177177
178178const visitLoop = (
179179 traversalChildren : AbstractElement [ ] [ ] ,
180- traversalIndex : number [ ] ,
181180 traversalMap : Array < void | ContextMap > ,
182181 traversalStore : Array < void | ContextEntry > ,
183182 queue : Frame [ ] ,
184183 visitor : Visitor
185- ) = > {
184+ ) : boolean => {
186185 const start = Date . now ( )
187186
188187 while ( traversalChildren . length > 0 ) {
189- const currChildren = traversalChildren [ traversalChildren . length - 1 ]
190- const currIndex = traversalIndex [ traversalIndex . length - 1 ] ++
191-
192- if ( currIndex < currChildren . length ) {
193- const element = currChildren [ currIndex ]
188+ const element = traversalChildren [ traversalChildren . length - 1 ] . shift ( )
189+ if ( element !== undefined ) {
194190 const children = visitElement ( element , queue , visitor )
195-
196- if ( children . length > 0 ) {
197- traversalChildren . push ( children )
198- traversalIndex . push ( 0 )
199- traversalMap . push ( flushPrevContextMap ( ) )
200- traversalStore . push ( flushPrevContextStore ( ) )
201- } else {
202- restoreContextMap ( flushPrevContextMap ( ) )
203- restoreContextStore ( flushPrevContextStore ( ) )
204- }
191+ traversalChildren . push ( children )
192+ traversalMap . push ( flushPrevContextMap ( ) )
193+ traversalStore . push ( flushPrevContextStore ( ) )
205194 } else {
206195 traversalChildren . pop ( )
207- traversalIndex . pop ( )
208196 restoreContextMap ( traversalMap . pop ( ) )
209197 restoreContextStore ( traversalStore . pop ( ) )
210198 }
199+ }
211200
212- /*
213- if (Date.now() - start > YIELD_AFTER_MS) {
214- return true
215- }
216- */
201+ if ( Date . now ( ) - start > YIELD_AFTER_MS ) {
202+ return true
217203 }
218204
219205 return false
@@ -225,12 +211,10 @@ export const visitChildren = (
225211 visitor : Visitor
226212) = > {
227213 const traversalChildren : AbstractElement [ ] [ ] = [ init ]
228- const traversalIndex : number [ ] = [ 0 ]
229214 const traversalMap : Array < void | ContextMap > = [ flushPrevContextMap ( ) ]
230215 const traversalStore : Array < void | ContextEntry > = [ flushPrevContextStore ( ) ]
231216 const hasYielded = visitLoop (
232217 traversalChildren ,
233- traversalIndex ,
234218 traversalMap ,
235219 traversalStore ,
236220 queue ,
@@ -244,7 +228,6 @@ export const visitChildren = (
244228 thenable : Promise . resolve ( ) ,
245229 kind : 'frame.yield' ,
246230 children : traversalChildren ,
247- index : traversalIndex ,
248231 map : traversalMap ,
249232 store : traversalStore
250233 } )
@@ -260,5 +243,5 @@ export const resumeVisitChildren = (
260243 setCurrentContextMap ( frame . contextMap )
261244 setCurrentContextStore ( frame . contextStore )
262245
263- visitLoop ( frame . children , frame . index , frame . map , frame . store , queue , visitor )
246+ visitLoop ( frame . children , frame . map , frame . store , queue , visitor )
264247}
0 commit comments