summaryrefslogtreecommitdiff
path: root/js/codeq
diff options
context:
space:
mode:
Diffstat (limited to 'js/codeq')
-rw-r--r--js/codeq/comms.js71
1 files changed, 57 insertions, 14 deletions
diff --git a/js/codeq/comms.js b/js/codeq/comms.js
index d2c2910..eece7e7 100644
--- a/js/codeq/comms.js
+++ b/js/codeq/comms.js
@@ -3,6 +3,7 @@
var activityQueue = [];
var send = function (service, json) {
+ if (json instanceof Object) json = codeq.jsonize(json);
return Q.Promise(function (resolve, reject, notify) {
$.ajax({
'type': 'POST',
@@ -24,21 +25,36 @@
};
var sendCount = 0,
+ resolveWaiters = [],
+ currentSolution = null,
+ resolveActivity = function () {
+ var i;
+ // signal everyone that the queue is flushed
+ for (i = 0; i < resolveWaiters.length; i++) {
+ try {
+ resolveWaiters[i][0]();
+ }
+ catch (e) {}
+ }
+ resolveWaiters.length = 0;
+ },
sendActivityInternal = function () {
- var request;
+ var trace;
// send max. 100 activities, do not be excessive
+ if (activityQueue.length == 0) return resolveActivity();
if (activityQueue.length > 100) {
sendCount = 100;
- request = '[' + activityQueue.slice(0, 100).join(',') + ']';
+ trace = activityQueue.slice(0, 100);
}
else {
sendCount = activityQueue.length;
- request = '[' + activityQueue.join(',') + ']';
+ trace = activityQueue;
}
- send('activity', request).then(
+ send('trace', {'trace': trace, 'solution': currentSolution}).then(
function sendActivitySuccess() {
activityQueue.splice(0, sendCount);
if (activityQueue.length > 0) sendActivityInternal();
+ else resolveActivity();
},
function sendActivityFailure() {
Q.delay(500).then(sendActivityInternal).done();
@@ -47,27 +63,54 @@
};
codeq.comms = {
- sendActivity: function commsSendActivity (json) {
- var triggerSending = activityQueue.length == 0;
- json['sid'] = codeq.sid;
- activityQueue.push(codeq.jsonize(json));
- if (triggerSending) {
- setTimeout(sendActivityInternal, 0); // async trigger: see if you can collect some more payload
- }
+ sendActivity: function commsSendActivity (json, solution) {
+ return Q.Promise(function (resolve, reject, notify) {
+ var triggerSending = activityQueue.length == 0,
+ i, js;
+ if (json instanceof Array) {
+ for (i = 0; i < json.length; i++) {
+ js = json[i];
+ js['sid'] = codeq.sid;
+ activityQueue.push(js);
+ }
+ }
+ else {
+ json['sid'] = codeq.sid;
+ activityQueue.push(json);
+ }
+ if (solution) currentSolution = solution;
+ resolveWaiters.push([resolve, reject]);
+ if (triggerSending) {
+ setTimeout(sendActivityInternal, 0); // async trigger: see if you can collect some more payload
+ }
+ });
},
sendQuery: function commsSendQuery (json) {
+ var thisTrace;
json['sid'] = codeq.sid;
- return send('query', codeq.jsonize(json));
+ if (activityQueue.length > 0) {
+ thisTrace = json['trace'];
+ if (thisTrace instanceof Array) {
+ activityQueue.splice(0, 0, 0, 0); // add two zeros in front, they will form the first two parameters to the next splice()
+ thisTrace.splice.apply(thisTrace, activityQueue);
+ }
+ else {
+ json['trace'] = activityQueue.slice();
+ }
+ activityQueue.length = 0;
+ if (currentSolution) json['solution'] = currentSolution;
+ }
+ return send('query', json);
},
getProblem: function commsGetProblem (language, problem_group, problem) {
- return send('get_problem', codeq.jsonize({
+ return send('get_problem', {
'sid': codeq.sid,
'language': language,
'problem_group': problem_group,
'problem': problem
- }));
+ });
}
};
})();