async-rails 0.2.10 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c20d85f99ebb71a71566ada46a73fb7f423a6ab
4
- data.tar.gz: 9c833abbfe8d81296206e2b5431822767d1cf314
3
+ metadata.gz: 70969f55042fe2f0ebd73b62429c669b1aba2cba
4
+ data.tar.gz: d49d49c31d1945008a754c59a96edcc4d68d1ee0
5
5
  SHA512:
6
- metadata.gz: 770915f7715652b73b7f646b78ae35a7091e7d4a32fccaa0515440e443d1c8dfed130ae4601ffdb52a75d7efc234278e711a6623b08cc5fe257b88475f3a28ac
7
- data.tar.gz: 631996b6b1378a5b54e2b65d2a26fe2b09614841ba09ab8a04116ea33d4bbb460d43126f647ad61015e5e64026ed5923599ae4f3e186ea6990e68ee7e1c1c862
6
+ metadata.gz: 7bc3d32c07997aca0e9748329f38fba1fddc25e4d6755bd9e8a3d38b7c77a6d24bd4710ecaeec62c22737e5c2b781a6940b5157d6ec24d90d366fde64f951975
7
+ data.tar.gz: f1d7b4560cad1e30f5d9ec3511d4dd5cd9adbe2b320f446beab11f08ae71ca862624fc4972020cb8bb15508cdbf3a5a749fe185376a822f4c01cba66f506ebf3
@@ -1,5 +1,5 @@
1
1
  module Async
2
2
  module Rails
3
- VERSION = "0.2.10"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -1,3 +1,4 @@
1
+ /*jshint onevar: false, indent:4 */
1
2
  /*global setImmediate: false, setTimeout: false, console: false */
2
3
  (function () {
3
4
 
@@ -27,6 +28,10 @@
27
28
 
28
29
  //// cross-browser compatiblity functions ////
29
30
 
31
+ var _isArray = Array.isArray || function (obj) {
32
+ return toString.call(obj) === '[object Array]';
33
+ };
34
+
30
35
  var _each = function (arr, iterator) {
31
36
  if (arr.forEach) {
32
37
  return arr.forEach(iterator);
@@ -108,19 +113,20 @@
108
113
  }
109
114
  var completed = 0;
110
115
  _each(arr, function (x) {
111
- iterator(x, only_once(function (err) {
112
- if (err) {
113
- callback(err);
114
- callback = function () {};
115
- }
116
- else {
117
- completed += 1;
118
- if (completed >= arr.length) {
119
- callback(null);
120
- }
121
- }
122
- }));
116
+ iterator(x, only_once(done) );
123
117
  });
118
+ function done(err) {
119
+ if (err) {
120
+ callback(err);
121
+ callback = function () {};
122
+ }
123
+ else {
124
+ completed += 1;
125
+ if (completed >= arr.length) {
126
+ callback(null);
127
+ }
128
+ }
129
+ }
124
130
  };
125
131
  async.forEach = async.each;
126
132
 
@@ -422,8 +428,11 @@
422
428
 
423
429
  addListener(function () {
424
430
  if (_keys(results).length === keys.length) {
425
- callback(null, results);
431
+ var theCallback = callback;
432
+ // prevent final callback from calling itself if it errors
426
433
  callback = function () {};
434
+
435
+ theCallback(null, results);
427
436
  }
428
437
  });
429
438
 
@@ -472,7 +481,7 @@
472
481
 
473
482
  async.waterfall = function (tasks, callback) {
474
483
  callback = callback || function () {};
475
- if (tasks.constructor !== Array) {
484
+ if (!_isArray(tasks)) {
476
485
  var err = new Error('First argument to waterfall must be an array of functions');
477
486
  return callback(err);
478
487
  }
@@ -505,7 +514,7 @@
505
514
 
506
515
  var _parallel = function(eachfn, tasks, callback) {
507
516
  callback = callback || function () {};
508
- if (tasks.constructor === Array) {
517
+ if (_isArray(tasks)) {
509
518
  eachfn.map(tasks, function (fn, callback) {
510
519
  if (fn) {
511
520
  fn(function (err) {
@@ -545,7 +554,7 @@
545
554
 
546
555
  async.series = function (tasks, callback) {
547
556
  callback = callback || function () {};
548
- if (tasks.constructor === Array) {
557
+ if (_isArray(tasks)) {
549
558
  async.mapSeries(tasks, function (fn, callback) {
550
559
  if (fn) {
551
560
  fn(function (err) {
@@ -633,7 +642,8 @@
633
642
  if (err) {
634
643
  return callback(err);
635
644
  }
636
- if (test()) {
645
+ var args = Array.prototype.slice.call(arguments, 1);
646
+ if (test.apply(null, args)) {
637
647
  async.doWhilst(iterator, test, callback);
638
648
  }
639
649
  else {
@@ -661,7 +671,8 @@
661
671
  if (err) {
662
672
  return callback(err);
663
673
  }
664
- if (!test()) {
674
+ var args = Array.prototype.slice.call(arguments, 1);
675
+ if (!test.apply(null, args)) {
665
676
  async.doUntil(iterator, test, callback);
666
677
  }
667
678
  else {
@@ -675,9 +686,17 @@
675
686
  concurrency = 1;
676
687
  }
677
688
  function _insert(q, data, pos, callback) {
678
- if(data.constructor !== Array) {
689
+ if (!_isArray(data)) {
679
690
  data = [data];
680
691
  }
692
+ if(data.length == 0) {
693
+ // call drain immediately if there are no tasks
694
+ return async.setImmediate(function() {
695
+ if (q.drain) {
696
+ q.drain();
697
+ }
698
+ });
699
+ }
681
700
  _each(data, function(task) {
682
701
  var item = {
683
702
  data: task,
@@ -690,7 +709,7 @@
690
709
  q.tasks.push(item);
691
710
  }
692
711
 
693
- if (q.saturated && q.tasks.length === concurrency) {
712
+ if (q.saturated && q.tasks.length === q.concurrency) {
694
713
  q.saturated();
695
714
  }
696
715
  async.setImmediate(q.process);
@@ -751,8 +770,9 @@
751
770
  saturated: null,
752
771
  empty: null,
753
772
  drain: null,
773
+ drained: true,
754
774
  push: function (data, callback) {
755
- if(data.constructor !== Array) {
775
+ if (!_isArray(data)) {
756
776
  data = [data];
757
777
  }
758
778
  _each(data, function(task) {
@@ -760,6 +780,7 @@
760
780
  data: task,
761
781
  callback: typeof callback === 'function' ? callback : null
762
782
  });
783
+ cargo.drained = false;
763
784
  if (cargo.saturated && tasks.length === payload) {
764
785
  cargo.saturated();
765
786
  }
@@ -769,13 +790,14 @@
769
790
  process: function process() {
770
791
  if (working) return;
771
792
  if (tasks.length === 0) {
772
- if(cargo.drain) cargo.drain();
793
+ if(cargo.drain && !cargo.drained) cargo.drain();
794
+ cargo.drained = true;
773
795
  return;
774
796
  }
775
797
 
776
798
  var ts = typeof payload === 'number'
777
799
  ? tasks.splice(0, payload)
778
- : tasks.splice(0);
800
+ : tasks.splice(0, tasks.length);
779
801
 
780
802
  var ds = _map(ts, function (task) {
781
803
  return task.data;
@@ -843,7 +865,9 @@
843
865
  var callback = args.pop();
844
866
  var key = hasher.apply(null, args);
845
867
  if (key in memo) {
846
- callback.apply(null, memo[key]);
868
+ async.nextTick(function () {
869
+ callback.apply(null, memo[key]);
870
+ });
847
871
  }
848
872
  else if (key in queues) {
849
873
  queues[key].push(callback);
@@ -887,8 +911,8 @@
887
911
  return async.mapSeries(counter, iterator, callback);
888
912
  };
889
913
 
890
- async.compose = function (/* functions... */) {
891
- var fns = Array.prototype.reverse.call(arguments);
914
+ async.seq = function (/* functions... */) {
915
+ var fns = arguments;
892
916
  return function () {
893
917
  var that = this;
894
918
  var args = Array.prototype.slice.call(arguments);
@@ -906,6 +930,10 @@
906
930
  };
907
931
  };
908
932
 
933
+ async.compose = function (/* functions... */) {
934
+ return async.seq.apply(null, Array.prototype.reverse.call(arguments));
935
+ };
936
+
909
937
  var _applyEach = function (eachfn, fns /*args...*/) {
910
938
  var go = function () {
911
939
  var that = this;
@@ -940,16 +968,16 @@
940
968
  next();
941
969
  };
942
970
 
971
+ // Node.js
972
+ if (typeof module !== 'undefined' && module.exports) {
973
+ module.exports = async;
974
+ }
943
975
  // AMD / RequireJS
944
- if (typeof define !== 'undefined' && define.amd) {
976
+ else if (typeof define !== 'undefined' && define.amd) {
945
977
  define([], function () {
946
978
  return async;
947
979
  });
948
980
  }
949
- // Node.js
950
- else if (typeof module !== 'undefined' && module.exports) {
951
- module.exports = async;
952
- }
953
981
  // included directly via <script> tag
954
982
  else {
955
983
  root.async = async;
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Chen