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