Merge pull request #1507 from bruce-one/next-tick

Use process.nextTick if available to reduce stack size.
This commit is contained in:
Leo Horie 2017-01-10 08:18:59 -05:00 committed by GitHub
commit 618020cd79

View file

@ -1,7 +1,7 @@
"use strict"
module.exports = new function init() {
var spec = {}, subjects = [], results = [], only = null, ctx = spec, start, stack = 0, hasProcess = typeof process === "object", hasOwn = ({}).hasOwnProperty
var spec = {}, subjects = [], results = [], only = null, ctx = spec, start, stack = 0, nextTickish, hasProcess = typeof process === "object", hasOwn = ({}).hasOwnProperty
function o(subject, predicate) {
if (predicate === undefined) return new Assert(subject)
@ -67,7 +67,6 @@ module.exports = new function init() {
next()
function next() {
stack++
if (cursor === fns.length) return
var fn = fns[cursor++]
@ -104,8 +103,7 @@ module.exports = new function init() {
}
else {
fn()
if (stack < 5000) next()
else (hasProcess ? process.nextTick : setTimeout)(next, stack = 0)
nextTickish(next)
}
}
}
@ -218,5 +216,14 @@ module.exports = new function init() {
if (hasProcess && status === 1) process.exit(1)
}
if(hasProcess) {
nextTickish = process.nextTick
} else {
nextTickish = function fakeFastNextTick(fn) {
if (stack++ < 5000) next()
else setTimeout(next, stack = 0)
}
}
return o
}