????JFIF??x?x????'
| Server IP : 79.136.114.73 / Your IP : 216.73.216.191 Web Server : Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.29 OpenSSL/1.0.1f System : Linux b8009 3.13.0-170-generic #220-Ubuntu SMP Thu May 9 12:40:49 UTC 2019 x86_64 User : www-data ( 33) PHP Version : 5.5.9-1ubuntu4.29 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/lib/node_modules/npm/node_modules/treeverse/lib/ |
Upload File : |
// Perform a breadth-first walk of a tree, either logical or physical
// This one only visits, it doesn't leave. That's because
// in a breadth-first traversal, children may be visited long
// after their parent, so the "exit" pass ends up being just
// another breadth-first walk.
//
// Breadth-first traversals are good for either creating a tree (ie,
// reifying a dep graph based on a package.json without a node_modules
// or package-lock), or mutating it in-place. For a map-reduce type of
// walk, it doesn't make a lot of sense, and is very expensive.
const breadth = ({
visit,
filter = () => true,
getChildren,
tree,
}) => {
const queue = []
const seen = new Map()
const next = () => {
while (queue.length) {
const node = queue.shift()
const res = visitNode(node)
if (isPromise(res)) {
return res.then(() => next())
}
}
return seen.get(tree)
}
const visitNode = (tree) => {
if (seen.has(tree))
return seen.get(tree)
seen.set(tree, null)
const res = visit ? visit(tree) : tree
if (isPromise(res)) {
const fullResult = res.then(res => {
seen.set(tree, res)
return kidNodes(tree)
})
seen.set(tree, fullResult)
return fullResult
} else {
seen.set(tree, res)
return kidNodes(tree)
}
}
const kidNodes = (tree) => {
const kids = getChildren(tree, seen.get(tree))
return isPromise(kids) ? kids.then(processKids) : processKids(kids)
}
const processKids = (kids) => {
kids = (kids || []).filter(filter)
queue.push(...kids)
}
queue.push(tree)
return next()
}
const isPromise = p => p && typeof p.then === 'function'
module.exports = breadth