async-rails 0.8.0 → 0.9.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: 068d8675ffd8a61568db7cd9fbf23a465dab50b3
4
- data.tar.gz: 24d8779a76a34332eace9e7570c8aea6c4a75635
3
+ metadata.gz: 2f01c101f03eb8aa2d3873c859a6ef8d78ae7e2f
4
+ data.tar.gz: 949f14dfcad842183708a4c6f64d8871a9fa6ad6
5
5
  SHA512:
6
- metadata.gz: 5331b11ecd1c6291792628eca381009d2e258ae64a11b64d7cfd3df95cc08b20585853ee2220ba3f9dc8ba6b39590c1825bf38665776c51a464ed5d4299cc7a7
7
- data.tar.gz: 67307faee27128d841ac8ece9b51c043d38235f66fbb077487399527e585295d9bc758ee042f5ecb9652024b392046846ad1d3ef1c4382b22ae53494a11ba182
6
+ metadata.gz: 9271ea2e91e509f2342828f81ce0e75d2490babdc96f25e84508f00495eebb966510aa7c0d48efbb32484648d081fe98318da47f0bccdb99fdd5c9a34663a92d
7
+ data.tar.gz: 00b32fcc1057aa27f1dbc8c1166ef9df9ff1f6bab9a0fea211c242b7bd0c3ab845c7c64e3388d4a03791c6a5d120d27b66a2f3cbc739326dc13338ff8498abf7
@@ -1,5 +1,5 @@
1
1
  module Async
2
2
  module Rails
3
- VERSION = "0.8.0"
3
+ VERSION = "0.9.0"
4
4
  end
5
5
  end
@@ -832,6 +832,71 @@
832
832
  return q;
833
833
  };
834
834
 
835
+ async.priorityQueue = function (worker, concurrency) {
836
+
837
+ function _compareTasks(a, b){
838
+ return a.priority - b.priority;
839
+ };
840
+
841
+ function _binarySearch(sequence, item, compare) {
842
+ var beg = -1,
843
+ end = sequence.length - 1;
844
+ while (beg < end) {
845
+ var mid = beg + ((end - beg + 1) >>> 1);
846
+ if (compare(item, sequence[mid]) >= 0) {
847
+ beg = mid;
848
+ } else {
849
+ end = mid - 1;
850
+ }
851
+ }
852
+ return beg;
853
+ }
854
+
855
+ function _insert(q, data, priority, callback) {
856
+ if (!q.started){
857
+ q.started = true;
858
+ }
859
+ if (!_isArray(data)) {
860
+ data = [data];
861
+ }
862
+ if(data.length == 0) {
863
+ // call drain immediately if there are no tasks
864
+ return async.setImmediate(function() {
865
+ if (q.drain) {
866
+ q.drain();
867
+ }
868
+ });
869
+ }
870
+ _each(data, function(task) {
871
+ var item = {
872
+ data: task,
873
+ priority: priority,
874
+ callback: typeof callback === 'function' ? callback : null
875
+ };
876
+
877
+ q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item);
878
+
879
+ if (q.saturated && q.tasks.length === q.concurrency) {
880
+ q.saturated();
881
+ }
882
+ async.setImmediate(q.process);
883
+ });
884
+ }
885
+
886
+ // Start with a normal queue
887
+ var q = async.queue(worker, concurrency);
888
+
889
+ // Override push to accept second parameter representing priority
890
+ q.push = function (data, priority, callback) {
891
+ _insert(q, data, priority, callback);
892
+ };
893
+
894
+ // Remove unshift function
895
+ delete q.unshift;
896
+
897
+ return q;
898
+ };
899
+
835
900
  async.cargo = function (worker, payload) {
836
901
  var working = false,
837
902
  tasks = [];
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.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Chen