📄 engine.js
字号:
* A map of all mapped classes whose class declarations have been loaded * (dwrClassName -> constructor function) * This could have been pre-created by interface scripts, so we need to check. */ if (typeof dwr.engine['_mappedClasses'] == 'undefined') { dwr.engine._mappedClasses = {}; } /** * Find the HTTP session id sent by the web server * @private */ dwr.engine._getHttpSessionId = function() { // try to find the httpSessionId var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i]; while (cookie.charAt(0) == ' ') cookie = cookie.substring(1, cookie.length); if (cookie.indexOf(dwr.engine._sessionCookieName + "=") == 0) { return cookie.substring(dwr.engine._sessionCookieName.length + 1, cookie.length); } } return ""; }; /** A function to call if something fails. */ dwr.engine._errorHandler = dwr.engine.defaultErrorHandler; /** For debugging when something unexplained happens. */ dwr.engine._warningHandler = dwr.engine.defaultWarningHandler; /** Undocumented interceptors - do not use */ dwr.engine._postSeperator = "\n"; dwr.engine._defaultInterceptor = function(data) { return data; }; dwr.engine._urlRewriteHandler = dwr.engine._defaultInterceptor; dwr.engine._contentRewriteHandler = dwr.engine._defaultInterceptor; dwr.engine._replyRewriteHandler = dwr.engine._defaultInterceptor; /** Is this page in the process of unloading? */ dwr.engine._unloading = false; /** @private Abort any XHRs in progress at page unload (solves zombie socket problems in IE). */ dwr.engine._unloader = function() { dwr.engine._unloading = true; // Empty queue of waiting ordered requests dwr.engine._batchQueue.length = 0; // Abort any ongoing XHRs and clear their batches var batch; for (var batchId in dwr.engine._batches) { batch = dwr.engine._batches[batchId]; // Only process objects that look like batches (avoid prototype additions!) if (batch && batch.map) { if (batch.req) { batch.req.abort(); } } } // If we have used reverse ajax then we try to tell the server we are gone if (dwr.engine._isNotifyServerOnPageUnload) { dwr.engine._debug("calling unloader for: " + dwr.engine._scriptSessionId); batch = { map:{ callCount:1, 'c0-scriptName':'__System', 'c0-methodName':'pageUnloaded', 'c0-id':0 }, paramCount:0, isPoll:false, async:true, headers:{}, preHooks:[], postHooks:[], timeout:dwr.engine._timeout, errorHandler:null, warningHandler:null, textHtmlHandler:null, path:dwr.engine._pathToDwrServlet, handlers:[{ exceptionHandler:null, callback:null }] }; dwr.engine.transport.send(batch); dwr.engine._isNotifyServerOnPageUnload = false; } }; // Now register the unload handler if (!dwr.engine.isJaxerServer) { if (window.addEventListener) window.addEventListener('unload', dwr.engine._unloader, false); else if (window.attachEvent) window.attachEvent('onunload', dwr.engine._unloader); } /** * Send a request. Called by the JavaScript interface stub * @private * @param path part of URL after the host and before the exec bit without leading or trailing /s * @param scriptName The class to execute * @param methodName The method on said class to execute * @param func The callback function to which any returned data should be passed * if this is null, any returned data will be ignored * @param args The parameters to passed to the above method */ dwr.engine._execute = function(path, scriptName, methodName, args) { var singleShot = false; if (dwr.engine._batch == null) { dwr.engine.beginBatch(); singleShot = true; } var batch = dwr.engine._batch; // All the paths MUST be to the same servlet if (batch.path == null) { batch.path = path; } else { if (batch.path != path) { dwr.engine._handleError(batch, { name:"dwr.engine.multipleServlets", message:"Can't batch requests to multiple DWR Servlets." }); return; } } dwr.engine.batch.addCall(batch, scriptName, methodName, args); // Now we have finished remembering the call, we increment the call count batch.map.callCount++; if (singleShot) { return dwr.engine.endBatch(); } }; /** * Poll the server to see if there is any data waiting * @private */ dwr.engine._poll = function() { if (!dwr.engine._activeReverseAjax) { return; } var batch = dwr.engine.batch.createPoll(); dwr.engine.transport.send(batch); }; /** * Try to recover from polling errors * @param {Object} msg * @param {Object} ex */ dwr.engine._pollErrorHandler = function(msg, ex) { if (dwr.engine._pollRetries > dwr.engine._maxPollRetries) { dwr.engine._activeReverseAjax = false; dwr.engine._debug("Reverse Ajax poll failed (retries=" + dwr.engine._pollRetries + "). Giving Up: " + ex.name + " : " + ex.message); dwr.engine._debug("Giving up."); return; } var index = dwr.engine._pollRetries; if (index >= dwr.engine._retryIntervals.length) { index = dwr.engine._retryIntervals.length - 1; } dwr.engine._debug("Reverse Ajax poll failed (retries=" + dwr.engine._pollRetries + "). Trying again in " + dwr.engine._retryIntervals[index] + "s: " + ex.name + " : " + ex.message); setTimeout(dwr.engine._poll, 1000 * dwr.engine._retryIntervals[index]); dwr.engine._pollRetries++; }; /** @private This is a hack to make the context be this window */ dwr.engine._eval = function(script) { if (script == null) { return null; } if (script == "") { dwr.engine._debug("Warning: blank script", true); return null; } // dwr.engine._debug("Exec: [" + script + "]", true); return eval(script); }; /** @private call all the post hooks for a batch */ dwr.engine._callPostHooks = function(batch) { if (batch.postHooks) { for (var i = 0; i < batch.postHooks.length; i++) { batch.postHooks[i](); } batch.postHooks = null; } }; /** * Generic error handling routing to save having null checks everywhere * @private * @param {Object} batch * @param {Object} ex */ dwr.engine._handleError = function(batch, ex) { if (typeof ex == "string") ex = { name:"unknown", message:ex }; if (ex.message == null) ex.message = ""; if (ex.name == null) ex.name = "unknown"; if (batch && typeof batch.errorHandler == "function") batch.errorHandler(ex.message, ex); else if (dwr.engine._errorHandler) dwr.engine._errorHandler(ex.message, ex); if (batch) dwr.engine.batch.remove(batch); }; /** * Generic error handling routing to save having null checks everywhere * @private * @param {Object} batch * @param {Object} ex */ dwr.engine._handleWarning = function(batch, ex) { if (typeof ex == "string") ex = { name:"unknown", message:ex }; if (ex.message == null) ex.message = ""; if (ex.name == null) ex.name = "unknown"; if (batch && typeof batch.warningHandler == "function") batch.warningHandler(ex.message, ex); else if (dwr.engine._warningHandler) dwr.engine._warningHandler(ex.message, ex); if (batch) dwr.engine.batch.remove(batch); }; /** * Used internally when some message needs to get to the programmer * @private * @param {String} message * @param {Object} stacktrace */ dwr.engine._debug = function(message, stacktrace) { var written = false; try { if (window.console) { if (stacktrace && window.console.trace) window.console.trace(); window.console.log(message); written = true; } else if (window.opera && window.opera.postError) { window.opera.postError(message); written = true; } else if (window.Jaxer && Jaxer.isOnServer) { Jaxer.Log.info(message); written = true; } } catch (ex) { /* ignore */ } if (!written) { var debug = document.getElementById("dwr-debug"); if (debug) { var contents = message + "<br/>" + debug.innerHTML; if (contents.length > 2048) contents = contents.substring(0, 2048); debug.innerHTML = contents; } } }; /** * Functions called by the server */ dwr.engine.remote = { /** * Execute a callback * @private * @param {int} batchId The ID of the batch that we are replying to * @param {int} callId The call ID that the script relates to * @param {String} reply The script to execute */ handleCallback:function(batchId, callId, reply) { var batch = dwr.engine._batches[batchId]; if (batch == null) { dwr.engine._debug("Warning: batch == null in remoteHandleCallback for batchId=" + batchId, true); return; } // We store the reply in the batch so that in sync mode we can return the data batch.reply = reply; // Error handlers inside here indicate an error that is nothing to do // with DWR so we handle them differently. try { var handlers = batch.handlers[callId]; if (!handlers) { dwr.engine._debug("Warning: Missing handlers. callId=" + callId, true); } else { batch.handlers[callId].completed = true; if (typeof handlers.callback == "function") { handlers.callback.apply(handlers.callbackScope, [ reply, handlers.callbackArg ]); } } } catch (ex) { dwr.engine._handleError(batch, ex); } }, /** * Called by the server when a JavascriptFunction is executed * @param id The ID of the serialized function * @param args The arguments to pass to the function */ handleFunctionCall:function(id, args) { var func = dwr.engine.serialize.remoteFunctions[id]; func.apply(window, args); }, /** * Called by the server when a JavascriptFunction is executed * @param id The ID of the serialized function * @param args The arguments to pass to the function */ handleObjectCall:function(id, methodName, args) { var obj = dwr.engine.serialize.remoteFunctions[id]; obj[methodName].apply(obj, args); }, /** * Called by the server when a JavascriptFunction is executed * @param propertyName The ID of the serialized function * @param data The arguments to pass to the function */ handleSetCall:function(id, propertyName, data) { var obj = dwr.engine.serialize.remoteFunctions[id]; obj[propertyName] = data; }, /** * Called by the server when a JavascriptFunction is closed * @param id The ID of the serialized function */ handleFunctionClose:function(id) { delete dwr.engine.serialize.remoteFunctions[id]; }, /** * Called by the server: Handle an exception for a call * @private * @param {int} batchId The ID of the batch that we are replying to * @param {int} callId The call ID that the script relates to * @param {String} reply The script to execute */ handleException:function(batchId, callId, ex) { var batch = dwr.engine._batches[batchId]; if (batch == null) { dwr.engine._debug("Warning: null batch in remoteHandleException", true); return; } var handlers = batch.handlers[callId]; batch.handlers[callId].completed = true; if (handlers == null) { dwr.engine._debug("Warning: null handlers in remoteHandleException", true); return; } if (ex.message == undefined) { ex.message = ""; } if (typeof handlers.exceptionHandler == "function") { handlers.exceptionHandler.call(handlers.exceptionScope, ex.message, ex, handlers.exceptionArg); } else if (typeof batch.errorHandler == "function") { batch.errorHandler(ex.message, ex); } }, /** * Called by the server: The whole batch is broken * @private * @param {Object} ex The data about what broke * @param {int} batchId The ID of the batch that we are replying to */ handleBatchException:function(ex, batchId) { var searchBatch = (dwr.engine._receivedBatch == null && batchId != null); if (searchBatch) { dwr.engine._receivedBatch = dwr.engine._batches[batchId]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -