summaryrefslogtreecommitdiff
path: root/js/codeq/comms.js
diff options
context:
space:
mode:
authorAleš Smodiš <aless@guru.si>2015-08-24 19:17:43 +0200
committerAleš Smodiš <aless@guru.si>2015-08-24 19:17:43 +0200
commit21d213dcff1367c16dc0c3f6585b8e35d7c2f0c7 (patch)
treeea7164cde0b4ad7ab42b94b291f690813c22b65b /js/codeq/comms.js
parent42ee67384fa52bc3db4716ffb9aff2cd0dda3f5e (diff)
Introduced the Q promises library, created a basic login page to test prolog examples, and started tying the terminal and editor activity to the REST services.
Diffstat (limited to 'js/codeq/comms.js')
-rw-r--r--js/codeq/comms.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/js/codeq/comms.js b/js/codeq/comms.js
new file mode 100644
index 0000000..fb5d348
--- /dev/null
+++ b/js/codeq/comms.js
@@ -0,0 +1,64 @@
+(function () {
+
+ var activityQueue = [];
+
+ var send = function (service, json) {
+ return Q.Promise(function (resolve, reject, notify) {
+ $.ajax({
+ 'type': 'POST',
+ 'url': codeq.urlPrefix + service,
+ 'accepts': 'application/json',
+ 'contentType': 'application/json; charset=UTF-8', // type of our request
+ 'data': json,
+ 'processData': false, // don't process outgoing data
+ 'dataType': 'json', // expected type of the response
+ 'timeout': 60000, // one minute
+ 'error': function sendErrorHandler(jqXHR, textStatus, errorThrown) {
+ reject(new Error(errorThrown || textStatus));
+ },
+ 'success': function sendSuccessHandler(data, textStatus, jqXHR) {
+ resolve(data);
+ }
+ });
+ });
+ };
+
+ var sendCount = 0,
+ sendActivityInternal = function () {
+ var request;
+ // send max. 100 activities, do not be excessive
+ if (activityQueue.length > 100) {
+ sendCount = 100;
+ request = '[' + activityQueue.slice(0, 100).join(',') + ']';
+ }
+ else {
+ sendCount = activityQueue.length;
+ request = '[' + activityQueue.join(',') + ']';
+ }
+ send('activity', request).then(
+ function sendActivitySuccess() {
+ activityQueue.splice(0, sendCount);
+ if (activityQueue.length > 0) sendActivityInternal();
+ },
+ function sendActivityFailure() {
+ Q.delay(500).then(sendActivityInternal).done();
+ }
+ ).done();
+ };
+
+ 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
+ }
+ },
+
+ sendQuery: function commsSendQuery (json) {
+ json['sid'] = codeq.sid;
+ return send('query', codeq.jsonize(json));
+ }
+ };
+})();