Separate into Future and Promise
This commit is contained in:
@@ -1,66 +1,36 @@
|
||||
package lime.app;
|
||||
|
||||
|
||||
@:allow(lime.app.Promise)
|
||||
|
||||
|
||||
class Future<T> {
|
||||
|
||||
|
||||
private var __completeData:T;
|
||||
public var isCompleted (get, null):Bool;
|
||||
public var value:T;
|
||||
|
||||
private var __completed:Bool;
|
||||
private var __completeListeners:Array<T->Void>;
|
||||
private var __errored:Bool;
|
||||
private var __errorListeners:Array<Dynamic->Void>;
|
||||
private var __errorMessage:Dynamic;
|
||||
private var __progressListeners:Array<Float->Void>;
|
||||
private var __thenCallback:Void->Void;
|
||||
|
||||
|
||||
public function new () {
|
||||
public function new (work:Void->T = null) {
|
||||
|
||||
if (work != null) {
|
||||
|
||||
try {
|
||||
|
||||
}
|
||||
value = work ();
|
||||
__completed = true;
|
||||
|
||||
} catch (e:Dynamic) {
|
||||
|
||||
public function complete (data:T):Void {
|
||||
|
||||
if (!__errored) {
|
||||
|
||||
__completed = true;
|
||||
__completeData = data;
|
||||
|
||||
if (__completeListeners != null) {
|
||||
|
||||
for (listener in __completeListeners) {
|
||||
|
||||
listener (data);
|
||||
|
||||
}
|
||||
|
||||
__completeListeners = null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
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<T> {
|
||||
|
||||
public function onComplete (listener:T->Void):Future<T> {
|
||||
|
||||
if (__completed) {
|
||||
if (listener != null) {
|
||||
|
||||
listener (__completeData);
|
||||
if (__completed) {
|
||||
|
||||
} else if (!__errored) {
|
||||
listener (value);
|
||||
|
||||
if (__completeListeners == null) {
|
||||
} else if (!__errored) {
|
||||
|
||||
__completeListeners = new Array ();
|
||||
if (__completeListeners == null) {
|
||||
|
||||
__completeListeners = new Array ();
|
||||
|
||||
}
|
||||
|
||||
__completeListeners.push (listener);
|
||||
|
||||
}
|
||||
|
||||
__completeListeners.push (listener);
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
@@ -94,20 +68,24 @@ class Future<T> {
|
||||
|
||||
public function onError (listener:Dynamic->Void):Future<T> {
|
||||
|
||||
if (__errored) {
|
||||
if (listener != null) {
|
||||
|
||||
listener (__errorMessage);
|
||||
if (__errored) {
|
||||
|
||||
} else if (!__completed) {
|
||||
listener (__errorMessage);
|
||||
|
||||
if (__errorListeners == null) {
|
||||
} else if (!__completed) {
|
||||
|
||||
__errorListeners = new Array ();
|
||||
if (__errorListeners == null) {
|
||||
|
||||
__errorListeners = new Array ();
|
||||
|
||||
}
|
||||
|
||||
__errorListeners.push (listener);
|
||||
|
||||
}
|
||||
|
||||
__errorListeners.push (listener);
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
@@ -117,35 +95,20 @@ class Future<T> {
|
||||
|
||||
public function onProgress (listener:Float->Void):Future<T> {
|
||||
|
||||
if (__progressListeners == null) {
|
||||
if (listener != null) {
|
||||
|
||||
__progressListeners = new Array ();
|
||||
if (__progressListeners == null) {
|
||||
|
||||
}
|
||||
|
||||
__progressListeners.push (listener);
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function progress (progress:Float):Void {
|
||||
|
||||
if (!__errored && !__completed) {
|
||||
|
||||
if (__progressListeners != null) {
|
||||
|
||||
for (listener in __progressListeners) {
|
||||
|
||||
listener (progress);
|
||||
|
||||
}
|
||||
__progressListeners = new Array ();
|
||||
|
||||
}
|
||||
|
||||
__progressListeners.push (listener);
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -153,7 +116,7 @@ class Future<T> {
|
||||
|
||||
if (__completed) {
|
||||
|
||||
return next (__completeData);
|
||||
return next (value);
|
||||
|
||||
} else if (__errored) {
|
||||
|
||||
@@ -163,21 +126,38 @@ class Future<T> {
|
||||
|
||||
} else {
|
||||
|
||||
var future = new Future<U> ();
|
||||
onError (future.error);
|
||||
onProgress (future.progress);
|
||||
var promise = new Promise<U> ();
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
117
lime/app/Promise.hx
Normal file
117
lime/app/Promise.hx
Normal file
@@ -0,0 +1,117 @@
|
||||
package lime.app;
|
||||
|
||||
|
||||
class Promise<T> {
|
||||
|
||||
|
||||
public var future (default, null):Future<T>;
|
||||
public var isCompleted (get, null):Bool;
|
||||
|
||||
|
||||
public function new () {
|
||||
|
||||
future = new Future ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function complete (data:T):Promise<T> {
|
||||
|
||||
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<T>):Promise<T> {
|
||||
|
||||
future.onComplete (complete);
|
||||
future.onError (error);
|
||||
future.onProgress (progress);
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function error (msg:Dynamic):Promise<T> {
|
||||
|
||||
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<T> {
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user