• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2throw new Error("TODO: Not yet implemented.")
3
4/*
5usage:
6
7Like asyncMap, but only can take a single cb, and guarantees
8the order of the results.
9*/
10
11module.exports = asyncMapOrdered
12
13function asyncMapOrdered (list, fn, cb_) {
14  if (typeof cb_ !== "function") throw new Error(
15    "No callback provided to asyncMapOrdered")
16
17  if (typeof fn !== "function") throw new Error(
18    "No map function provided to asyncMapOrdered")
19
20  if (list === undefined || list === null) return cb_(null, [])
21  if (!Array.isArray(list)) list = [list]
22  if (!list.length) return cb_(null, [])
23
24  var errState = null
25    , l = list.length
26    , a = l
27    , res = []
28    , resCount = 0
29    , maxArgLen = 0
30
31  function cb (index) { return function () {
32    if (errState) return
33    var er = arguments[0]
34    var argLen = arguments.length
35    maxArgLen = Math.max(maxArgLen, argLen)
36    res[index] = argLen === 1 ? [er] : Array.apply(null, arguments)
37
38    // see if any new things have been added.
39    if (list.length > l) {
40      var newList = list.slice(l)
41      a += (list.length - l)
42      var oldLen = l
43      l = list.length
44      process.nextTick(function () {
45        newList.forEach(function (ar, i) { fn(ar, cb(i + oldLen)) })
46      })
47    }
48
49    if (er || --a === 0) {
50      errState = er
51      cb_.apply(null, [errState].concat(flip(res, resCount, maxArgLen)))
52    }
53  }}
54  // expect the supplied cb function to be called
55  // "n" times for each thing in the array.
56  list.forEach(function (ar) {
57    steps.forEach(function (fn, i) { fn(ar, cb(i)) })
58  })
59}
60
61function flip (res, resCount, argLen) {
62  var flat = []
63  // res = [[er, x, y], [er, x1, y1], [er, x2, y2, z2]]
64  // return [[x, x1, x2], [y, y1, y2], [undefined, undefined, z2]]
65
66