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 +4 -4
- data/lib/async-rails/version.rb +1 -1
- data/vendor/assets/javascripts/async.js +65 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f01c101f03eb8aa2d3873c859a6ef8d78ae7e2f
|
4
|
+
data.tar.gz: 949f14dfcad842183708a4c6f64d8871a9fa6ad6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9271ea2e91e509f2342828f81ce0e75d2490babdc96f25e84508f00495eebb966510aa7c0d48efbb32484648d081fe98318da47f0bccdb99fdd5c9a34663a92d
|
7
|
+
data.tar.gz: 00b32fcc1057aa27f1dbc8c1166ef9df9ff1f6bab9a0fea211c242b7bd0c3ab845c7c64e3388d4a03791c6a5d120d27b66a2f3cbc739326dc13338ff8498abf7
|
data/lib/async-rails/version.rb
CHANGED
@@ -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 = [];
|