Simplify use of BackgroundWorker in Assets, add optional onError condition in worker
This commit is contained in:
@@ -16,9 +16,13 @@ import neko.vm.Thread;
|
||||
class BackgroundWorker {
|
||||
|
||||
|
||||
private static var MESSAGE_COMPLETE = "__COMPLETE__";
|
||||
private static var MESSAGE_ERROR = "__ERROR__";
|
||||
|
||||
public var canceled (default, null):Bool;
|
||||
public var doWork = new Event<Dynamic->Void> ();
|
||||
public var onComplete = new Event<Dynamic->Void> ();
|
||||
public var onError = new Event<Dynamic->Void> ();
|
||||
public var onProgress = new Event<Dynamic->Void> ();
|
||||
|
||||
private var __runMessage:Dynamic;
|
||||
@@ -74,7 +78,7 @@ class BackgroundWorker {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__messageQueue.add ("__DONE__");
|
||||
__messageQueue.add (MESSAGE_COMPLETE);
|
||||
__messageQueue.add (message);
|
||||
|
||||
#else
|
||||
@@ -91,6 +95,27 @@ class BackgroundWorker {
|
||||
}
|
||||
|
||||
|
||||
public function sendError (message:Dynamic):Void {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__messageQueue.add (MESSAGE_ERROR);
|
||||
__messageQueue.add (message);
|
||||
|
||||
#else
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
canceled = true;
|
||||
onError.dispatch (message);
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function sendProgress (message:Dynamic):Void {
|
||||
|
||||
#if (cpp || neko)
|
||||
@@ -116,7 +141,7 @@ class BackgroundWorker {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__messageQueue.add ("__DONE__");
|
||||
__messageQueue.add (MESSAGE_COMPLETE);
|
||||
|
||||
#else
|
||||
|
||||
@@ -140,15 +165,18 @@ class BackgroundWorker {
|
||||
|
||||
if (message != null) {
|
||||
|
||||
if (message != "__DONE__") {
|
||||
if (message == MESSAGE_ERROR) {
|
||||
|
||||
Application.current.onUpdate.remove (__update);
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
onProgress.dispatch (message);
|
||||
canceled = true;
|
||||
onError.dispatch (__messageQueue.pop (false));
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
} else if (message == MESSAGE_COMPLETE) {
|
||||
|
||||
Application.current.onUpdate.remove (__update);
|
||||
|
||||
@@ -159,6 +187,14 @@ class BackgroundWorker {
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
onProgress.dispatch (message);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -556,7 +556,7 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
});
|
||||
|
||||
worker.onComplete.add (function (bytes) handler (bytes));
|
||||
worker.onComplete.add (handler);
|
||||
worker.run ();
|
||||
|
||||
#end
|
||||
@@ -613,7 +613,7 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
});
|
||||
|
||||
worker.onComplete.add (function (image) handler (image));
|
||||
worker.onComplete.add (handler);
|
||||
worker.run ();
|
||||
|
||||
#end
|
||||
|
||||
Reference in New Issue
Block a user