Revert BackgroundWorker to its 8.1.3 version.
It looks like we'll want to take `BackgroundWorker` in a different direction, so for the moment it's safest not to change anything about it. That way, there's only one historical version to maintain backwards compatibility with.
This commit is contained in:
@@ -1,4 +1,177 @@
|
||||
package lime.system;
|
||||
|
||||
@:deprecated("Replace references to lime.system.BackgroundWorker with lime.system.ThreadPool. As the API is identical, no other changes are necessary.")
|
||||
typedef BackgroundWorker = ThreadPool;
|
||||
import lime.app.Application;
|
||||
import lime.app.Event;
|
||||
#if sys
|
||||
#if haxe4
|
||||
import sys.thread.Deque;
|
||||
import sys.thread.Thread;
|
||||
#elseif cpp
|
||||
import cpp.vm.Deque;
|
||||
import cpp.vm.Thread;
|
||||
#elseif neko
|
||||
import neko.vm.Deque;
|
||||
import neko.vm.Thread;
|
||||
#end
|
||||
#end
|
||||
#if !lime_debug
|
||||
@:fileXml('tags="haxe,release"')
|
||||
@:noDebug
|
||||
#end
|
||||
|
||||
/**
|
||||
A background worker executes a single function on a background thread,
|
||||
allowing it to avoid blocking the main thread. However, only system targets
|
||||
have thread support, meaning the function will block on any other target.
|
||||
@see `ThreadPool` for improved thread safety, HTML5 threads, and more.
|
||||
**/
|
||||
class BackgroundWorker
|
||||
{
|
||||
private static var MESSAGE_COMPLETE = "__COMPLETE__";
|
||||
private static var MESSAGE_ERROR = "__ERROR__";
|
||||
|
||||
public var canceled(default, null):Bool;
|
||||
public var completed(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>();
|
||||
|
||||
@:noCompletion private var __runMessage:Dynamic;
|
||||
#if (cpp || neko)
|
||||
@:noCompletion private var __messageQueue:Deque<Dynamic>;
|
||||
@:noCompletion private var __workerThread:Thread;
|
||||
#end
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function cancel():Void
|
||||
{
|
||||
canceled = true;
|
||||
|
||||
#if (cpp || neko)
|
||||
__workerThread = null;
|
||||
#end
|
||||
}
|
||||
|
||||
public function run(message:Dynamic = null):Void
|
||||
{
|
||||
canceled = false;
|
||||
completed = false;
|
||||
__runMessage = message;
|
||||
|
||||
#if (cpp || neko)
|
||||
__messageQueue = new Deque<Dynamic>();
|
||||
__workerThread = Thread.create(__doWork);
|
||||
|
||||
// TODO: Better way to do this
|
||||
|
||||
if (Application.current != null)
|
||||
{
|
||||
Application.current.onUpdate.add(__update);
|
||||
}
|
||||
#else
|
||||
__doWork();
|
||||
#end
|
||||
}
|
||||
|
||||
public function sendComplete(message:Dynamic = null):Void
|
||||
{
|
||||
completed = true;
|
||||
|
||||
#if (cpp || neko)
|
||||
__messageQueue.add(MESSAGE_COMPLETE);
|
||||
__messageQueue.add(message);
|
||||
#else
|
||||
if (!canceled)
|
||||
{
|
||||
canceled = true;
|
||||
onComplete.dispatch(message);
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
public function sendError(message:Dynamic = null):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 = null):Void
|
||||
{
|
||||
#if (cpp || neko)
|
||||
__messageQueue.add(message);
|
||||
#else
|
||||
if (!canceled)
|
||||
{
|
||||
onProgress.dispatch(message);
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
@:noCompletion private function __doWork():Void
|
||||
{
|
||||
doWork.dispatch(__runMessage);
|
||||
|
||||
// #if (cpp || neko)
|
||||
//
|
||||
// __messageQueue.add (MESSAGE_COMPLETE);
|
||||
//
|
||||
// #else
|
||||
//
|
||||
// if (!canceled) {
|
||||
//
|
||||
// canceled = true;
|
||||
// onComplete.dispatch (null);
|
||||
//
|
||||
// }
|
||||
//
|
||||
// #end
|
||||
}
|
||||
|
||||
@:noCompletion private function __update(deltaTime:Int):Void
|
||||
{
|
||||
#if (cpp || neko)
|
||||
var message = __messageQueue.pop(false);
|
||||
|
||||
if (message != null)
|
||||
{
|
||||
if (message == MESSAGE_ERROR)
|
||||
{
|
||||
Application.current.onUpdate.remove(__update);
|
||||
|
||||
if (!canceled)
|
||||
{
|
||||
canceled = true;
|
||||
onError.dispatch(__messageQueue.pop(false));
|
||||
}
|
||||
}
|
||||
else if (message == MESSAGE_COMPLETE)
|
||||
{
|
||||
Application.current.onUpdate.remove(__update);
|
||||
|
||||
if (!canceled)
|
||||
{
|
||||
canceled = true;
|
||||
onComplete.dispatch(__messageQueue.pop(false));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!canceled)
|
||||
{
|
||||
onProgress.dispatch(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user