You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by re...@apache.org on 2020/02/26 11:37:37 UTC

[wicket] 01/01: [WICKET-6750] add a request monitor class that can be used operate in on AJAX request.

This is an automated email from the ASF dual-hosted git repository.

reiern70 pushed a commit to branch feature/reiern70/WICKET-6750-aborting-ajax-requests-take-II
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit c569f7129554720a0bf66664cb4c9c5983d502f5
Author: reiern70 <re...@gmail.com>
AuthorDate: Wed Feb 26 13:36:57 2020 +0200

    [WICKET-6750] add a request monitor class that can be used operate in on AJAX request.
---
 .../wicket/ajax/res/js/wicket-ajax-jquery.js       | 122 ++++++++++-----------
 .../examples/ajax/builtin/FileUploadPage.java      |   7 +-
 2 files changed, 62 insertions(+), 67 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
index 8a66ad7..91125f1 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
@@ -278,20 +278,6 @@
 			}
 		},
 
-		// aborts current request is there is any running
-		abort: function () {
-			if (isUndef(this.jqXHR)) {
-				Wicket.Log.debug("There is no executing request for channel " + this.name)
-			} else {
-				// if request has not finished yet, then abort it
-				if (this.jqXHR.readyState !== 4) {
-					this.jqXHR.abort();
-				} else {
-					Wicket.Log.debug("There is no executing request for channel " + this.name)
-				}
-			}
-		},
-
 		done: function () {
 			var callback = null;
 
@@ -320,8 +306,8 @@
 			this.channels = {};
 		},
 
-		// creates or retirves a channel
-		_createOrGetChanel: function (channel) {
+		// Schedules the callback to channel with given name.
+		schedule: function (channel, callback) {
 			var parsed = new Wicket.Channel(channel);
 			var c = this.channels[parsed.name];
 			if (isUndef(c)) {
@@ -330,22 +316,7 @@
 			} else {
 				c.type = parsed.type;
 			}
-			return c;
-		},
-
-		// Schedules the callback to channel with given name.
-		schedule: function (channel, callback) {
-			return this._createOrGetChanel(channel).schedule(callback);
-		},
-
-		// registers current jqXHR with channel using it
-		registerCurrentAjaxRequest: function(channel, jqXHR) {
-			this._createOrGetChanel(channel).jqXHR = jqXHR;
-		},
-
-		// aborts the current request for channel with name channel
-		abortCurrentRequestForChannel: function(channel) {
-			this._createOrGetChanel(channel).abort();
+			return c.schedule(callback);
 		},
 
 		// Tells the ChannelManager that the current callback in channel with given name
@@ -365,6 +336,58 @@
 	Wicket.ChannelManager.FunctionsExecuter = FunctionsExecuter;
 
 	/**
+	 * Channel manager maintains a map of channels.
+	 */
+	Wicket.AjaxRequestMonitor = Wicket.Class.create();
+
+	Wicket.AjaxRequestMonitor.prototype = {
+		initialize: function () {
+			this.requests = [];
+		},
+
+		init: function() {
+			var requests = this.requests;
+			Wicket.Event.subscribe('/ajax/call/beforeSend',
+				function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
+					requests [attributes.ch] = jqXHR;
+				}
+			);
+
+			Wicket.Event.subscribe('/ajax/call/complete',
+				function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
+					delete requests[attributes.ch];
+				}
+			);
+		},
+
+		getReuest: function (channel) {
+			var channelName = channel;
+			// (ajax channel)
+			if (typeof (channelName) !== 'string') {
+				channelName = '0|s';
+			}
+			return this.requests[channelName];
+
+		},
+
+		abortRequest: function (channel) {
+			var channelName = channel;
+			// (ajax channel)
+			if (typeof(channelName) !== 'string') {
+				channelName = '0|s';
+			}
+			var jqXHR = getReuest(channelName);
+			if (jqXHR) {
+				try {
+					jqXHR.abort();
+				} catch (exception) {
+					Wicket.Log.error("Couldn't abort current AJAX request:", exception);
+				}
+			}
+		}
+	};
+
+	/**
 	 * The Ajax.Request class encapsulates a XmlHttpRequest.
 	 */
 	Wicket.Ajax = {};
@@ -576,6 +599,7 @@
 		 */
 		ajax: function (attrs) {
 			this._initializeDefaults(attrs);
+
 			var res = Wicket.channelManager.schedule(attrs.ch, Wicket.bind(function () {
 				this.doAjax(attrs);
 			}, this));
@@ -583,32 +607,6 @@
 		},
 
 		/**
-		 * Aborts current AJAX request, if any is running, for channel @channel.
-		 *
-		 * WARNING! Mind that this does not implies the server will immediately know about
-		 * request being aborted. And there is no reliable way to tell server to stop process
-		 * started by halted request. Thus server side processing of request and, by consequence,
-		 * Wicket's lock on page night continue. Therefore any following AJAX requests might need to wait at
-		 * server side for lock on page to be released. Nevertheless, this method might prove useful, for instance,
-		 * in order to abort/halt big AJAX uploads.
-		 *
-		 * TODO: implement some kind of client side mechanism + server side counterpart allowing to tell server
-		 * please discard/request + release page lock?
-		 *
-		 * @param {String} channel - the name of the channel. If no parameter is provided then default channel is
-		 *        assumed.
-		 */
-		abortRequest: function (channel) {
-			var attr = {};
-			if (isUndef(channel) || typeof(channel) !== 'string') {
-				this._initializeDefaults(attr);
-			} else {
-				attr.ch = channel;
-			}
-			Wicket.channelManager.abortCurrentRequestForChannel(attr.ch);
-		},
-
-		/**
 		 * Is an element still present for Ajax requests. 
 		 */
 		_isPresent: function(id) {
@@ -750,9 +748,6 @@
 				contentType: wwwFormUrlEncoded,
 				
 				beforeSend: function (jqXHR, settings) {
-					if (attrs.async) {
-						Wicket.channelManager.registerCurrentAjaxRequest(attrs.ch, jqXHR);
-					}
 					self._executeHandlers(attrs.bsh, attrs, jqXHR, settings);
 					we.publish(topic.AJAX_CALL_BEFORE_SEND, attrs, jqXHR, settings);
 
@@ -1702,11 +1697,6 @@
 
 				return Wicket.Ajax.ajax(attrs);
 			},
-			// aborts an AJAX request
-			abortRequest: function(channel) {
-				var call = new Wicket.Ajax.Call();
-				call.abortRequest(channel);
-			},
 
 			ajax: function(attrs) {
 
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
index f7ace30..448537f 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
@@ -136,7 +136,7 @@ public class FileUploadPage extends BasePage
 			@Override
 			public void renderHead(IHeaderResponse response)
 			{
-				String script = "$('#" + cancelUpload.getMarkupId() +"').on('click', function() { Wicket.Ajax.abortRequest(); });";
+				String script = "$('#" + cancelUpload.getMarkupId() +"').on('click', function() { window.requestMonitor.abortRequest(); });";
 				response.render(OnDomReadyHeaderItem.forScript(script));
 			}
 		};
@@ -173,6 +173,11 @@ public class FileUploadPage extends BasePage
 		add(drop);
 	}
 
+	@Override
+	public void renderHead(IHeaderResponse response) {
+		response.render(OnDomReadyHeaderItem.forScript("window.requestMonitor = new Wicket.AjaxRequestMonitor(); window.requestMonitor.init();"));
+	}
+
 	private String showCancelUploadButtonScript() {
 		return "$('#" + cancelUpload.getMarkupId() + "').show();";
 	}