This commit is contained in:
2013-11-05 17:08:50 +04:00
parent 71a149c2b2
commit 6a4c1161cf
10 changed files with 182 additions and 2 deletions

73
CompilationOption.hx Executable file
View File

@@ -0,0 +1,73 @@
package;
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
#end
/**
* CompilationOption allows us to use compile-time variables for advanced conditional compilation.
*/
class CompilationOption {
#if macro
/**
* Internal storage of the options.
*/
static var storage = new Map<String,Dynamic>();
#end
/**
* Set `key` to `value`.
*
* For simplicity `value` can only be constant Bool/Int/Float/String or null.
* Array and structures are also possible, check:
* http://haxe.org/manual/macros#constant-arguments
*
* Set `force` to true in order to override an option.
* But be careful overriding will influenced by compilation order which may be hard to predict.
*/
@:overload(function (key:String, value:Bool, ?force:Bool = false):Void{})
@:overload(function (key:String, value:Int, ?force:Bool = false):Void{})
@:overload(function (key:String, value:Float, ?force:Bool = false):Void{})
@:overload(function (key:String, value:String, ?force:Bool = false):Void{})
macro static public function set(key:String, value:Dynamic, ?force:Bool = false) {
if (!force && storage.exists(key))
throw key + " has already been set to " + storage.get(key);
storage.set(key, value);
return macro {}; //an empty block, which means nothing
}
/**
* Return the option as a constant.
*/
macro static public function get(key:String):Expr {
return Context.makeExpr(storage.get(key), Context.currentPos());
}
/**
* Tell if `key` was set.
*/
macro static public function exists(key:String):ExprOf<Bool> {
return Context.makeExpr(storage.exists(key), Context.currentPos());
}
/**
* Removes an option. Returns true if there was such option.
*/
macro static public function remove(key:String):ExprOf<Bool> {
return Context.makeExpr(storage.remove(key), Context.currentPos());
}
/**
* Dump the options as an object with keys as fields.
* eg. trace(CompilationOption.dump());
*/
macro static public function dump():ExprOf<Dynamic> {
var obj = {};
for (key in storage.keys()) {
Reflect.setField(obj, key, storage.get(key));
}
return Context.makeExpr(obj, Context.currentPos());
}
}

30
Meta.hx Executable file
View File

@@ -0,0 +1,30 @@
package;
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
#end
class Meta {
#if macro
private static inline var VERSION:String = "version";
private static inline var BUILD:String = "build";
private static var data:Map<String, Dynamic> = new Map<String, Dynamic>();
#end
macro static public function getBuild():ExprOf<String> {
return Context.makeExpr(data.get(BUILD), Context.currentPos());
}
macro static public function getVersion():ExprOf<String> {
return Context.makeExpr(data.get(VERSION), Context.currentPos());
}
macro static public function set(version:String) {
data.set(BUILD, Date.now().toString());
data.set(VERSION, version);
return macro {};
}
}

View File

@@ -1,5 +1,6 @@
package haxework.net;
import flash.utils.ByteArray;
import haxework.net.callback.Callback;
import haxework.net.callback.ICallback;
import flash.events.Event;
@@ -31,6 +32,14 @@ class BaseLoader<T> implements ILoader<T> {
return callback;
}
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> {
return request(url, Method.GET, data);
}
@@ -43,6 +52,10 @@ class BaseLoader<T> implements ILoader<T> {
throw "Abstract";
}
private function internalFromBytes(data:ByteArray):Void {
throw "Abstract";
}
private function onComplete(e:Event):Void {
var data:T = extrudeResult(e);
var c:ICallback<T> = callback;

View File

@@ -1,5 +1,6 @@
package haxework.net;
import flash.utils.ByteArray;
import haxework.net.BaseLoader;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
@@ -16,6 +17,11 @@ class BaseMediaLoader<T> extends BaseLoader<T> {
loader.load(new URLRequest(url));
}
override private function internalFromBytes(data:ByteArray):Void {
loader = buildLoader();
loader.loadBytes(data);
}
private function buildLoader():Loader {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

View File

@@ -12,9 +12,9 @@ class BaseURLLoader<T> extends BaseLoader<T> {
private var dataFormat:URLLoaderDataFormat;
private var loader:URLLoader;
public function new() {
public function new(dateFormat:URLLoaderDataFormat = null) {
super();
dataFormat = URLLoaderDataFormat.TEXT;
this.dataFormat = (dateFormat == null ? URLLoaderDataFormat.TEXT : dateFormat);
}
override private function internalRequest(url:String):Void {

17
haxework/net/BytesLoader.hx Executable file
View File

@@ -0,0 +1,17 @@
package haxework.net;
import flash.net.URLLoaderDataFormat;
import flash.utils.ByteArray;
import flash.net.URLLoader;
import flash.events.Event;
class BytesLoader extends BaseURLLoader<ByteArray> {
public function new() {
super(URLLoaderDataFormat.BINARY);
}
override private function extrudeResult(e:Event):ByteArray {
return cast(cast(e.currentTarget, URLLoader).data, ByteArray);
}
}

View File

@@ -1,5 +1,6 @@
package haxework.net;
import flash.utils.ByteArray;
import haxework.net.callback.ICallback;
enum Method {
@@ -10,6 +11,7 @@ enum Method {
interface ILoader<T> {
public var busy(default, null):Bool;
public function request(url:String, method:Method, data:Dynamic = null):ICallback<T>;
public function fromBytes(data:ByteArray):ICallback<T>;
public function GET(url:String, data:Dynamic = null):ICallback<T>;
public function POST(url:String, data:Dynamic = null):ICallback<T>;
public function cancel():Void;

16
haxework/net/SwfLoader.hx Executable file
View File

@@ -0,0 +1,16 @@
package haxework.net;
import flash.display.MovieClip;
import flash.display.LoaderInfo;
import flash.display.Loader;
import flash.events.Event;
class SwfLoader extends BaseMediaLoader<MovieClip> {
override private function extrudeResult(e:Event):MovieClip {
var content:MovieClip = cast(cast(e.currentTarget, LoaderInfo).loader.content, MovieClip);
content.gotoAndStop(0);
return content;
}
}

View File

@@ -1,5 +1,7 @@
package haxework.net.callback;
import haxe.Timer;
class Callback<T> implements ICallback<T> {
private var _success:Null<T -> Void>;
@@ -25,6 +27,12 @@ class Callback<T> implements ICallback<T> {
}
}
public function callSuccessAsync(data:T):Void {
Timer.delay(function():Void {
callSuccess(data);
}, 1);
}
public function callFail(error:Dynamic):Void {
try {
if (_fail != null) _fail(error);
@@ -32,4 +40,16 @@ class Callback<T> implements ICallback<T> {
trace(error);
}
}
public function callFailAsync(error:Dynamic):Void {
Timer.delay(function():Void {
callFail(error);
}, 1);
}
public function glue(callback:ICallback<T>):ICallback<T> {
this._success = callback.callSuccess;
this._fail = callback.callFail;
return this;
}
}

View File

@@ -4,6 +4,9 @@ interface ICallback<T> {
public function success(f:T -> Void):ICallback<T>;
public function fail(f:Dynamic -> Void):ICallback<T>;
public function callSuccess(data:T):Void;
public function callSuccessAsync(data:T):Void;
public function callFail(error:Dynamic):Void;
public function callFailAsync(error:Dynamic):Void;
public function glue(callback:ICallback<T>):ICallback<T>;
}