Browse Source

update async lib

Nikolay Suslov 4 years ago
parent
commit
f2edb9315d
2 changed files with 80 additions and 58 deletions
  1. 80 58
      public/lib/async/async.js
  2. 0 0
      public/lib/async/async.min.js

+ 80 - 58
public/lib/async/async.js

@@ -60,6 +60,8 @@
         };
     }
 
+    /* istanbul ignore file */
+
     var hasSetImmediate = typeof setImmediate === 'function' && setImmediate;
     var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function';
 
@@ -215,10 +217,6 @@
             })
         }
 
-        Object.defineProperty(awaitable, 'name', {
-            value: `awaitable(${asyncFn.name})`
-        });
-
         return awaitable
     }
 
@@ -617,13 +615,14 @@
      * function.
      * @param {Function} [callback] - the final argument should be the callback,
      * called when all functions have completed processing.
-     * @returns {Function} - If only the first argument, `fns`, is provided, it will
-     * return a function which lets you pass in the arguments as if it were a single
-     * function call. The signature is `(..args, callback)`. If invoked with any
-     * arguments, `callback` is required.
+     * @returns {AsyncFunction} - Returns a function that takes no args other than
+     * an optional callback, that is the result of applying the `args` to each
+     * of the functions.
      * @example
      *
-     * async.applyEach([enableSearch, updateSchema], 'bucket', (err, results) => {
+     * const appliedFn = async.applyEach([enableSearch, updateSchema], 'bucket')
+     *
+     * appliedFn((err, results) => {
      *     // results[0] is the results for `enableSearch`
      *     // results[1] is the results for `updateSchema`
      * });
@@ -631,7 +630,7 @@
      * // partial application example:
      * async.each(
      *     buckets,
-     *     async.applyEach([enableSearch, updateSchema]),
+     *     async (bucket) => async.applyEach([enableSearch, updateSchema], bucket)(),
      *     callback
      * );
      */
@@ -699,9 +698,9 @@
      * function.
      * @param {Function} [callback] - the final argument should be the callback,
      * called when all functions have completed processing.
-     * @returns {Function} - If only the first argument is provided, it will return
-     * a function which lets you pass in the arguments as if it were a single
-     * function call.
+     * @returns {AsyncFunction} - A function, that when called, is the result of
+     * appling the `args` to the list of functions.  It takes no args, other than
+     * a callback.
      */
     var applyEachSeries = applyEach(mapSeries$1);
 
@@ -973,8 +972,8 @@
         return callback[PROMISE_SYMBOL]
     }
 
-    var FN_ARGS = /^(?:async\s+)?(?:function)?\s*[^(]*\(\s*([^)]+)\s*\)(?:\s*{)/m;
-    var ARROW_FN_ARGS = /^(?:async\s+)?(?:function\s+)?\(?\s*([^)^=]+)\s*\)?(?:\s*=>)/m;
+    var FN_ARGS = /^(?:async\s+)?(?:function)?\s*\w*\s*\(\s*([^)]+)\s*\)(?:\s*{)/;
+    var ARROW_FN_ARGS = /^(?:async\s+)?\(?\s*([^)=]+)\s*\)?(?:\s*=>)/;
     var FN_ARG_SPLIT = /,/;
     var FN_ARG = /(=.+)?(\s*)$/;
     var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
@@ -1251,27 +1250,26 @@
         }
 
         var processingScheduled = false;
-        function _insert(data, insertAtFront, callback) {
+        function _insert(data, insertAtFront, rejectOnError, callback) {
             if (callback != null && typeof callback !== 'function') {
                 throw new Error('task callback must be a function');
             }
             q.started = true;
-            /*if (Array.isArray(data)) {
-
-                return data.map(datum => _insert(datum, insertAtFront, callback));
-            }*/
 
-            var res;
+            var res, rej;
+            function promiseCallback (err, ...args) {
+                // we don't care about the error, let the global error handler
+                // deal with it
+                if (err) return rejectOnError ? rej(err) : res()
+                if (args.length <= 1) return res(args[0])
+                res(args);
+            }
 
             var item = {
                 data,
-                callback: callback || function (err, ...args) {
-                    // we don't care about the error, let the global error handler
-                    // deal with it
-                    if (err) return
-                    if (args.length <= 1) return res(args[0])
-                    res(args);
-                }
+                callback: rejectOnError ?
+                    promiseCallback :
+                    (callback || promiseCallback)
             };
 
             if (insertAtFront) {
@@ -1288,9 +1286,10 @@
                 });
             }
 
-            if (!callback) {
-                return new Promise((resolve) => {
+            if (rejectOnError || !callback) {
+                return new Promise((resolve, reject) => {
                     res = resolve;
+                    rej = reject;
                 })
             }
         }
@@ -1327,6 +1326,15 @@
             };
         }
 
+        function _maybeDrain(data) {
+            if (data.length === 0 && q.idle()) {
+                // call drain immediately if there are no tasks
+                setImmediate$1(() => trigger('drain'));
+                return true
+            }
+            return false
+        }
+
         const eventMethod = (name) => (handler) => {
             if (!handler) {
                 return new Promise((resolve, reject) => {
@@ -1354,13 +1362,17 @@
             paused: false,
             push (data, callback) {
                 if (Array.isArray(data)) {
-                    if (data.length === 0 && q.idle()) {
-                        // call drain immediately if there are no tasks
-                        return setImmediate$1(() => trigger('drain'));
-                    }
-                    return data.map(datum => _insert(datum, false, callback))
+                    if (_maybeDrain(data)) return
+                    return data.map(datum => _insert(datum, false, false, callback))
+                }
+                return _insert(data, false, false, callback);
+            },
+            pushAsync (data, callback) {
+                if (Array.isArray(data)) {
+                    if (_maybeDrain(data)) return
+                    return data.map(datum => _insert(datum, false, true, callback))
                 }
-                return _insert(data, false, callback);
+                return _insert(data, false, true, callback);
             },
             kill () {
                 off();
@@ -1368,13 +1380,17 @@
             },
             unshift (data, callback) {
                 if (Array.isArray(data)) {
-                    if (data.length === 0 && q.idle()) {
-                        // call drain immediately if there are no tasks
-                        return setImmediate$1(() => trigger('drain'));
-                    }
-                    return data.map(datum => _insert(datum, true, callback))
+                    if (_maybeDrain(data)) return
+                    return data.map(datum => _insert(datum, true, false, callback))
+                }
+                return _insert(data, true, false, callback);
+            },
+            unshiftAsync (data, callback) {
+                if (Array.isArray(data)) {
+                    if (_maybeDrain(data)) return
+                    return data.map(datum => _insert(datum, true, true, callback))
                 }
-                return _insert(data, true, callback);
+                return _insert(data, true, true, callback);
             },
             remove (testFn) {
                 q._tasks.remove(testFn);
@@ -1536,7 +1552,7 @@
      * @param {number} [payload=Infinity] - An optional `integer` for determining
      * how many tasks should be processed per round; if omitted, the default is
      * unlimited.
-     * @returns {module:ControlFlow.CargoObject} A cargoQueue object to manage the tasks. Callbacks can
+     * @returns {module:ControlFlow.QueueObject} A cargoQueue object to manage the tasks. Callbacks can
      * attached as certain properties to listen for specific events during the
      * lifecycle of the cargoQueue and inner queue.
      * @example
@@ -2925,7 +2941,7 @@
 
     var nextTick = wrap(_defer$1);
 
-    var _parallel = awaitify((eachfn, tasks, callback) => {
+    var parallel = awaitify((eachfn, tasks, callback) => {
         var results = isArrayLike(tasks) ? [] : {};
 
         eachfn(tasks, (task, key, taskCb) => {
@@ -3009,8 +3025,8 @@
      *     // results is now equals to: {one: 1, two: 2}
      * });
      */
-    function parallel(tasks, callback) {
-        return _parallel(eachOf$1, tasks, callback);
+    function parallel$1(tasks, callback) {
+        return parallel(eachOf$1, tasks, callback);
     }
 
     /**
@@ -3034,7 +3050,7 @@
      * @returns {Promise} a promise, if a callback is not passed
      */
     function parallelLimit(tasks, limit, callback) {
-        return _parallel(eachOfLimit(limit), tasks, callback);
+        return parallel(eachOfLimit(limit), tasks, callback);
     }
 
     /**
@@ -3057,12 +3073,16 @@
      * @property {number} payload - an integer that specifies how many items are
      * passed to the worker function at a time. only applies if this is a
      * [cargo]{@link module:ControlFlow.cargo} object
-     * @property {Function} push - add a new task to the `queue`. Calls `callback`
+     * @property {AsyncFunction} push - add a new task to the `queue`. Calls `callback`
      * once the `worker` has finished processing the task. Instead of a single task,
      * a `tasks` array can be submitted. The respective callback is used for every
      * task in the list. Invoke with `queue.push(task, [callback])`,
-     * @property {Function} unshift - add a new task to the front of the `queue`.
+     * @property {AsyncFunction} unshift - add a new task to the front of the `queue`.
      * Invoke with `queue.unshift(task, [callback])`.
+     * @property {AsyncFunction} pushAsync - the same as `q.push`, except this returns
+     * a promise that rejects if an error occurs.
+     * @property {AsyncFunction} unshirtAsync - the same as `q.unshift`, except this returns
+     * a promise that rejects if an error occurs.
      * @property {Function} remove - remove items from the queue that match a test
      * function.  The test function will be passed an object with a `data` property,
      * and a `priority` property, if this is a
@@ -3100,7 +3120,7 @@
      * should be pushed to the queue after calling this function. Invoke with `queue.kill()`.
      *
      * @example
-     * const q = aync.queue(worker, 2)
+     * const q = async.queue(worker, 2)
      * q.push(item1)
      * q.push(item2)
      * q.push(item3)
@@ -3934,7 +3954,7 @@
      * });
      */
     function series(tasks, callback) {
-        return _parallel(eachOfSeries$1, tasks, callback);
+        return parallel(eachOfSeries$1, tasks, callback);
     }
 
     /**
@@ -4408,7 +4428,7 @@
      *
      * var count = 0;
      * async.whilst(
-     *     function test(cb) { cb(null, count < 5;) },
+     *     function test(cb) { cb(null, count < 5); },
      *     function iter(callback) {
      *         count++;
      *         setTimeout(function() {
@@ -4469,14 +4489,16 @@
      *
      * @example
      * const results = []
-     * async.until(function iter(next) {
+     * let finished = false
+     * async.until(function test(page, cb) {
+     *     cb(null, finished)
+     * }, function iter(next) {
      *     fetchPage(url, (err, body) => {
      *         if (err) return next(err)
      *         results = results.concat(body.objects)
-     *         next(err, body)
+     *         finished = !!body.next
+     *         next(err)
      *     })
-     * }, function test(page, cb) {
-     *     cb(null, page.next == null)
      * }, function done (err) {
      *     // all pages have been fetched
      * })
@@ -4652,7 +4674,7 @@
         mapValuesSeries,
         memoize,
         nextTick,
-        parallel,
+        parallel: parallel$1,
         parallelLimit,
         priorityQueue,
         queue: queue$1,
@@ -4760,7 +4782,7 @@
     exports.mapValuesSeries = mapValuesSeries;
     exports.memoize = memoize;
     exports.nextTick = nextTick;
-    exports.parallel = parallel;
+    exports.parallel = parallel$1;
     exports.parallelLimit = parallelLimit;
     exports.priorityQueue = priorityQueue;
     exports.queue = queue$1;

File diff suppressed because it is too large
+ 0 - 0
public/lib/async/async.min.js


Some files were not shown because too many files changed in this diff