From e3f73003336720a26bd3c9f4e75a9e83a8128524 Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 6 Mar 2008 21:32:27 +0000 Subject: [PATCH] added support for blocking connect and request calls git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1262 9efc2488-bf62-4759-914b-345cdb29e865 --- src/javascript/opensrf.js | 15 +++++++++++++-- src/javascript/opensrf_xhr.js | 34 +++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/javascript/opensrf.js b/src/javascript/opensrf.js index af19873..3f5cb0f 100644 --- a/src/javascript/opensrf.js +++ b/src/javascript/opensrf.js @@ -74,6 +74,7 @@ OpenSRF.Session.prototype.cleanup = function() { } 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 @@ OpenSRF.ClientSession = function(service) { 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.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session'); 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 = function(session, reqid, args) { OpenSRF.Request.prototype.recv = function(timeout) { if(this.response_queue.length > 0) return this.response_queue.shift(); + return null; } OpenSRF.Request.prototype.send = function() { diff --git a/src/javascript/opensrf_xhr.js b/src/javascript/opensrf_xhr.js index 0ea7f55..d10f885 100644 --- a/src/javascript/opensrf_xhr.js +++ b/src/javascript/opensrf_xhr.js @@ -34,14 +34,33 @@ OpenSRF.XHRequest.prototype.send = function() { 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 @@ OpenSRF.XHRequest.prototype.send = function() { 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 @@ OpenSRF.XHRequest.prototype.core_handler = function() { 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); -- 2.43.2