async-rails 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d28e11df0279381c8d0e03b6a2f51ecf9253846
4
- data.tar.gz: e2e6a3d8e860fdab23ae6534d7700e56f670a7a0
3
+ metadata.gz: 6a7bd80f3580610ce08bedf6109fff6f54ad7120
4
+ data.tar.gz: 3a8ad7537bc492c85e8a40b21d7e1054ca23de3c
5
5
  SHA512:
6
- metadata.gz: 053811083f01269722e23d9f066d2d4846aa0ef8474717361f3d09b55fcb344614db1096792cce018b86bfe7b65df2302940861d351bbcfbc0e86a03ff45cc3b
7
- data.tar.gz: 8606494455f7a21cf80951e24c9e280aa1a500c003382282e78e15d41518a77bc978ca66d32e4eca9c017d6796bdac03a0fb8d291a1da71a3cd3626585165e57
6
+ metadata.gz: d8072b21d7c64f39aab41ad85caa776f23ca9c082bc5cbc360c85c8085c6a98bf1022fc6a03447229a4576e9c2adfe826fb3d97522b44bc192ffc008963abeb7
7
+ data.tar.gz: bf2475def426eab8bd83e68f9938ad61d60c5e91d9d24acd8c5f5400b95424d5feab615ae1b8520aeb84304eb21250374618c144da8c0444f4ce0526fe285e49
@@ -1,5 +1,5 @@
1
1
  module Async
2
2
  module Rails
3
- VERSION = "1.3.0"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
@@ -9,6 +9,15 @@
9
9
 
10
10
  var async = {};
11
11
  function noop() {}
12
+ function identity(v) {
13
+ return v;
14
+ }
15
+ function toBool(v) {
16
+ return !!v;
17
+ }
18
+ function notId(v) {
19
+ return !v;
20
+ }
12
21
 
13
22
  // global on the server, window in the browser
14
23
  var previous_async;
@@ -30,20 +39,18 @@
30
39
  };
31
40
 
32
41
  function only_once(fn) {
33
- var called = false;
34
42
  return function() {
35
- if (called) throw new Error("Callback was already called.");
36
- called = true;
43
+ if (fn === null) throw new Error("Callback was already called.");
37
44
  fn.apply(this, arguments);
45
+ fn = null;
38
46
  };
39
47
  }
40
48
 
41
49
  function _once(fn) {
42
- var called = false;
43
50
  return function() {
44
- if (called) return;
45
- called = true;
51
+ if (fn === null) return;
46
52
  fn.apply(this, arguments);
53
+ fn = null;
47
54
  };
48
55
  }
49
56
 
@@ -107,6 +114,13 @@
107
114
  });
108
115
  }
109
116
 
117
+ function _indexOf(arr, item) {
118
+ for (var i = 0; i < arr.length; i++) {
119
+ if (arr[i] === item) return i;
120
+ }
121
+ return -1;
122
+ }
123
+
110
124
  var _keys = Object.keys || function (obj) {
111
125
  var keys = [];
112
126
  for (var k in obj) {
@@ -137,21 +151,30 @@
137
151
  }
138
152
  }
139
153
 
140
- function _baseSlice(arr, start) {
141
- start = start || 0;
142
- var index = -1;
143
- var length = arr.length;
144
-
145
- if (start) {
146
- length -= start;
147
- length = length < 0 ? 0 : length;
148
- }
149
- var result = Array(length);
150
-
151
- while (++index < length) {
152
- result[index] = arr[index + start];
153
- }
154
- return result;
154
+ // Similar to ES6's rest param (http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html)
155
+ // This accumulates the arguments passed into an array, after a given index.
156
+ // From underscore.js (https://github.com/jashkenas/underscore/pull/2140).
157
+ function _restParam(func, startIndex) {
158
+ startIndex = startIndex == null ? func.length - 1 : +startIndex;
159
+ return function() {
160
+ var length = Math.max(arguments.length - startIndex, 0);
161
+ var rest = Array(length);
162
+ for (var index = 0; index < length; index++) {
163
+ rest[index] = arguments[index + startIndex];
164
+ }
165
+ switch (startIndex) {
166
+ case 0: return func.call(this, rest);
167
+ case 1: return func.call(this, arguments[0], rest);
168
+ case 2: return func.call(this, arguments[0], arguments[1], rest);
169
+ }
170
+ // Currently unused but handle cases outside of the switch statement:
171
+ // var args = Array(startIndex + 1);
172
+ // for (index = 0; index < startIndex; index++) {
173
+ // args[index] = arguments[index];
174
+ // }
175
+ // args[startIndex] = rest;
176
+ // return func.apply(this, args);
177
+ };
155
178
  }
156
179
 
157
180
  function _withoutIndex(iterator) {
@@ -165,38 +188,22 @@
165
188
  //// nextTick implementation with browser-compatible fallback ////
166
189
 
167
190
  // capture the global reference to guard against fakeTimer mocks
168
- var _setImmediate;
169
- if (typeof setImmediate === 'function') {
170
- _setImmediate = setImmediate;
171
- }
191
+ var _setImmediate = typeof setImmediate === 'function' && setImmediate;
172
192
 
173
- if (typeof process === 'undefined' || !(process.nextTick)) {
174
- if (_setImmediate) {
175
- async.nextTick = function (fn) {
176
- // not a direct alias for IE10 compatibility
177
- _setImmediate(fn);
178
- };
179
- async.setImmediate = async.nextTick;
180
- }
181
- else {
182
- async.nextTick = function (fn) {
183
- setTimeout(fn, 0);
184
- };
185
- async.setImmediate = async.nextTick;
186
- }
187
- }
188
- else {
193
+ var _delay = _setImmediate ? function(fn) {
194
+ // not a direct alias for IE10 compatibility
195
+ _setImmediate(fn);
196
+ } : function(fn) {
197
+ setTimeout(fn, 0);
198
+ };
199
+
200
+ if (typeof process === 'object' && typeof process.nextTick === 'function') {
189
201
  async.nextTick = process.nextTick;
190
- if (_setImmediate) {
191
- async.setImmediate = function (fn) {
192
- // not a direct alias for IE10 compatibility
193
- _setImmediate(fn);
194
- };
195
- }
196
- else {
197
- async.setImmediate = async.nextTick;
198
- }
202
+ } else {
203
+ async.nextTick = _delay;
199
204
  }
205
+ async.setImmediate = _setImmediate ? _delay : async.nextTick;
206
+
200
207
 
201
208
  async.forEach =
202
209
  async.each = function (arr, iterator, callback) {
@@ -328,8 +335,8 @@
328
335
  return fn(async.eachOf, obj, iterator, callback);
329
336
  };
330
337
  }
331
- function doParallelLimit(limit, fn) {
332
- return function (obj, iterator, callback) {
338
+ function doParallelLimit(fn) {
339
+ return function (obj, limit, iterator, callback) {
333
340
  return fn(_eachOfLimit(limit), obj, iterator, callback);
334
341
  };
335
342
  }
@@ -354,13 +361,7 @@
354
361
 
355
362
  async.map = doParallel(_asyncMap);
356
363
  async.mapSeries = doSeries(_asyncMap);
357
- async.mapLimit = function (arr, limit, iterator, callback) {
358
- return _mapLimit(limit)(arr, iterator, callback);
359
- };
360
-
361
- function _mapLimit(limit) {
362
- return doParallelLimit(limit, _asyncMap);
363
- }
364
+ async.mapLimit = doParallelLimit(_asyncMap);
364
365
 
365
366
  // reduce only has a series version, as doing reduce in parallel won't
366
367
  // work in many situations.
@@ -379,21 +380,16 @@
379
380
 
380
381
  async.foldr =
381
382
  async.reduceRight = function (arr, memo, iterator, callback) {
382
- var reversed = _map(arr, function (x) {
383
- return x;
384
- }).reverse();
383
+ var reversed = _map(arr, identity).reverse();
385
384
  async.reduce(reversed, memo, iterator, callback);
386
385
  };
387
386
 
388
387
  function _filter(eachfn, arr, iterator, callback) {
389
388
  var results = [];
390
- arr = _map(arr, function (x, i) {
391
- return {index: i, value: x};
392
- });
393
389
  eachfn(arr, function (x, index, callback) {
394
- iterator(x.value, function (v) {
390
+ iterator(x, function (v) {
395
391
  if (v) {
396
- results.push(x);
392
+ results.push({index: index, value: x});
397
393
  }
398
394
  callback();
399
395
  });
@@ -409,6 +405,9 @@
409
405
  async.select =
410
406
  async.filter = doParallel(_filter);
411
407
 
408
+ async.selectLimit =
409
+ async.filterLimit = doParallelLimit(_filter);
410
+
412
411
  async.selectSeries =
413
412
  async.filterSeries = doSeries(_filter);
414
413
 
@@ -420,55 +419,49 @@
420
419
  }, callback);
421
420
  }
422
421
  async.reject = doParallel(_reject);
422
+ async.rejectLimit = doParallelLimit(_reject);
423
423
  async.rejectSeries = doSeries(_reject);
424
424
 
425
- function _detect(eachfn, arr, iterator, main_callback) {
426
- eachfn(arr, function (x, index, callback) {
427
- iterator(x, function (result) {
428
- if (result) {
429
- main_callback(x);
430
- main_callback = noop;
431
- }
432
- else {
425
+ function _createTester(eachfn, check, getResult) {
426
+ return function(arr, limit, iterator, cb) {
427
+ function done() {
428
+ if (cb) cb(getResult(false, void 0));
429
+ }
430
+ function iteratee(x, _, callback) {
431
+ if (!cb) return callback();
432
+ iterator(x, function (v) {
433
+ if (cb && check(v)) {
434
+ cb(getResult(true, x));
435
+ cb = iterator = false;
436
+ }
433
437
  callback();
434
- }
435
- });
436
- }, function () {
437
- main_callback();
438
- });
438
+ });
439
+ }
440
+ if (arguments.length > 3) {
441
+ eachfn(arr, limit, iteratee, done);
442
+ } else {
443
+ cb = iterator;
444
+ iterator = limit;
445
+ eachfn(arr, iteratee, done);
446
+ }
447
+ };
439
448
  }
440
- async.detect = doParallel(_detect);
441
- async.detectSeries = doSeries(_detect);
442
449
 
443
450
  async.any =
444
- async.some = function (arr, iterator, main_callback) {
445
- async.eachOf(arr, function (x, _, callback) {
446
- iterator(x, function (v) {
447
- if (v) {
448
- main_callback(true);
449
- main_callback = noop;
450
- }
451
- callback();
452
- });
453
- }, function () {
454
- main_callback(false);
455
- });
456
- };
451
+ async.some = _createTester(async.eachOf, toBool, identity);
452
+
453
+ async.someLimit = _createTester(async.eachOfLimit, toBool, identity);
457
454
 
458
455
  async.all =
459
- async.every = function (arr, iterator, main_callback) {
460
- async.eachOf(arr, function (x, _, callback) {
461
- iterator(x, function (v) {
462
- if (!v) {
463
- main_callback(false);
464
- main_callback = noop;
465
- }
466
- callback();
467
- });
468
- }, function () {
469
- main_callback(true);
470
- });
471
- };
456
+ async.every = _createTester(async.eachOf, notId, notId);
457
+
458
+ async.everyLimit = _createTester(async.eachOfLimit, notId, notId);
459
+
460
+ function _findGetResult(v, x) {
461
+ return x;
462
+ }
463
+ async.detect = _createTester(async.eachOf, identity, _findGetResult);
464
+ async.detectSeries = _createTester(async.eachOfSeries, identity, _findGetResult);
472
465
 
473
466
  async.sortBy = function (arr, iterator, callback) {
474
467
  async.map(arr, function (x, callback) {
@@ -513,12 +506,8 @@
513
506
  listeners.unshift(fn);
514
507
  }
515
508
  function removeListener(fn) {
516
- for (var i = 0; i < listeners.length; i += 1) {
517
- if (listeners[i] === fn) {
518
- listeners.splice(i, 1);
519
- return;
520
- }
521
- }
509
+ var idx = _indexOf(listeners, fn);
510
+ if (idx >= 0) listeners.splice(idx, 1);
522
511
  }
523
512
  function taskComplete() {
524
513
  remainingTasks--;
@@ -535,15 +524,14 @@
535
524
 
536
525
  _arrayEach(keys, function (k) {
537
526
  var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]];
538
- function taskCallback(err) {
539
- var args = _baseSlice(arguments, 1);
527
+ var taskCallback = _restParam(function(err, args) {
540
528
  if (args.length <= 1) {
541
529
  args = args[0];
542
530
  }
543
531
  if (err) {
544
532
  var safeResults = {};
545
- _arrayEach(_keys(results), function(rkey) {
546
- safeResults[rkey] = results[rkey];
533
+ _forEachOf(results, function(val, rkey) {
534
+ safeResults[rkey] = val;
547
535
  });
548
536
  safeResults[k] = args;
549
537
  callback(err, safeResults);
@@ -552,8 +540,8 @@
552
540
  results[k] = args;
553
541
  async.setImmediate(taskComplete);
554
542
  }
555
- }
556
- var requires = task.slice(0, Math.abs(task.length - 1)) || [];
543
+ });
544
+ var requires = task.slice(0, task.length - 1);
557
545
  // prevent dead-locks
558
546
  var len = requires.length;
559
547
  var dep;
@@ -561,7 +549,7 @@
561
549
  if (!(dep = tasks[requires[len]])) {
562
550
  throw new Error('Has inexistant dependency');
563
551
  }
564
- if (_isArray(dep) && !!~dep.indexOf(k)) {
552
+ if (_isArray(dep) && _indexOf(dep, k) >= 0) {
565
553
  throw new Error('Has cyclic dependencies');
566
554
  }
567
555
  }
@@ -587,53 +575,40 @@
587
575
 
588
576
 
589
577
 
590
- async.retry = function(/*[times,] task [, callback]*/) {
578
+ async.retry = function(times, task, callback) {
591
579
  var DEFAULT_TIMES = 5;
592
580
  var DEFAULT_INTERVAL = 0;
593
581
 
594
582
  var attempts = [];
595
583
 
596
584
  var opts = {
597
- times: DEFAULT_TIMES,
598
- interval: DEFAULT_INTERVAL
585
+ times: DEFAULT_TIMES,
586
+ interval: DEFAULT_INTERVAL
599
587
  };
600
588
 
601
589
  function parseTimes(acc, t){
602
- if(typeof t === 'number'){
603
- acc.times = parseInt(t, 10) || DEFAULT_TIMES;
604
- } else if(typeof t === 'object'){
605
- acc.times = parseInt(t.times, 10) || DEFAULT_TIMES;
606
- acc.interval = parseInt(t.interval, 10) || DEFAULT_INTERVAL;
607
- } else {
608
- throw new Error('Unsupported argument type for \'times\': ' + typeof(t));
609
- }
590
+ if(typeof t === 'number'){
591
+ acc.times = parseInt(t, 10) || DEFAULT_TIMES;
592
+ } else if(typeof t === 'object'){
593
+ acc.times = parseInt(t.times, 10) || DEFAULT_TIMES;
594
+ acc.interval = parseInt(t.interval, 10) || DEFAULT_INTERVAL;
595
+ } else {
596
+ throw new Error('Unsupported argument type for \'times\': ' + typeof(t));
597
+ }
610
598
  }
611
599
 
612
- switch(arguments.length){
613
- case 1: {
614
- opts.task = arguments[0];
615
- break;
616
- }
617
- case 2 : {
618
- if(typeof arguments[0] === 'number' || typeof arguments[0] === 'object'){
619
- parseTimes(opts, arguments[0]);
620
- opts.task = arguments[1];
621
- } else {
622
- opts.task = arguments[0];
623
- opts.callback = arguments[1];
624
- }
625
- break;
626
- }
627
- case 3: {
628
- parseTimes(opts, arguments[0]);
629
- opts.task = arguments[1];
630
- opts.callback = arguments[2];
631
- break;
632
- }
633
- default: {
634
- throw new Error('Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)');
635
- }
636
- }
600
+ var length = arguments.length;
601
+ if (length < 1 || length > 3) {
602
+ throw new Error('Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)');
603
+ } else if (length <= 2 && typeof times === 'function') {
604
+ callback = task;
605
+ task = times;
606
+ }
607
+ if (typeof times !== 'function') {
608
+ parseTimes(opts, times);
609
+ }
610
+ opts.callback = callback;
611
+ opts.task = task;
637
612
 
638
613
  function wrappedTask(wrappedCallback, wrappedResults) {
639
614
  function retryAttempt(task, finalAttempt) {
@@ -645,11 +620,11 @@
645
620
  }
646
621
 
647
622
  function retryInterval(interval){
648
- return function(seriesCallback){
649
- setTimeout(function(){
650
- seriesCallback(null);
651
- }, interval);
652
- };
623
+ return function(seriesCallback){
624
+ setTimeout(function(){
625
+ seriesCallback(null);
626
+ }, interval);
627
+ };
653
628
  }
654
629
 
655
630
  while (opts.times) {
@@ -657,7 +632,7 @@
657
632
  var finalAttempt = !(opts.times-=1);
658
633
  attempts.push(retryAttempt(opts.task, finalAttempt));
659
634
  if(!finalAttempt && opts.interval > 0){
660
- attempts.push(retryInterval(opts.interval));
635
+ attempts.push(retryInterval(opts.interval));
661
636
  }
662
637
  }
663
638
 
@@ -681,12 +656,11 @@
681
656
  return callback();
682
657
  }
683
658
  function wrapIterator(iterator) {
684
- return function (err) {
659
+ return _restParam(function (err, args) {
685
660
  if (err) {
686
- callback.apply(null, arguments);
661
+ callback.apply(null, [err].concat(args));
687
662
  }
688
663
  else {
689
- var args = _baseSlice(arguments, 1);
690
664
  var next = iterator.next();
691
665
  if (next) {
692
666
  args.push(wrapIterator(next));
@@ -696,7 +670,7 @@
696
670
  }
697
671
  ensureAsync(iterator).apply(null, args);
698
672
  }
699
- };
673
+ });
700
674
  }
701
675
  wrapIterator(async.iterator(tasks))();
702
676
  };
@@ -706,14 +680,13 @@
706
680
  var results = _isArrayLike(tasks) ? [] : {};
707
681
 
708
682
  eachfn(tasks, function (task, key, callback) {
709
- task(function (err) {
710
- var args = _baseSlice(arguments, 1);
683
+ task(_restParam(function (err, args) {
711
684
  if (args.length <= 1) {
712
685
  args = args[0];
713
686
  }
714
687
  results[key] = args;
715
688
  callback(err);
716
- });
689
+ }));
717
690
  }, function (err) {
718
691
  callback(err, results);
719
692
  });
@@ -727,22 +700,8 @@
727
700
  _parallel(_eachOfLimit(limit), tasks, callback);
728
701
  };
729
702
 
730
- async.series = function (tasks, callback) {
731
- callback = callback || noop;
732
- var results = _isArrayLike(tasks) ? [] : {};
733
-
734
- async.eachOfSeries(tasks, function (task, key, callback) {
735
- task(function (err) {
736
- var args = _baseSlice(arguments, 1);
737
- if (args.length <= 1) {
738
- args = args[0];
739
- }
740
- results[key] = args;
741
- callback(err);
742
- });
743
- }, function (err) {
744
- callback(err, results);
745
- });
703
+ async.series = function(tasks, callback) {
704
+ _parallel(async.eachOfSeries, tasks, callback);
746
705
  };
747
706
 
748
707
  async.iterator = function (tasks) {
@@ -761,14 +720,13 @@
761
720
  return makeCallback(0);
762
721
  };
763
722
 
764
- async.apply = function (fn) {
765
- var args = _baseSlice(arguments, 1);
766
- return function () {
723
+ async.apply = _restParam(function (fn, args) {
724
+ return _restParam(function (callArgs) {
767
725
  return fn.apply(
768
- null, args.concat(_baseSlice(arguments))
726
+ null, args.concat(callArgs)
769
727
  );
770
- };
771
- };
728
+ });
729
+ });
772
730
 
773
731
  function _concat(eachfn, arr, fn, callback) {
774
732
  var result = [];
@@ -787,105 +745,74 @@
787
745
  async.whilst = function (test, iterator, callback) {
788
746
  callback = callback || noop;
789
747
  if (test()) {
790
- iterator(function (err) {
748
+ var next = _restParam(function(err, args) {
791
749
  if (err) {
792
- return callback(err);
750
+ callback(err);
751
+ } else if (test.apply(this, args)) {
752
+ iterator(next);
753
+ } else {
754
+ callback(null);
793
755
  }
794
- async.whilst(test, iterator, callback);
795
756
  });
796
- }
797
- else {
757
+ iterator(next);
758
+ } else {
798
759
  callback(null);
799
760
  }
800
761
  };
801
762
 
802
763
  async.doWhilst = function (iterator, test, callback) {
803
- callback = callback || noop;
804
- iterator(function (err) {
805
- if (err) {
806
- return callback(err);
807
- }
808
- var args = _baseSlice(arguments, 1);
809
- if (test.apply(null, args)) {
810
- async.doWhilst(iterator, test, callback);
811
- }
812
- else {
813
- callback(null);
814
- }
815
- });
764
+ var calls = 0;
765
+ return async.whilst(function() {
766
+ return ++calls <= 1 || test.apply(this, arguments);
767
+ }, iterator, callback);
816
768
  };
817
769
 
818
770
  async.until = function (test, iterator, callback) {
819
- callback = callback || noop;
820
- if (!test()) {
821
- iterator(function (err) {
822
- if (err) {
823
- return callback(err);
824
- }
825
- async.until(test, iterator, callback);
826
- });
827
- }
828
- else {
829
- callback(null);
830
- }
771
+ return async.whilst(function() {
772
+ return !test.apply(this, arguments);
773
+ }, iterator, callback);
831
774
  };
832
775
 
833
776
  async.doUntil = function (iterator, test, callback) {
834
- callback = callback || noop;
835
- iterator(function (err) {
836
- if (err) {
837
- return callback(err);
838
- }
839
- var args = _baseSlice(arguments, 1);
840
- if (!test.apply(null, args)) {
841
- async.doUntil(iterator, test, callback);
842
- }
843
- else {
844
- callback(null);
845
- }
846
- });
777
+ return async.doWhilst(iterator, function() {
778
+ return !test.apply(this, arguments);
779
+ }, callback);
847
780
  };
848
781
 
849
782
  async.during = function (test, iterator, callback) {
850
783
  callback = callback || noop;
851
- test(function(err, truth) {
784
+
785
+ var next = _restParam(function(err, args) {
852
786
  if (err) {
853
- return callback(err);
854
- }
855
- if (truth) {
856
- iterator(function (err) {
857
- if (err) {
858
- return callback(err);
859
- }
860
- async.during(test, iterator, callback);
861
- });
787
+ callback(err);
788
+ } else {
789
+ args.push(check);
790
+ test.apply(this, args);
862
791
  }
863
- else {
792
+ });
793
+
794
+ var check = function(err, truth) {
795
+ if (err) {
796
+ callback(err);
797
+ } else if (truth) {
798
+ iterator(next);
799
+ } else {
864
800
  callback(null);
865
801
  }
866
- });
802
+ };
803
+
804
+ test(check);
867
805
  };
868
806
 
869
807
  async.doDuring = function (iterator, test, callback) {
870
- callback = callback || noop;
871
- iterator(function (err) {
872
- if (err) {
873
- return callback(err);
808
+ var calls = 0;
809
+ async.during(function(next) {
810
+ if (calls++ < 1) {
811
+ next(null, true);
812
+ } else {
813
+ test.apply(this, arguments);
874
814
  }
875
- var args = _baseSlice(arguments, 1);
876
- args.push(function (err, truth) {
877
- if (err) {
878
- return callback(err);
879
- }
880
- if (truth) {
881
- async.doDuring(iterator, test, callback);
882
- }
883
- else {
884
- callback(null);
885
- }
886
- });
887
- test.apply(null, args);
888
- });
815
+ }, iterator, callback);
889
816
  };
890
817
 
891
818
  function _queue(worker, concurrency, payload) {
@@ -906,7 +833,7 @@
906
833
  if(data.length === 0 && q.idle()) {
907
834
  // call drain immediately if there are no tasks
908
835
  return async.setImmediate(function() {
909
- q.drain();
836
+ q.drain();
910
837
  });
911
838
  }
912
839
  _arrayEach(data, function(task) {
@@ -1022,17 +949,17 @@
1022
949
  }
1023
950
 
1024
951
  function _binarySearch(sequence, item, compare) {
1025
- var beg = -1,
1026
- end = sequence.length - 1;
1027
- while (beg < end) {
1028
- var mid = beg + ((end - beg + 1) >>> 1);
1029
- if (compare(item, sequence[mid]) >= 0) {
1030
- beg = mid;
1031
- } else {
1032
- end = mid - 1;
1033
- }
1034
- }
1035
- return beg;
952
+ var beg = -1,
953
+ end = sequence.length - 1;
954
+ while (beg < end) {
955
+ var mid = beg + ((end - beg + 1) >>> 1);
956
+ if (compare(item, sequence[mid]) >= 0) {
957
+ beg = mid;
958
+ } else {
959
+ end = mid - 1;
960
+ }
961
+ }
962
+ return beg;
1036
963
  }
1037
964
 
1038
965
  function _insert(q, data, priority, callback) {
@@ -1084,10 +1011,8 @@
1084
1011
  };
1085
1012
 
1086
1013
  function _console_fn(name) {
1087
- return function (fn) {
1088
- var args = _baseSlice(arguments, 1);
1089
- fn.apply(null, args.concat([function (err) {
1090
- var args = _baseSlice(arguments, 1);
1014
+ return _restParam(function (fn, args) {
1015
+ fn.apply(null, args.concat([_restParam(function (err, args) {
1091
1016
  if (typeof console !== 'undefined') {
1092
1017
  if (err) {
1093
1018
  if (console.error) {
@@ -1100,8 +1025,8 @@
1100
1025
  });
1101
1026
  }
1102
1027
  }
1103
- }]));
1104
- };
1028
+ })]));
1029
+ });
1105
1030
  }
1106
1031
  async.log = _console_fn('log');
1107
1032
  async.dir = _console_fn('dir');
@@ -1112,11 +1037,8 @@
1112
1037
  async.memoize = function (fn, hasher) {
1113
1038
  var memo = {};
1114
1039
  var queues = {};
1115
- hasher = hasher || function (x) {
1116
- return x;
1117
- };
1118
- function memoized() {
1119
- var args = _baseSlice(arguments);
1040
+ hasher = hasher || identity;
1041
+ var memoized = _restParam(function memoized(args) {
1120
1042
  var callback = args.pop();
1121
1043
  var key = hasher.apply(null, args);
1122
1044
  if (key in memo) {
@@ -1129,16 +1051,16 @@
1129
1051
  }
1130
1052
  else {
1131
1053
  queues[key] = [callback];
1132
- fn.apply(null, args.concat([function () {
1133
- memo[key] = _baseSlice(arguments);
1054
+ fn.apply(null, args.concat([_restParam(function (args) {
1055
+ memo[key] = args;
1134
1056
  var q = queues[key];
1135
1057
  delete queues[key];
1136
1058
  for (var i = 0, l = q.length; i < l; i++) {
1137
- q[i].apply(null, arguments);
1059
+ q[i].apply(null, args);
1138
1060
  }
1139
- }]));
1061
+ })]));
1140
1062
  }
1141
- }
1063
+ });
1142
1064
  memoized.memo = memo;
1143
1065
  memoized.unmemoized = fn;
1144
1066
  return memoized;
@@ -1164,11 +1086,10 @@
1164
1086
 
1165
1087
  async.seq = function (/* functions... */) {
1166
1088
  var fns = arguments;
1167
- return function () {
1089
+ return _restParam(function (args) {
1168
1090
  var that = this;
1169
- var args = _baseSlice(arguments);
1170
1091
 
1171
- var callback = args.slice(-1)[0];
1092
+ var callback = args[args.length - 1];
1172
1093
  if (typeof callback == 'function') {
1173
1094
  args.pop();
1174
1095
  } else {
@@ -1176,16 +1097,14 @@
1176
1097
  }
1177
1098
 
1178
1099
  async.reduce(fns, args, function (newargs, fn, cb) {
1179
- fn.apply(that, newargs.concat([function () {
1180
- var err = arguments[0];
1181
- var nextargs = _baseSlice(arguments, 1);
1100
+ fn.apply(that, newargs.concat([_restParam(function (err, nextargs) {
1182
1101
  cb(err, nextargs);
1183
- }]));
1102
+ })]));
1184
1103
  },
1185
1104
  function (err, results) {
1186
1105
  callback.apply(that, [err].concat(results));
1187
1106
  });
1188
- };
1107
+ });
1189
1108
  };
1190
1109
 
1191
1110
  async.compose = function (/* functions... */) {
@@ -1193,33 +1112,27 @@
1193
1112
  };
1194
1113
 
1195
1114
 
1196
- function _applyEach(eachfn, fns /*args...*/) {
1197
- function go() {
1198
- var that = this;
1199
- var args = _baseSlice(arguments);
1200
- var callback = args.pop();
1201
- return eachfn(fns, function (fn, _, cb) {
1202
- fn.apply(that, args.concat([cb]));
1203
- },
1204
- callback);
1205
- }
1206
- if (arguments.length > 2) {
1207
- var args = _baseSlice(arguments, 2);
1208
- return go.apply(this, args);
1209
- }
1210
- else {
1211
- return go;
1212
- }
1115
+ function _applyEach(eachfn) {
1116
+ return _restParam(function(fns, args) {
1117
+ var go = _restParam(function(args) {
1118
+ var that = this;
1119
+ var callback = args.pop();
1120
+ return eachfn(fns, function (fn, _, cb) {
1121
+ fn.apply(that, args.concat([cb]));
1122
+ },
1123
+ callback);
1124
+ });
1125
+ if (args.length) {
1126
+ return go.apply(this, args);
1127
+ }
1128
+ else {
1129
+ return go;
1130
+ }
1131
+ });
1213
1132
  }
1214
1133
 
1215
- async.applyEach = function (/*fns, args...*/) {
1216
- var args = _baseSlice(arguments);
1217
- return _applyEach.apply(null, [async.eachOf].concat(args));
1218
- };
1219
- async.applyEachSeries = function (/*fns, args...*/) {
1220
- var args = _baseSlice(arguments);
1221
- return _applyEach.apply(null, [async.eachOfSeries].concat(args));
1222
- };
1134
+ async.applyEach = _applyEach(async.eachOf);
1135
+ async.applyEachSeries = _applyEach(async.eachOfSeries);
1223
1136
 
1224
1137
 
1225
1138
  async.forever = function (fn, callback) {
@@ -1235,8 +1148,7 @@
1235
1148
  };
1236
1149
 
1237
1150
  function ensureAsync(fn) {
1238
- return function (/*...args, callback*/) {
1239
- var args = _baseSlice(arguments);
1151
+ return _restParam(function (args) {
1240
1152
  var callback = args.pop();
1241
1153
  args.push(function () {
1242
1154
  var innerArgs = arguments;
@@ -1251,22 +1163,21 @@
1251
1163
  var sync = true;
1252
1164
  fn.apply(this, args);
1253
1165
  sync = false;
1254
- };
1166
+ });
1255
1167
  }
1256
1168
 
1257
1169
  async.ensureAsync = ensureAsync;
1258
1170
 
1259
- async.constant = function constant(/*values...*/) {
1260
- var args = [null].concat(_baseSlice(arguments));
1171
+ async.constant = _restParam(function(values) {
1172
+ var args = [null].concat(values);
1261
1173
  return function (callback) {
1262
1174
  return callback.apply(this, args);
1263
1175
  };
1264
- };
1176
+ });
1265
1177
 
1266
1178
  async.wrapSync =
1267
1179
  async.asyncify = function asyncify(func) {
1268
- return function (/*args..., callback*/) {
1269
- var args = _baseSlice(arguments);
1180
+ return _restParam(function (args) {
1270
1181
  var callback = args.pop();
1271
1182
  var result;
1272
1183
  try {
@@ -1274,8 +1185,17 @@
1274
1185
  } catch (e) {
1275
1186
  return callback(e);
1276
1187
  }
1277
- callback(null, result);
1278
- };
1188
+ // if result is Promise object
1189
+ if (typeof result !== 'undefined' && typeof result.then === "function") {
1190
+ result.then(function(value) {
1191
+ callback(null, value);
1192
+ }).catch(function(err) {
1193
+ callback(err.message ? err : new Error(err));
1194
+ });
1195
+ } else {
1196
+ callback(null, result);
1197
+ }
1198
+ });
1279
1199
  };
1280
1200
 
1281
1201
  // Node.js
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-15 00:00:00.000000000 Z
11
+ date: 2015-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties