[Opensrf-commits] r1262 - trunk/src/javascript

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Mar 6 17:04:59 EST 2008


Author: erickson
Date: 2008-03-06 16:32:27 -0500 (Thu, 06 Mar 2008)
New Revision: 1262

Modified:
   trunk/src/javascript/opensrf.js
   trunk/src/javascript/opensrf_xhr.js
Log:
added support for blocking connect and request calls

Modified: trunk/src/javascript/opensrf.js
===================================================================
--- trunk/src/javascript/opensrf.js	2008-03-06 21:32:11 UTC (rev 1261)
+++ trunk/src/javascript/opensrf.js	2008-03-06 21:32:27 UTC (rev 1262)
@@ -74,6 +74,7 @@
 }
 
 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
+    args = (args) ? args : {};
     switch(OpenSRF.Session.transport) {
         case OSRF_TRANSPORT_TYPE_XHR:
             return this.send_xhr(osrf_msg, args);
@@ -101,7 +102,6 @@
     this.locale = 'en-US';
     this.last_id = 0;
     this.thread = Math.random() + '' + new Date().getTime();
-    this.do_connect = false;
     this.requests = [];
     this.onconnect = null;
     OpenSRF.Session.cache[this.thread] = this;
@@ -110,15 +110,25 @@
 
 
 OpenSRF.ClientSession.prototype.connect = function(args) {
-    if(args && args.onconnect)
+    args = (args) ? args : {};
+
+    if(args.onconnect)
         this.onconnect = args.onconnect;
 
+    /* if no handler is provided, make this a synchronous call */
+    if(!this.onconnect) 
+        this.timeout = (args.timeout) ? args.timeout : 5;
+
     message = new osrfMessage({
         'threadTrace' : this.reqid, 
         'type' : OSRF_MESSAGE_TYPE_CONNECT,
     });
 
     this.send(message, {'timeout' : this.timeout});
+
+    if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
+        return true;
+    return false;
 }
 
 OpenSRF.ClientSession.prototype.disconnect = function(args) {
@@ -177,6 +187,7 @@
 OpenSRF.Request.prototype.recv = function(timeout) {
     if(this.response_queue.length > 0)
         return this.response_queue.shift();
+    return null;
 }
 
 OpenSRF.Request.prototype.send = function() {

Modified: trunk/src/javascript/opensrf_xhr.js
===================================================================
--- trunk/src/javascript/opensrf_xhr.js	2008-03-06 21:32:11 UTC (rev 1261)
+++ trunk/src/javascript/opensrf_xhr.js	2008-03-06 21:32:27 UTC (rev 1262)
@@ -34,14 +34,33 @@
     var xhr_req = this;
     var xreq = this.xreq
 
-    xreq.multipart = true; /* XXX browser check */
-    xreq.onload = function(evt) {xhr_req.core_handler(evt.target);}
-    xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
+    if(this.args.timeout) {
+        /* this is a standard blocking (non-multipart) call */
+        xreq.open('POST', OSRF_HTTP_TRANSLATOR, false);
 
+    } else {
+
+        if( /* XXX browser != mozilla */ false ) {
+
+            /* standard asynchronous call */
+            xreq.onreadystatechange = function() {
+                if(xreq.readyState == 4)
+                    xhr_req.core_handler();
+            }
+            xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
+
+        } else {
+
+            /* asynchronous multipart call */
+            xreq.multipart = true;
+            xreq.onload = function(evt) {xhr_req.core_handler();}
+            xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
+            xreq.setRequestHeader(OSRF_HTTP_HEADER_MULTIPART, 'true');
+        }
+    }
+
     xreq.setRequestHeader('Content-Type', OSRF_POST_CONTENT_TYPE);
-    xreq.setRequestHeader(OSRF_HTTP_HEADER_MULTIPART, 'true');
     xreq.setRequestHeader(OSRF_HTTP_HEADER_THREAD, this.args.thread);
-
     if(this.args.rcpt)
         xreq.setRequestHeader(OSRF_HTTP_HEADER_TO, this.args.rcpt);
     else
@@ -50,9 +69,13 @@
     var post = 'osrf-msg=' + encodeURIComponent(js2JSON([this.message.serialize()]));
     xreq.send(post);
 
+    if(this.args.timeout) /* this was a blocking call, manually run the handler */
+        this.core_handler()
+
     return this;
 }
 
+
 OpenSRF.XHRequest.prototype.core_handler = function() {
     sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
     thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
@@ -75,6 +98,7 @@
             if(req) {
                 if(req.response_queue.length > 0 && xhr.args.onresponse) 
                     return xhr.args.onresponse(req);
+
                 if(req.complete && xhr.args.oncomplete && !xhr.args.oncomplete_called) {
                     xhr.args.oncomplete_called = true;
                     return xhr.args.oncomplete(req);



More information about the opensrf-commits mailing list