Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwind recursion in loopAsync when possible #2923

Merged
merged 3 commits into from
Jan 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions modules/AsyncUtils.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
export function loopAsync(turns, work, callback) {
let currentTurn = 0, isDone = false
let sync = false, hasNext = false, doneArgs

function done() {
isDone = true
if (sync) {
// Iterate instead of recursing if possible.
doneArgs = [ ...arguments ]
return
}

callback.apply(this, arguments)
}

function next() {
if (isDone)
if (isDone) {
return
}

hasNext = true
if (sync) {
// Iterate instead of recursing if possible.
return
}

sync = true

if (currentTurn < turns) {
while (!isDone && currentTurn < turns && hasNext) {
hasNext = false
work.call(this, currentTurn++, next, done)
} else {
done.apply(this, arguments)
}

sync = false

if (isDone) {
// This means the loop finished synchronously.
callback.apply(this, doneArgs)
return
}

if (currentTurn >= turns && hasNext) {
isDone = true
callback()
}
}

Expand Down
35 changes: 26 additions & 9 deletions modules/matchRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import { createRoutes } from './RouteUtils'

function getChildRoutes(route, location, callback) {
if (route.childRoutes) {
callback(null, route.childRoutes)
} else if (route.getChildRoutes) {
route.getChildRoutes(location, function (error, childRoutes) {
callback(error, !error && createRoutes(childRoutes))
})
} else {
callback()
return [ null, route.childRoutes ]
}
if (!route.getChildRoutes) {
return []
}

let sync = true, result

route.getChildRoutes(location, function (error, childRoutes) {
childRoutes = !error && createRoutes(childRoutes)
if (sync) {
result = [ error, childRoutes ]
return
}

callback(error, childRoutes)
})

sync = false
return result // Might be undefined.
}

function getIndexRoute(route, location, callback) {
Expand Down Expand Up @@ -116,7 +128,7 @@ function matchRouteDeep(
// Either a) this route matched at least some of the path or b)
// we don't have to load this route's children asynchronously. In
// either case continue checking for matches in the subtree.
getChildRoutes(route, location, function (error, childRoutes) {
const onChildRoutes = (error, childRoutes) => {
if (error) {
callback(error)
} else if (childRoutes) {
Expand All @@ -135,7 +147,12 @@ function matchRouteDeep(
} else {
callback()
}
})
}

const result = getChildRoutes(route, location, onChildRoutes)
if (result) {
onChildRoutes(...result)
}
} else {
callback()
}
Expand Down