157 lines
3.9 KiB
Haxe
Executable File
157 lines
3.9 KiB
Haxe
Executable File
package haxework.net;
|
|
|
|
import haxe.Timer;
|
|
import flash.net.URLRequestMethod;
|
|
import flash.events.ProgressEvent;
|
|
import haxework.net.manage.ILoaderManager;
|
|
import haxework.provider.Provider;
|
|
import flash.utils.ByteArray;
|
|
import haxework.net.callback.Callback;
|
|
import haxework.net.callback.ICallback;
|
|
import flash.events.Event;
|
|
|
|
class BaseLoader<T> implements ILoader<T> {
|
|
|
|
private static inline var TAG:String = "Loader";
|
|
|
|
//ToDo: move to LoaderManager
|
|
public static var urlProcessors(default, null):Array<String->String> = new Array<String->String>();
|
|
public static function prepareUrl(url:String):String { for (p in urlProcessors) url = p(url); return url; }
|
|
|
|
|
|
public var timeout(default, default):Int;
|
|
public var busy(default, null):Bool;
|
|
public var completed(default, null):Float;
|
|
|
|
private var url:String;
|
|
private var method:String;
|
|
private var data:Null<Dynamic>;
|
|
private var callback:ICallback<T>;
|
|
private var timer:Timer;
|
|
|
|
public function new(?timeout = 0) {
|
|
this.timeout = timeout;
|
|
busy = false;
|
|
completed = Math.NaN;
|
|
}
|
|
|
|
public function request(url:String, method:String, data:Dynamic = null):ICallback<T> {
|
|
if (busy) throw "Busy";
|
|
busy = true;
|
|
this.url = url;
|
|
this.method = method;
|
|
this.data = data;
|
|
callback = new Callback<T>();
|
|
var url:String = this.url;
|
|
//L.d(TAG, "Request: " + prepareUrl(url));
|
|
//internalRequest(prepareUrl(url));
|
|
Provider.get(ILoaderManager).add(this);
|
|
return callback;
|
|
}
|
|
|
|
private function cockTimeout():Void {
|
|
if (timeout > 0) {
|
|
timer = new Timer(timeout);
|
|
timer.run = callTimeout;
|
|
}
|
|
}
|
|
|
|
public function fromBytes(data:ByteArray):ICallback<T> {
|
|
if (busy) throw "Busy";
|
|
busy = true;
|
|
callback = new Callback<T>();
|
|
internalFromBytes(data);
|
|
return callback;
|
|
}
|
|
|
|
public function GET(url:String, data:Dynamic = null):ICallback<T> {
|
|
#if openfl
|
|
if (StringTools.startsWith(url, "%assets%")) {
|
|
var path:String = url.substring(9);
|
|
var bytes:ByteArray = openfl.Assets.getBytes(path);
|
|
return fromBytes(bytes);
|
|
}
|
|
#end
|
|
return request(url, URLRequestMethod.GET, data);
|
|
}
|
|
|
|
public function POST(url:String, data:Dynamic = null):ICallback<T> {
|
|
return request(url, URLRequestMethod.POST, data);
|
|
}
|
|
|
|
public function DELETE(url:String, data:Dynamic = null):ICallback<T> {
|
|
return request(url, URLRequestMethod.DELETE, data);
|
|
}
|
|
|
|
private function internalRequest(url:String):Void {
|
|
throw "Abstract";
|
|
}
|
|
|
|
private function internalFromBytes(data:ByteArray):Void {
|
|
throw "Abstract";
|
|
}
|
|
|
|
private function onInit(e:Event):Void {}
|
|
|
|
private function onProgress(e:ProgressEvent):Void {
|
|
completed = e.bytesLoaded / e.bytesTotal;
|
|
}
|
|
|
|
private function onComplete(e:Event):Void {
|
|
var data:T = extrudeResult(e);
|
|
if (data != null) {
|
|
var c:ICallback<T> = callback;
|
|
dispose();
|
|
c.callSuccess(data);
|
|
}
|
|
}
|
|
|
|
private function onError(e:Event):Void {
|
|
var c:ICallback<T> = callback;
|
|
dispose();
|
|
c.callFail(e);
|
|
}
|
|
|
|
private function callTimeout():Void {
|
|
var c:ICallback<T> = callback;
|
|
var error:String = "Timeout for: " + url;
|
|
dispose();
|
|
c.callFail(error);
|
|
}
|
|
|
|
private function extrudeResult(e:Event):T {
|
|
throw "Abstract";
|
|
return null;
|
|
}
|
|
|
|
private function dispose():Void {
|
|
if (timer != null) {
|
|
timer.stop();
|
|
timer = null;
|
|
}
|
|
url = null;
|
|
data = null;
|
|
callback = null;
|
|
busy = false;
|
|
completed = Math.NaN;
|
|
Provider.get(ILoaderManager).release(this);
|
|
}
|
|
|
|
public function cancel():Void {
|
|
dispose();
|
|
}
|
|
|
|
public function run():Void {
|
|
var u:String = url;
|
|
if (data != null && method == URLRequestMethod.GET) {
|
|
var a:Array<String> = [];
|
|
for (key in Reflect.fields(data)) {
|
|
a.push(key + "=" + Reflect.field(data, key));
|
|
}
|
|
u += "?" + a.join("&");
|
|
}
|
|
internalRequest(prepareUrl(u));
|
|
}
|
|
}
|
|
|