This is an automated email from the ASF dual-hosted git repository.
harbs pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git
The following commit(s) were added to refs/heads/develop by this push:
new 855b294 Beefed up AsyncTask Added HTTP request tasks
855b294 is described below
commit 855b294c0d2766c6da4f73be88c5bf150d3d7dc3
Author: Harbs <[email protected]>
AuthorDate: Sun Oct 24 12:52:27 2021 +0300
Beefed up AsyncTask
Added HTTP request tasks
---
.../org/apache/royale/utils/async/AsyncTask.as | 59 ++++++++-
.../Network/src/main/royale/NetworkClasses.as | 4 +
.../apache/royale/utils/async/HttpDownloadTask.as | 81 ++++++++++++
.../apache/royale/utils/async/HttpRequestTask.as | 141 +++++++++++++++++++++
.../apache/royale/utils/async/HttpUploadTask.as | 96 ++++++++++++++
5 files changed, 375 insertions(+), 6 deletions(-)
diff --git
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/async/AsyncTask.as
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/async/AsyncTask.as
index 44a31ce..e54178e 100644
---
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/async/AsyncTask.as
+++
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/async/AsyncTask.as
@@ -117,12 +117,12 @@ package org.apache.royale.utils.async
}
protected function notifyDone():void{
dispatchEvent(new Event("done"));
- if(!doneCallbacks){
- return;
- }
- for(var i:int=0;i<doneCallbacks.length;i++){
- doneCallbacks[i](this);
+ if(doneCallbacks){
+ for(var i:int=0;i<doneCallbacks.length;i++){
+ doneCallbacks[i](this);
+ }
}
+ destroy();
}
private var doneCallbacks:Array;
@@ -175,6 +175,53 @@ package org.apache.royale.utils.async
{
_data = value;
}
+ /**
+ * Keep references to event listeners for automatic cleanup
+ */
+ COMPILE::JS
+ override public function addEventListener(type:String, handler:Function,
useCapture:Boolean = false, scope:Object = null):void
+ {
+ super.addEventListener(type,handler,useCapture,scope);
+ if(!listeners)
+ {
+ listeners = [];
+ }
+ listeners.push({
+ type:type,
+ handler:handler,
+ useCapture:useCapture
+ });
+ }
+ COMPILE::SWF
+ override public function addEventListener(type:String, listener:Function,
useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean =
false):void
+ {
+ if(!listeners)
+ {
+ listeners = [];
+ }
+ listeners.push({
+ type:type,
+ handler:listener,
+ useCapture:useCapture
+ });
+
+ }
+ private var listeners:Array;
+ // Clean up the instance for garbage collection
+ protected function destroy():void
+ {
+
+ doneCallbacks = null;
+ if(listeners)
+ {
+ for(var i:int=0;i<listeners.length;i++)
+ {
+ var l:Object = listeners[i];
+ removeEventListener(l.type,l.handler,l.useCapture);
+ }
+ listeners = null;
+ }
+ }
}
-}
\ No newline at end of file
+}
diff --git a/frameworks/projects/Network/src/main/royale/NetworkClasses.as
b/frameworks/projects/Network/src/main/royale/NetworkClasses.as
index 7fa2a5d..18ef788 100644
--- a/frameworks/projects/Network/src/main/royale/NetworkClasses.as
+++ b/frameworks/projects/Network/src/main/royale/NetworkClasses.as
@@ -56,6 +56,10 @@ package
//TEMPORARY WIP - this should be migrated to AMFConnection at some
point when fully completed:
import org.apache.royale.net.remoting.amf.AMF0NetConnection;
AMF0NetConnection;
+
+ import org.apache.royale.utils.async.HttpRequestTask; HttpRequestTask;
+ import org.apache.royale.utils.async.HttpDownloadTask;
HttpDownloadTask;
+ import org.apache.royale.utils.async.HttpUploadTask; HttpUploadTask;
}
}
diff --git
a/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpDownloadTask.as
b/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpDownloadTask.as
new file mode 100644
index 0000000..196ea13
--- /dev/null
+++
b/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpDownloadTask.as
@@ -0,0 +1,81 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.utils.async
+{
+ import org.apache.royale.events.ProgressEvent;
+
+ public class HttpDownloadTask extends HttpRequestTask
+ {
+ /**
+ * HttpDownloadTask is a HttpRequestTask which has download
progress.
+ * It is a basic request and not appropriate for a Multipart
upload.
+ */
+ public function HttpDownloadTask()
+ {
+ super();
+ }
+
+ private var _bytesLoaded:int;
+
+ public function get bytesLoaded():int
+ {
+ return _bytesLoaded;
+ }
+
+ private var _bytesTotal:int;
+
+ public function get bytesTotal():int
+ {
+ return _bytesTotal;
+ }
+
+ override protected function attachLoaderCallbacks():void
+ {
+ super.attachLoaderCallbacks();
+ loader.onProgress = onProgress;
+ }
+ private function onProgress():void
+ {
+ _bytesLoaded = loader.bytesLoaded;
+ _bytesTotal = loader.bytesTotal;
+ dispatchEvent(new
ProgressEvent("progress",false,false,bytesLoaded,bytesTotal));
+ if(progressCallbacks)
+ {
+ for(var i:int=0;i<progressCallbacks.length;i++)
+ {
+ progressCallbacks[i](this);
+ }
+ }
+ }
+ private var progressCallbacks:Array;
+ public function progress(callback:Function):AsyncTask{
+ if(!progressCallbacks){
+ progressCallbacks = [];
+ }
+ progressCallbacks.push(callback);
+ return this;
+ }
+ override protected function destroy():void
+ {
+ super.destroy();
+ progressCallbacks = null;
+ }
+
+ }
+}
\ No newline at end of file
diff --git
a/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpRequestTask.as
b/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpRequestTask.as
new file mode 100644
index 0000000..98b7b7a
--- /dev/null
+++
b/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpRequestTask.as
@@ -0,0 +1,141 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.utils.async
+{
+ import org.apache.royale.net.HTTPConstants;
+ import org.apache.royale.debugging.assert;
+ import org.apache.royale.net.URLBinaryLoader;
+ import org.apache.royale.net.URLRequest;
+ import org.apache.royale.utils.BinaryData;
+
+ /**
+ * HttpRequestTask is an AsyncTask for making HTTP requests.
+ * You can access the result as well as the http status codes after the
task completes.
+ * As in all tasks, it cleans up after itself, so memory leaks should
be very uncommon.
+ */
+ public class HttpRequestTask extends AsyncTask
+ {
+ public function HttpRequestTask()
+ {
+ super();
+ }
+
+ override public function run(data:Object=null):void{
+ assert(url != null,"url must be specified!");
+ var request:URLRequest = new URLRequest(url);
+ request.contentType = contentType;
+ request.method = method;
+ if(requestHeaders)
+ request.requestHeaders = requestHeaders;
+ if(parameters)
+ request.data = parameters;
+ loader = getLoader();
+ attachLoaderCallbacks();
+ loader.load(request);
+ }
+ public var contentType:String = HTTPConstants.FORM_URL_ENCODED;
+ public var method:String = HTTPConstants.GET;
+ public var url:String;
+ /**
+ * An array url URLRequestHeaders if needed
+ */
+ public var requestHeaders:Array;
+ /**
+ * parameters is any object URLRequest can accept
+ */
+ public var parameters:Object;
+
+ public function get resultString():String
+ {
+ var l:URLBinaryLoader = loader;
+ if(l && l.data)
+ {
+ return l.data.readUTFBytes(l.data.length);
+ }
+ return "";
+ }
+ /**
+ * We try to parse the httpResult as JSON. If that fails, it
defaults to a string.
+ * For other response types (such as XML), the resultString
should be used to construct the correct result.
+ * If the binary response is needed, use binaryResult instead.
+ */
+ public function get httpResult():Object
+ {
+ var resultStr:String = resultString;
+ try{
+ return JSON.parse(resultStr);
+ }catch(err:Error){
+ return resultStr;
+ }
+ }
+
+ public function get httpStatus():Number
+ {
+ if(loader)
+ {
+ return loader.requestStatus;
+ }
+ return 0;
+ }
+
+ public function get binaryResult():BinaryData
+ {
+ return loader ? loader.data : null;
+ }
+ protected var loader:URLBinaryLoader;
+
+ /**
+ * Convenience function to access the underlying loader.
+ * Should not usually be necessary since the result can be
accessed directly from the task.
+ */
+ public function getLoader():URLBinaryLoader
+ {
+ return loader;
+ }
+ /**
+ * Can be overridded in a subclass to use a different
implementation (such as URLBinaryUploader)
+ */
+ protected function createLoader():URLBinaryLoader{
+ return new URLBinaryLoader();
+ }
+ /**
+ * Can be overridden in a subclass
+ */
+ protected function attachLoaderCallbacks():void
+ {
+ loader.onComplete = onComplete;
+ loader.onError = onError;
+ }
+ /**
+ * Can be overridden in a subclass
+ */
+ protected function onComplete():void
+ {
+ complete();
+ }
+ /**
+ * Can be overridden in a subclass
+ */
+ protected function onError():void
+ {
+ fail();
+ }
+
+ }
+}
\ No newline at end of file
diff --git
a/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpUploadTask.as
b/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpUploadTask.as
new file mode 100644
index 0000000..8c04c82
--- /dev/null
+++
b/frameworks/projects/Network/src/main/royale/org/apache/royale/utils/async/HttpUploadTask.as
@@ -0,0 +1,96 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.utils.async
+{
+ import org.apache.royale.events.ProgressEvent;
+ import org.apache.royale.net.URLBinaryUploader;
+ import org.apache.royale.net.URLBinaryLoader;
+
+ public class HttpUploadTask extends HttpRequestTask
+ {
+ /**
+ * HttpUploadTask is a HttpRequestTask which has upload
progress.
+ * It is a basic request and not appropriate for a Multipart
upload.
+ *
+ * HttpUploadTask uses URLBinaryUploader
+ * Care should be taken when using this class because it
attaches a progress listener to the xhr.upload object.
+ * Doing so causes browsers to send OPTIONS requests. This will
return an unauthorized response from servers not
+ * configured to allow CORS OPTIONS requests. See this S.O.
post for details. https://stackoverflow.com/a/17057853
+ *
+ * progress can be listened to by one of the following:
+ * 1. Specifying a progress(callback)
+ * 2. attaching an event listener for ProgressEvent.PROGRESS
+ *
+ */
+ public function HttpUploadTask()
+ {
+ super();
+ }
+
+ private var _bytesLoaded:int;
+
+ public function get bytesLoaded():int
+ {
+ return _bytesLoaded;
+ }
+
+ private var _bytesTotal:int;
+
+ public function get bytesTotal():int
+ {
+ return _bytesTotal;
+ }
+ override protected function createLoader():URLBinaryLoader{
+ return new URLBinaryUploader();
+ }
+
+ override protected function attachLoaderCallbacks():void
+ {
+ super.attachLoaderCallbacks();
+ (loader as URLBinaryUploader).onUploadProgress =
onProgress;
+ }
+ private function onProgress():void
+ {
+ _bytesLoaded = loader.bytesLoaded;
+ _bytesTotal = loader.bytesTotal;
+ dispatchEvent(new
ProgressEvent("progress",false,false,bytesLoaded,bytesTotal));
+ if(progressCallbacks)
+ {
+ for(var i:int=0;i<progressCallbacks.length;i++)
+ {
+ progressCallbacks[i](this);
+ }
+ }
+ }
+ private var progressCallbacks:Array;
+ public function progress(callback:Function):AsyncTask{
+ if(!progressCallbacks){
+ progressCallbacks = [];
+ }
+ progressCallbacks.push(callback);
+ return this;
+ }
+ override protected function destroy():void
+ {
+ super.destroy();
+ progressCallbacks = null;
+ }
+
+ }
+}
\ No newline at end of file