diff --git a/lime/app/Future.hx b/lime/app/Future.hx index b0520c2c3..911e01ea6 100644 --- a/lime/app/Future.hx +++ b/lime/app/Future.hx @@ -1,66 +1,36 @@ package lime.app; +@:allow(lime.app.Promise) + + class Future { - private var __completeData:T; + public var isCompleted (get, null):Bool; + public var value:T; + private var __completed:Bool; private var __completeListeners:ArrayVoid>; private var __errored:Bool; private var __errorListeners:ArrayVoid>; private var __errorMessage:Dynamic; private var __progressListeners:ArrayVoid>; - private var __thenCallback:Void->Void; - public function new () { + public function new (work:Void->T = null) { - - - } - - - public function complete (data:T):Void { - - if (!__errored) { + if (work != null) { - __completed = true; - __completeData = data; - - if (__completeListeners != null) { + try { - for (listener in __completeListeners) { - - listener (data); - - } + value = work (); + __completed = true; - __completeListeners = null; + } catch (e:Dynamic) { - } - - } - - } - - - public function error (msg:Dynamic):Void { - - if (!__completed) { - - __errored = true; - __errorMessage = msg; - - if (__errorListeners != null) { - - for (listener in __errorListeners) { - - listener (msg); - - } - - __errorListeners = null; + __errored = true; + __errorMessage = e; } @@ -71,20 +41,24 @@ class Future { public function onComplete (listener:T->Void):Future { - if (__completed) { + if (listener != null) { - listener (__completeData); - - } else if (!__errored) { - - if (__completeListeners == null) { + if (__completed) { - __completeListeners = new Array (); + listener (value); + + } else if (!__errored) { + + if (__completeListeners == null) { + + __completeListeners = new Array (); + + } + + __completeListeners.push (listener); } - __completeListeners.push (listener); - } return this; @@ -94,20 +68,24 @@ class Future { public function onError (listener:Dynamic->Void):Future { - if (__errored) { + if (listener != null) { - listener (__errorMessage); - - } else if (!__completed) { - - if (__errorListeners == null) { + if (__errored) { - __errorListeners = new Array (); + listener (__errorMessage); + + } else if (!__completed) { + + if (__errorListeners == null) { + + __errorListeners = new Array (); + + } + + __errorListeners.push (listener); } - __errorListeners.push (listener); - } return this; @@ -117,35 +95,20 @@ class Future { public function onProgress (listener:Float->Void):Future { - if (__progressListeners == null) { + if (listener != null) { - __progressListeners = new Array (); - - } - - __progressListeners.push (listener); - - return this; - - } - - - public function progress (progress:Float):Void { - - if (!__errored && !__completed) { - - if (__progressListeners != null) { + if (__progressListeners == null) { - for (listener in __progressListeners) { - - listener (progress); - - } + __progressListeners = new Array (); } + __progressListeners.push (listener); + } + return this; + } @@ -153,7 +116,7 @@ class Future { if (__completed) { - return next (__completeData); + return next (value); } else if (__errored) { @@ -163,21 +126,38 @@ class Future { } else { - var future = new Future (); - onError (future.error); - onProgress (future.progress); + var promise = new Promise (); + + onError (promise.error); + onProgress (promise.progress); + onComplete (function (val) { - var f = next (val); - f.onError (future.error); - f.onComplete (future.complete); + var future = next (val); + future.onError (promise.error); + future.onComplete (promise.complete); }); - return future; + + return promise.future; } } + + + // Get & Set Methods + + + + + private function get_isCompleted ():Bool { + + return (__completed || __errored); + + } + + } \ No newline at end of file diff --git a/lime/app/Promise.hx b/lime/app/Promise.hx new file mode 100644 index 000000000..782074cae --- /dev/null +++ b/lime/app/Promise.hx @@ -0,0 +1,117 @@ +package lime.app; + + +class Promise { + + + public var future (default, null):Future; + public var isCompleted (get, null):Bool; + + + public function new () { + + future = new Future (); + + } + + + public function complete (data:T):Promise { + + if (!future.__errored) { + + future.__completed = true; + future.value = data; + + if (future.__completeListeners != null) { + + for (listener in future.__completeListeners) { + + listener (data); + + } + + future.__completeListeners = null; + + } + + } + + return this; + + } + + + public function completeWith (future:Future):Promise { + + future.onComplete (complete); + future.onError (error); + future.onProgress (progress); + + return this; + + } + + + + public function error (msg:Dynamic):Promise { + + if (!future.__completed) { + + future.__errored = true; + future.__errorMessage = msg; + + if (future.__errorListeners != null) { + + for (listener in future.__errorListeners) { + + listener (msg); + + } + + future.__errorListeners = null; + + } + + } + + return this; + + } + + + public function progress (progress:Float):Promise { + + if (!future.__errored && !future.__completed) { + + if (future.__progressListeners != null) { + + for (listener in future.__progressListeners) { + + listener (progress); + + } + + } + + } + + return this; + + } + + + + + // Get & Set Methods + + + + + private function get_isCompleted ():Bool { + + return future.isCompleted; + + } + + +} \ No newline at end of file