Skip to content

Commit 45493e8

Browse files
committed
Ensures promise flow calls the componentWillUnmount in the correct order.
1 parent 582180a commit 45493e8

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

src/index.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,19 @@ export default function reactTreeWalker(
8080
const visitCurrentElement = (childResolver, context, compInstance) =>
8181
Promise.resolve(safeVisitor(currentElement, compInstance, context))
8282
.then(result => {
83-
if (result === false) {
84-
// Visitor returned false, indicating a desire to not visit
85-
// the children of the current element, so we will just resolve.
86-
innerResolve()
87-
} else {
83+
if (result !== false) {
8884
// A false wasn't returned so we will attempt to visit the children
8985
// for the current element.
9086
const child = ensureChild(childResolver())
9187
const theChildContext =
9288
typeof context === 'function' ? context() : context
93-
9489
if (child == null) {
95-
// No children. We've reached the end of this branch. resolve.
96-
innerResolve()
90+
// No children. We've reached the end of this branch.
9791
} else if (Children.count(child)) {
9892
// If its a react Children collection we need to breadth-first
9993
// traverse each of them, and pMapSeries allows us to do a
10094
// depth-first traversal that respects Promises. Thanks @sindresorhus!
101-
pMapSeries(
95+
return pMapSeries(
10296
Children.map(child, cur => cur),
10397
aChild =>
10498
aChild ? recursive(aChild, theChildContext) : undefined,
@@ -107,11 +101,12 @@ export default function reactTreeWalker(
107101
.catch(reject)
108102
} else {
109103
// Otherwise we pass the individual child to the next recursion.
110-
recursive(child, theChildContext)
104+
return recursive(child, theChildContext)
111105
.then(innerResolve, reject)
112106
.catch(reject)
113107
}
114108
}
109+
return undefined
115110
})
116111
.catch(reject)
117112

@@ -170,20 +165,22 @@ export default function reactTreeWalker(
170165
)
171166
: currentContext,
172167
instance,
173-
).then(() => {
174-
if (
175-
options.componentWillUnmount &&
176-
instance.componentWillUnmount
177-
) {
178-
instance.componentWillUnmount()
179-
}
180-
})
168+
)
169+
.then(() => {
170+
if (
171+
options.componentWillUnmount &&
172+
instance.componentWillUnmount
173+
) {
174+
instance.componentWillUnmount()
175+
}
176+
})
177+
.then(innerResolve)
181178
} else {
182179
// Stateless Functional Component
183180
visitCurrentElement(
184181
() => Component(props, currentContext),
185182
currentContext,
186-
)
183+
).then(innerResolve)
187184
}
188185
} else {
189186
// This must be a basic element, such as a string or dom node.
@@ -193,7 +190,7 @@ export default function reactTreeWalker(
193190
? currentElement.props.children
194191
: undefined,
195192
currentContext,
196-
)
193+
).then(innerResolve)
197194
}
198195
})
199196

0 commit comments

Comments
 (0)