added media loader
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
package
|
||||
|
||||
import com.abit.haxework.net.JsonLoader;
|
||||
import flash.display.Sprite;
|
||||
|
||||
class Test extends Sprite {
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
trace("Ok");
|
||||
|
||||
new JsonLoader()
|
||||
.GET("http://umix.tv/channel/data/maxim.json")
|
||||
.success(function(data:String):Void {
|
||||
trace("OK: " + data);
|
||||
})
|
||||
.fail(function(error:Dynamic):Void {
|
||||
trace("FAIL: " + error);
|
||||
});
|
||||
}
|
||||
}
|
||||
133
com/abit/haxework/core/Tuple.hx
Executable file
133
com/abit/haxework/core/Tuple.hx
Executable file
@@ -0,0 +1,133 @@
|
||||
package com.abit.haxework.core;
|
||||
typedef Tuple2#if!H<T1, T2>#end = {
|
||||
var first(default, null):T1;
|
||||
var second(default, null):T2;
|
||||
}
|
||||
typedef Tuple3#if!H<T1, T2, T3>#end = {> Tuple2<T1, T2>,
|
||||
var third(default, null):T3;
|
||||
}
|
||||
typedef Tuple4#if!H<T1, T2, T3, T4>#end = {> Tuple3<T1, T2, T3>,
|
||||
var fourth(default, null):T4;
|
||||
}
|
||||
typedef Tuple5#if!H<T1, T2, T3, T4, T5>#end = {> Tuple4<T1, T2, T3, T4>,
|
||||
var fifth(default, null):T5;
|
||||
}
|
||||
|
||||
class Tuple {
|
||||
public static function five<T1, T2, T3, T4, T5>(first:T1, second:T2, third:T3, fourth:T4, fifth:T5):Tuple5<T1, T2, T3, T4, T5> {
|
||||
return new InternalTuple5(first, second, third, fourth, fifth);
|
||||
}
|
||||
|
||||
public static function four<T1, T2, T3, T4>(first:T1, second:T2, third:T3, fourth:T4):Tuple4<T1, T2, T3, T4> {
|
||||
return new InternalTuple4(first, second, third, fourth);
|
||||
}
|
||||
|
||||
public static function three<T1, T2, T3>(first:T1, second:T2, third:T3):Tuple3<T1, T2, T3> {
|
||||
return new InternalTuple3(first, second, third);
|
||||
}
|
||||
|
||||
public static function two<T1, T2>(first:T1, second:T2):Tuple2<T1, T2> {
|
||||
return new InternalTuple2(first, second);
|
||||
}
|
||||
|
||||
public static inline function asTuple2<T1, T2, T3>(tuple:Tuple3<T1, T2, T3>):Tuple2<T1, T2>
|
||||
return tuple;
|
||||
|
||||
public static inline function asTuple3<T1, T2, T3, T4>(tuple:Tuple4<T1, T2, T3, T4>):Tuple3<T1, T2, T3>
|
||||
return tuple;
|
||||
|
||||
public static inline function asTuple4<T1, T2, T3, T4, T5>(tuple:Tuple5<T1, T2, T3, T4, T5>):Tuple4<T1, T2, T3, T4>
|
||||
return tuple;
|
||||
}
|
||||
|
||||
private class InternalTuple2<T1, T2> {
|
||||
public var first(default, null):T1;
|
||||
public var second(default, null):T2;
|
||||
|
||||
/**
|
||||
* Creates a new tuple.
|
||||
* @param first The first value.
|
||||
* @param second The second value.
|
||||
*/
|
||||
|
||||
public function new(first:T1, second:T2) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return "(" + first + ", " + second + ")";
|
||||
}
|
||||
}
|
||||
private class InternalTuple3<T1, T2, T3> extends InternalTuple2<T1, T2> {
|
||||
public var third(default, null):T3;
|
||||
|
||||
/**
|
||||
* Creates a new tuple.
|
||||
* @param first The first value.
|
||||
* @param second The second value.
|
||||
* @param third The third value.
|
||||
*/
|
||||
|
||||
public function new(first:T1, second:T2, third:T3) {
|
||||
super(first, second);
|
||||
this.third = third;
|
||||
}
|
||||
|
||||
public override function toString():String {
|
||||
return "("
|
||||
+ first + ", "
|
||||
+ second + ", "
|
||||
+ third + ")";
|
||||
}
|
||||
}
|
||||
private class InternalTuple4<T1, T2, T3, T4> extends InternalTuple3<T1, T2, T3> {
|
||||
public var fourth(default, null):T4;
|
||||
|
||||
/**
|
||||
* Creates a new tuple.
|
||||
* @param first The first value.
|
||||
* @param second The second value.
|
||||
* @param third The third value.
|
||||
* @param fourth The fourth value.
|
||||
*/
|
||||
|
||||
public function new(first:T1, second:T2, third:T3, fourth:T4) {
|
||||
super(first, second, third);
|
||||
this.fourth = fourth;
|
||||
}
|
||||
|
||||
public override function toString():String {
|
||||
return "("
|
||||
+ first + ", "
|
||||
+ second + ", "
|
||||
+ third + ", "
|
||||
+ fourth + ")";
|
||||
}
|
||||
}
|
||||
private class InternalTuple5<T1, T2, T3, T4, T5> extends InternalTuple4<T1, T2, T3, T4> {
|
||||
public var fifth(default, null):T5;
|
||||
|
||||
/**
|
||||
* Creates a new tuple.
|
||||
* @param first The first value.
|
||||
* @param second The second value.
|
||||
* @param third The third value.
|
||||
* @param fourth The fourth value.
|
||||
* @param fifth The fifth value.
|
||||
*/
|
||||
|
||||
public function new(first:T1, second:T2, third:T3, fourth:T4, fifth:T5) {
|
||||
super(first, second, third, fourth);
|
||||
this.fifth = fifth;
|
||||
}
|
||||
|
||||
public override function toString():String {
|
||||
return "("
|
||||
+ first + ", "
|
||||
+ second + ", "
|
||||
+ third + ", "
|
||||
+ fourth + ", "
|
||||
+ fifth + ")";
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package
|
||||
package com.abit.haxework.net;
|
||||
|
||||
import flash.net.URLLoaderDataFormat;
|
||||
import flash.events.SecurityErrorEvent;
|
||||
import com.abit.haxework.net.callback.Callback;
|
||||
import com.abit.haxework.net.callback.ICallback;
|
||||
@@ -13,7 +14,6 @@ class BaseLoader<T> implements ILoader<T> {
|
||||
|
||||
private var busy:Bool;
|
||||
private var url:String;
|
||||
private var loader:URLLoader;
|
||||
private var callback:ICallback<T>;
|
||||
|
||||
public function new() {
|
||||
@@ -25,8 +25,7 @@ class BaseLoader<T> implements ILoader<T> {
|
||||
busy = true;
|
||||
this.url = url;
|
||||
callback = new Callback<T>();
|
||||
loader = buildLoader();
|
||||
loader.load(new URLRequest(url));
|
||||
internalRequest();
|
||||
return callback;
|
||||
}
|
||||
|
||||
@@ -34,16 +33,12 @@ class BaseLoader<T> implements ILoader<T> {
|
||||
return request(url, Method.GET);
|
||||
}
|
||||
|
||||
private function buildLoader():URLLoader {
|
||||
var loader:URLLoader = new URLLoader();
|
||||
loader.addEventListener(Event.COMPLETE, onComplete);
|
||||
loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
return loader;
|
||||
private function internalRequest():Void {
|
||||
throw "Abstract";
|
||||
}
|
||||
|
||||
private function onComplete(e:Event):Void {
|
||||
var data:T = extrudeResult(loader.data);
|
||||
var data:T = extrudeResult(e);
|
||||
var c:ICallback<T> = callback;
|
||||
dispose();
|
||||
c.callSuccess(data);
|
||||
@@ -55,19 +50,19 @@ class BaseLoader<T> implements ILoader<T> {
|
||||
c.callFail(e);
|
||||
}
|
||||
|
||||
private function extrudeResult(e:Event):T {
|
||||
throw "Abstract";
|
||||
return null;
|
||||
}
|
||||
|
||||
private function dispose():Void {
|
||||
loader.removeEventListener(Event.COMPLETE, onComplete);
|
||||
loader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
loader = null;
|
||||
url = null;
|
||||
callback = null;
|
||||
busy = false;
|
||||
}
|
||||
|
||||
private function extrudeResult(data:Dynamic):T {
|
||||
throw "Abstract";
|
||||
return null;
|
||||
public function cancel():Void {
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
37
com/abit/haxework/net/BaseMediaLoader.hx
Executable file
37
com/abit/haxework/net/BaseMediaLoader.hx
Executable file
@@ -0,0 +1,37 @@
|
||||
package com.abit.haxework.net;
|
||||
|
||||
import com.abit.haxework.net.BaseLoader;
|
||||
import flash.events.SecurityErrorEvent;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.events.Event;
|
||||
import flash.net.URLRequest;
|
||||
import flash.display.Loader;
|
||||
|
||||
class BaseMediaLoader<T> extends BaseLoader<T> {
|
||||
|
||||
private var loader:Loader;
|
||||
|
||||
override private function internalRequest():Void {
|
||||
loader = buildLoader();
|
||||
loader.load(new URLRequest(url));
|
||||
}
|
||||
|
||||
private function buildLoader():Loader {
|
||||
var loader:Loader = new Loader();
|
||||
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
|
||||
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
return loader;
|
||||
}
|
||||
|
||||
override private function dispose():Void {
|
||||
super.dispose();
|
||||
if (loader != null) {
|
||||
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
|
||||
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
loader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
54
com/abit/haxework/net/BaseURLLoader.hx
Executable file
54
com/abit/haxework/net/BaseURLLoader.hx
Executable file
@@ -0,0 +1,54 @@
|
||||
package com.abit.haxework.net;
|
||||
|
||||
import flash.net.URLLoaderDataFormat;
|
||||
import flash.events.SecurityErrorEvent;
|
||||
import com.abit.haxework.net.callback.Callback;
|
||||
import com.abit.haxework.net.callback.ICallback;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.net.URLRequest;
|
||||
import flash.events.Event;
|
||||
import flash.net.URLLoader;
|
||||
import com.abit.haxework.net.ILoader.Method;
|
||||
|
||||
class BaseURLLoader<T> extends BaseLoader<T> {
|
||||
|
||||
private var dataFormat:URLLoaderDataFormat;
|
||||
private var loader:URLLoader;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
dataFormat = URLLoaderDataFormat.TEXT;
|
||||
}
|
||||
|
||||
override private function internalRequest():Void {
|
||||
loader = buildLoader();
|
||||
loader.load(new URLRequest(url));
|
||||
}
|
||||
|
||||
private function buildLoader():URLLoader {
|
||||
var loader:URLLoader = new URLLoader();
|
||||
loader.dataFormat = dataFormat;
|
||||
loader.addEventListener(Event.COMPLETE, onComplete);
|
||||
loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
return loader;
|
||||
}
|
||||
|
||||
override private function dispose():Void {
|
||||
super.dispose();
|
||||
if (loader != null) {
|
||||
loader.removeEventListener(Event.COMPLETE, onComplete);
|
||||
loader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
loader = null;
|
||||
}
|
||||
}
|
||||
|
||||
override public function cancel():Void {
|
||||
if (loader != null) {
|
||||
try {loader.close();} catch (error:Dynamic) {}
|
||||
}
|
||||
super.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package
|
||||
package com.abit.haxework.net;
|
||||
|
||||
import com.abit.haxework.net.callback.ICallback;
|
||||
|
||||
@@ -10,4 +10,6 @@ enum Method {
|
||||
interface ILoader<T> {
|
||||
public function request(url:String, method:Method):ICallback<T>;
|
||||
public function GET(url:String):ICallback<T>;
|
||||
|
||||
public function cancel():Void;
|
||||
}
|
||||
|
||||
16
com/abit/haxework/net/ImageLoader.hx
Executable file
16
com/abit/haxework/net/ImageLoader.hx
Executable file
@@ -0,0 +1,16 @@
|
||||
package com.abit.haxework.net;
|
||||
|
||||
import flash.display.LoaderInfo;
|
||||
import flash.display.Loader;
|
||||
import flash.display.Bitmap;
|
||||
import flash.events.Event;
|
||||
import flash.display.BitmapData;
|
||||
|
||||
class ImageLoader extends BaseMediaLoader<BitmapData> {
|
||||
|
||||
override private function extrudeResult(e:Event):BitmapData {
|
||||
var content:Bitmap = cast(cast(e.currentTarget, LoaderInfo).loader.content,Bitmap);
|
||||
return content.bitmapData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package
|
||||
package com.abit.haxework.net;
|
||||
|
||||
import flash.events.Event;
|
||||
import flash.net.URLLoader;
|
||||
import haxe.Json;
|
||||
|
||||
class JsonLoader extends BaseLoader<Dynamic> {
|
||||
class JsonLoader extends BaseURLLoader<Dynamic> {
|
||||
|
||||
override private function extrudeResult(data:Dynamic):Dynamic {
|
||||
return Json.parse(Std.string(data));
|
||||
override private function extrudeResult(e:Event):Dynamic {
|
||||
return Json.parse(Std.string(cast(e.currentTarget, URLLoader).data));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package
|
||||
package com.abit.haxework.net;
|
||||
|
||||
class TextLoader extends BaseLoader<String> {
|
||||
import flash.net.URLLoader;
|
||||
import flash.events.Event;
|
||||
|
||||
override private function extrudeResult(data:Dynamic):String {
|
||||
return Std.string(data);
|
||||
class TextLoader extends BaseURLLoader<String> {
|
||||
|
||||
override private function extrudeResult(e:Event):String {
|
||||
return Std.string(cast(e.currentTarget, URLLoader).data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package
|
||||
package com.abit.haxework.net.callback;
|
||||
|
||||
class Callback<T> implements ICallback<T> {
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package
|
||||
package com.abit.haxework.net.callback;
|
||||
|
||||
interface ICallback<T> {
|
||||
public function success(f:T -> Void):ICallback<T>;
|
||||
|
||||
Reference in New Issue
Block a user