var parseq = use('parseq', $_.delay)
var time = use('time')

function load_comment_from_api_requestor(id) {
  return function(cb) {
    return $_.delay(() => cb({ id, title: `Comment #${id}` }), 0.5)
    // returning the $_.delay return lets them be cancelled up the chain
  }
}

$_.receiver(tree => {
  var child_reqs = tree.children.map(child => cb => {
    $_.start(e => send(e.actor, child, cb), "tests/comments")
  })

  var job = parseq.par_all({
    comment: load_comment_from_api_requestor(tree.id),
    children: cb => parseq.par_all(child_reqs, /*time*/null, /*thr*/10)(cb)
  })

  job((result, reason) => {
    if (!result) {
      log.error(reason)
      send(tree, reason)
    }
    
    send(tree, { ...result.comment, children: result.children, time: time.text() })
  })
})
