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

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Mar 6 15:54:34 EST 2008


Author: erickson
Date: 2008-03-06 15:22:01 -0500 (Thu, 06 Mar 2008)
New Revision: 1260

Modified:
   trunk/src/javascript/opensrf.js
   trunk/src/javascript/opensrf_xhr.js
Log:
added connect/disconnect support, with onconnect callback

Modified: trunk/src/javascript/opensrf.js
===================================================================
--- trunk/src/javascript/opensrf.js	2008-03-06 19:32:07 UTC (rev 1259)
+++ trunk/src/javascript/opensrf.js	2008-03-06 20:22:01 UTC (rev 1260)
@@ -61,6 +61,7 @@
 /* general session superclass */
 OpenSRF.Session = function() {
     this.remote_id = null;
+    this.state = OSRF_APP_SESSION_DISCONNECTED;
 }
 
 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR; /* default to XHR */
@@ -102,11 +103,34 @@
     this.thread = Math.random() + '' + new Date().getTime();
     this.do_connect = false;
     this.requests = [];
+    this.onconnect = null;
     OpenSRF.Session.cache[this.thread] = this;
 }
 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
 
 
+OpenSRF.ClientSession.prototype.connect = function(args) {
+    if(args && args.onconnect)
+        this.onconnect = args.onconnect;
+
+    message = new osrfMessage({
+        'threadTrace' : this.reqid, 
+        'type' : OSRF_MESSAGE_TYPE_CONNECT,
+    });
+
+    this.send(message, {'timeout' : this.timeout});
+}
+
+OpenSRF.ClientSession.prototype.disconnect = function(args) {
+    this.send(
+        new osrfMessage({
+            'threadTrace' : this.reqid, 
+            'type' : OSRF_MESSAGE_TYPE_DISCONNECT,
+        })
+    );
+}
+
+
 OpenSRF.ClientSession.prototype.request = function(args) {
 
     if(typeof args == 'string') { 
@@ -118,6 +142,9 @@
             method : args, 
             params : params
         };
+    } else {
+        if(typeof args == 'undefined')
+            args = {};
     }
 
     var req = new OpenSRF.Request(this, this.last_id++, args);
@@ -193,20 +220,30 @@
     var req = null;
 
     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
+
         var payload = osrf_msg.payload();
         var status = payload.statusCode();
         var status_text = payload.status();
 
+        if(status == OSRF_STATUS_COMPLETE) {
+            req = ses.find_request(osrf_msg.threadTrace());
+            if(req) req.complete = true;
+        }
+
+        if(status == OSRF_STATUS_OK) {
+            ses.state = OSRF_APP_SESSION_CONNECTED;
+        }
+
         if(status == OSRF_STATUS_NOTFOUND) {
-            alert('status = ' + status_text);
+            alert('NOT_FOUND: ' + status_text);
+            return;
         }
     }
 
     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
         req = ses.find_request(osrf_msg.threadTrace());
-        if(req) {
+        if(req) 
             req.response_queue.push(osrf_msg.payload());
-        }
     }
 
     if(stack_callback)
@@ -245,7 +282,7 @@
         "__p": {
             'threadTrace' : this.hash.threadTrace,
             'type' : this.hash.type,
-            'payload' : this.hash.payload.serialize(),
+            'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
             'locale' : this.hash.locale
         }
     };

Modified: trunk/src/javascript/opensrf_xhr.js
===================================================================
--- trunk/src/javascript/opensrf_xhr.js	2008-03-06 19:32:07 UTC (rev 1259)
+++ trunk/src/javascript/opensrf_xhr.js	2008-03-06 20:22:01 UTC (rev 1260)
@@ -60,14 +60,25 @@
     stat = this.xreq.status;
 
     var xhr = this;
-    var req = OpenSRF.Stack.push(
+    OpenSRF.Stack.push(
         new OpenSRF.NetMessage(null, sender, thread, json),
+
         function(ses, req) {
+            if(ses) {
+                if(ses.state == OSRF_APP_SESSION_CONNECTED && 
+                    ses.onconnect && !ses.onconnect_called) {
+                        ses.onconnect_called = true;
+                        ses.onconnect();
+                }
+            }
+
             if(req) {
                 if(req.response_queue.length > 0 && xhr.args.onresponse) 
                     return xhr.args.onresponse(req);
-                if(req.complete && xhr.args.oncomplete)
+                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