📄 engine.js
字号:
DWREngine._removeSerializeFunctions();
// Now we have finished remembering the call, we incr the call count
DWREngine._batch.map.callCount++;
if (singleShot) {
DWREngine.endBatch();
}
};
/**
* @private Actually send the block of data in the batch object.
*/
DWREngine._sendData = function(batch) {
// If the batch is empty, don't send anything
if (batch.map.callCount == 0) return;
// Call any pre-hooks
for (var i = 0; i < batch.preHooks.length; i++) {
batch.preHooks[i]();
}
batch.preHooks = null;
// Set a timeout
if (batch.timeout && batch.timeout != 0) {
batch.interval = setInterval(function() {
clearInterval(batch.interval);
DWREngine._abortRequest(batch);
}, batch.timeout);
}
// A quick string to help people that use web log analysers
var statsInfo;
if (batch.map.callCount == 1) {
statsInfo = batch.map["c0-scriptName"] + "." + batch.map["c0-methodName"] + ".dwr";
}
else {
statsInfo = "Multiple." + batch.map.callCount + ".dwr";
}
// Get setup for XMLHttpRequest if possible
if (batch.method == DWREngine.XMLHttpRequest) {
if (window.XMLHttpRequest) {
batch.req = new XMLHttpRequest();
}
// IE5 for the mac claims to support window.ActiveXObject, but throws an error when it's used
else if (window.ActiveXObject && !(navigator.userAgent.indexOf('Mac') >= 0 && navigator.userAgent.indexOf("MSIE") >= 0)) {
batch.req = DWREngine._newActiveXObject(DWREngine._XMLHTTP);
}
}
var query = "";
var prop;
if (batch.req) {
batch.map.xml = "true";
// Proceed using XMLHttpRequest
if (batch.async) {
batch.req.onreadystatechange = function() {
DWREngine._stateChange(batch);
};
}
// Workaround for Safari 1.x POST bug
var indexSafari = navigator.userAgent.indexOf('Safari/');
if (indexSafari >= 0) {
// So this is Safari, are we on 1.x? POST is broken
var version = navigator.userAgent.substring(indexSafari + 7);
var verNum = parseInt(version, 10);
if (verNum < 400) {
batch.verb == "GET";
}
// else if (verNum <= 417) {
// batch.map.isBrokenSafari2 = "true";
// }
}
if (batch.verb == "GET") {
// Some browsers (Opera/Safari2) seem to fail to convert the value
// of batch.map.callCount to a string in the loop below so we do it
// manually here.
batch.map.callCount = "" + batch.map.callCount;
for (prop in batch.map) {
var qkey = encodeURIComponent(prop);
var qval = encodeURIComponent(batch.map[prop]);
if (qval == "") {
if (DWREngine._warningHandler) {
DWREngine._warningHandler("Found empty qval for qkey=" + qkey);
}
}
query += qkey + "=" + qval + "&";
}
query = query.substring(0, query.length - 1);
try {
batch.req.open("GET", batch.path + "/exec/" + statsInfo + "?" + query, batch.async);
batch.req.send(null);
if (!batch.async) {
DWREngine._stateChange(batch);
}
}
catch (ex) {
DWREngine._handleMetaDataError(null, ex);
}
}
else {
for (prop in batch.map) {
if (typeof batch.map[prop] != "function") {
query += prop + "=" + batch.map[prop] + "\n";
}
}
try {
// This might include Safari too, but it shouldn't do any harm
// if (navigator.userAgent.indexOf('Gecko') >= 0) {
// batch.req.setRequestHeader('Connection', 'close');
// }
batch.req.open("POST", batch.path + "/exec/" + statsInfo, batch.async);
batch.req.setRequestHeader('Content-Type', 'text/plain');
batch.req.send(query);
if (!batch.async) {
DWREngine._stateChange(batch);
}
}
catch (ex) {
DWREngine._handleMetaDataError(null, ex);
}
}
}
else {
batch.map.xml = "false";
var idname = "dwr-if-" + batch.map["c0-id"];
// Proceed using iframe
batch.div = document.createElement('div');
batch.div.innerHTML = "<iframe src='javascript:void(0)' frameborder='0' width='0' height='0' id='" + idname + "' name='" + idname + "'></iframe>";
document.body.appendChild(batch.div);
batch.iframe = document.getElementById(idname);
batch.iframe.setAttribute('style', 'width:0px; height:0px; border:0px;');
if (batch.verb == "GET") {
for (prop in batch.map) {
if (typeof batch.map[prop] != "function") {
query += encodeURIComponent(prop) + "=" + encodeURIComponent(batch.map[prop]) + "&";
}
}
query = query.substring(0, query.length - 1);
batch.iframe.setAttribute('src', batch.path + "/exec/" + statsInfo + "?" + query);
document.body.appendChild(batch.iframe);
}
else {
batch.form = document.createElement('form');
batch.form.setAttribute('id', 'dwr-form');
batch.form.setAttribute('action', batch.path + "/exec" + statsInfo);
batch.form.setAttribute('target', idname);
batch.form.target = idname;
batch.form.setAttribute('method', 'post');
for (prop in batch.map) {
var formInput = document.createElement('input');
formInput.setAttribute('type', 'hidden');
formInput.setAttribute('name', prop);
formInput.setAttribute('value', batch.map[prop]);
batch.form.appendChild(formInput);
}
document.body.appendChild(batch.form);
batch.form.submit();
}
}
};
/**
* @private Called by XMLHttpRequest to indicate that something has happened
*/
DWREngine._stateChange = function(batch) {
if (!batch.completed && batch.req.readyState == 4) {
try {
var reply = batch.req.responseText;
var status = batch.req.status;
if (reply == null || reply == "") {
DWREngine._handleMetaDataError(null, "No data received from server");
return;
}
// This should get us out of 404s etc.
if (reply.search("DWREngine._handle") == -1) {
DWREngine._handleMetaDataError(null, "Invalid reply from server");
return;
}
if (status != 200) {
if (reply == null) reply = "Unknown error occured";
DWREngine._handleMetaDataError(null, reply);
return;
}
eval(reply);
// We're done. Clear up
DWREngine._clearUp(batch);
}
catch (ex) {
if (ex == null) ex = "Unknown error occured";
DWREngine._handleMetaDataError(null, ex);
}
finally {
// If there is anything on the queue waiting to go out, then send it.
// We don't need to check for ordered mode, here because when ordered mode
// gets turned off, we still process *waiting* batches in an ordered way.
if (DWREngine._batchQueue.length != 0) {
var sendbatch = DWREngine._batchQueue.shift();
DWREngine._sendData(sendbatch);
DWREngine._batches[DWREngine._batches.length] = sendbatch;
}
}
}
};
/**
* @private Called by reply scripts generated as a result of remote requests
* @param id The identifier of the call that we are handling a response for
* @param reply The data to pass to the callback function
*/
DWREngine._handleResponse = function(id, reply) {
// Clear this callback out of the list - we don't need it any more
var handlers = DWREngine._handlersMap[id];
DWREngine._handlersMap[id] = null;
// TODO: How can we do this - delete DWREngine._handlersMap.id
if (handlers) {
// Error handlers inside here indicate an error that is nothing to do
// with DWR so we handle them differently.
try {
if (handlers.callback) handlers.callback(reply);
}
catch (ex) {
DWREngine._handleMetaDataError(handlers, ex);
}
}
// Finalize the call for IFrame transport
if (DWREngine._method == DWREngine.IFrame) {
var responseBatch = DWREngine._batches[DWREngine._batches.length-1];
// Only finalize after the last call has been handled
if (responseBatch.map["c"+(responseBatch.map.callCount-1)+"-id"] == id) {
DWREngine._clearUp(responseBatch);
}
}
};
/**
* @private This method is called by Javascript that is emitted by server
*/
DWREngine._handleServerError = function(id, error) {
// Clear this callback out of the list - we don't need it any more
var handlers = DWREngine._handlersMap[id];
DWREngine._handlersMap[id] = null;
if (error.message) {
DWREngine._handleMetaDataError(handlers, error.message, error);
}
else {
DWREngine._handleMetaDataError(handlers, error);
}
};
/**
* @private Called as a result of a request timeout
*/
DWREngine._abortRequest = function(batch) {
if (batch && batch.metadata != null && !batch.completed) {
DWREngine._clearUp(batch);
if (batch.req) batch.req.abort();
// Call all the timeout errorHandlers
var handlers;
var id;
for (var i = 0; i < batch.ids.length; i++) {
id = batch.ids[i];
handlers = DWREngine._handlersMap[id];
DWREngine._handleMetaDataError(handlers, "Timeout");
}
}
};
/**
* @private A call has finished by whatever means and we need to shut it all down.
*/
DWREngine._clearUp = function(batch) {
if (batch.completed) {
alert("double complete");
return;
}
// IFrame tidyup
if (batch.div) batch.div.parentNode.removeChild(batch.div);
if (batch.iframe) batch.iframe.parentNode.removeChild(batch.iframe);
if (batch.form) batch.form.parentNode.removeChild(batch.form);
// XHR tidyup: avoid IE handles increase
if (batch.req) delete batch.req;
for (var i = 0; i < batch.postHooks.length; i++) {
batch.postHooks[i]();
}
batch.postHooks = null;
// TODO: There must be a better way???
for (var i = 0; i < DWREngine._batches.length; i++) {
if (DWREngine._batches[i] == batch) {
DWREngine._batches.splice(i, 1);
break;
}
}
batch.completed = true;
};
/**
* @private Generic error handling routing to save having null checks everywhere.
*/
DWREngine._handleError = function(reason, ex) {
if (DWREngine._errorHandler) {
DWREngine._errorHandler(reason, ex);
}
};
/**
* @private Generic error handling routing to save having null checks everywhere.
*/
DWREngine._handleMetaDataError = function(handlers, reason, ex) {
if (handlers && typeof handlers.errorHandler == "function") {
handlers.errorHandler(reason, ex);
}
else {
DWREngine._handleError(reason, ex);
}
};
/**
* @private Hack a polymorphic dwrSerialize() function on all basic types. Yeulch
*/
DWREngine._addSerializeFunctions = function() {
Object.prototype.dwrSerialize = DWREngine._serializeObject;
Array.prototype.dwrSerialize = DWREngine._serializeArray;
Boolean.prototype.dwrSerialize = DWREngine._serializeBoolean;
Number.prototype.dwrSerialize = DWREngine._serializeNumber;
String.prototype.dwrSerialize = DWREngine._serializeString;
Date.prototype.dwrSerialize = DWREngine._serializeDate;
};
/**
* @private Remove the hacked polymorphic dwrSerialize() function on all basic types.
*/
DWREngine._removeSerializeFunctions = function() {
delete Object.prototype.dwrSerialize;
delete Array.prototype.dwrSerialize;
delete Boolean.prototype.dwrSerialize;
delete Number.prototype.dwrSerialize;
delete String.prototype.dwrSerialize;
delete Date.prototype.dwrSerialize;
};
/**
* @private Marshall a data item
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -