[Opensrf-commits] r1290 - trunk/src/javascript
svn at svn.open-ils.org
svn at svn.open-ils.org
Mon Mar 24 14:09:47 EDT 2008
Author: erickson
Date: 2008-03-24 13:34:44 -0400 (Mon, 24 Mar 2008)
New Revision: 1290
Modified:
trunk/src/javascript/opensrf.js
trunk/src/javascript/opensrf_xhr.js
Log:
implemented onerror, onmethoderror and ontransporterror. moved the callbacks into the main stack for clearer processing
Modified: trunk/src/javascript/opensrf.js
===================================================================
--- trunk/src/javascript/opensrf.js 2008-03-19 17:13:48 UTC (rev 1289)
+++ trunk/src/javascript/opensrf.js 2008-03-24 17:34:44 UTC (rev 1290)
@@ -174,9 +174,14 @@
OpenSRF.Request = function(session, reqid, args) {
this.session = session;
this.reqid = reqid;
+
+ /* callbacks */
this.onresponse = args.onresponse;
+ this.oncomplete = args.oncomplete;
this.onerror = args.onerror;
- this.oncomplete = args.oncomplete;
+ this.onmethoderror = args.onmethoderror;
+ this.ontransporterror = args.ontransporterror;
+
this.method = args.method;
this.params = args.params;
this.timeout = args.timeout;
@@ -203,7 +208,9 @@
'timeout' : this.timeout,
'onresponse' : this.onresponse,
'oncomplete' : this.oncomplete,
- 'onerror' : this.onerror
+ 'onerror' : this.onerror,
+ 'onmethoderror' : this.onmethoderror,
+ 'ontransporterror' : this.ontransporterror
});
}
@@ -217,16 +224,16 @@
OpenSRF.Stack = function() {
}
-OpenSRF.Stack.push = function(net_msg, stack_callback) {
+OpenSRF.Stack.push = function(net_msg, callbacks) {
var ses = OpenSRF.Session.find_session(net_msg.thread);
if(!ses) return;
ses.remote_id = net_msg.sender;
osrf_msgs = JSON2js(net_msg.body);
for(var i = 0; i < osrf_msgs.length; i++)
- OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);
+ OpenSRF.Stack.handle_message(ses, osrf_msgs[i], callbacks);
}
-OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
+OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
var req = null;
@@ -238,27 +245,40 @@
if(status == OSRF_STATUS_COMPLETE) {
req = ses.find_request(osrf_msg.threadTrace());
- if(req) req.complete = true;
+ if(req) {
+ req.complete = true;
+ if(callbacks.oncomplete && !req.oncomplete_called) {
+ req.oncomplete_called = true;
+ return callbacks.oncomplete(req);
+ }
+ }
}
if(status == OSRF_STATUS_OK) {
ses.state = OSRF_APP_SESSION_CONNECTED;
+
+ /* call the connect callback */
+ if(ses.onconnect && !ses.onconnect_called) {
+ ses.onconnect_called = true;
+ return ses.onconnect();
+ }
}
if(status == OSRF_STATUS_NOTFOUND) {
- alert('NOT_FOUND: ' + status_text);
- return;
+ req = ses.find_request(osrf_msg.threadTrace());
+ if(callbacks.onmethoderror)
+ return callbacks.onmethoderror(req, status, status_text);
}
}
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(callbacks.onresponse)
+ return callbacks.onresponse(req);
+ }
}
-
- if(stack_callback)
- stack_callback(ses, req);
}
/* The following classes map directly to network-serializable opensrf objects */
Modified: trunk/src/javascript/opensrf_xhr.js
===================================================================
--- trunk/src/javascript/opensrf_xhr.js 2008-03-19 17:13:48 UTC (rev 1289)
+++ trunk/src/javascript/opensrf_xhr.js 2008-03-24 17:34:44 UTC (rev 1290)
@@ -75,36 +75,42 @@
return this;
}
-
OpenSRF.XHRequest.prototype.core_handler = function() {
sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
json = this.xreq.responseText;
stat = this.xreq.status;
- var xhr = this;
+ if(stat >= 400)
+ return this.transport_error_handler();
+
OpenSRF.Stack.push(
new OpenSRF.NetMessage(null, sender, thread, json),
+ {
+ onresponse : this.args.onresponse,
+ oncomplete : this.args.oncomplete,
+ onerror : this.args.onerror,
+ onmethoderror : this.method_error_handler()
+ }
+ );
+}
- 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);
+OpenSRF.XHRequest.prototype.method_error_handler = function() {
+ var xhr = this;
+ return function(req, status, status_text) {
+ if(xhr.args.onmethoderror)
+ xhr.args.onmethoderror(req, status, status_text);
+ if(xhr.args.onerror)
+ xhr.args.onerror(xhr.message, xhr.args.rcpt || xhr.args.rcpt_service, xhr.args.thread);
+ }
+}
- if(req.complete && xhr.args.oncomplete && !xhr.args.oncomplete_called) {
- xhr.args.oncomplete_called = true;
- return xhr.args.oncomplete(req);
- }
- }
- }
- );
+OpenSRF.XHRequest.prototype.transport_error_handler = function() {
+ if(this.args.ontransporterror)
+ this.args.ontransporterror(this.xreq);
+ if(this.args.onerror)
+ this.args.onerror(this.message, this.args.rcpt || this.args.rcpt_service, this.args.thread);
}
+
More information about the opensrf-commits
mailing list