Merge pull request #1643 from player-03/doWork_compatibility

Improve `doWork`'s backwards compatibility.
This commit is contained in:
player-03
2023-03-02 17:01:28 -05:00
committed by GitHub
2 changed files with 35 additions and 187 deletions

View File

@@ -1,172 +1,4 @@
package lime.system;
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
@:deprecated("lime.system.BackgroundWorker is being deprecated since Lime 8.2.0, and it is recommended to use the lime.system.ThreadPool class instead.")
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
}
}
@:deprecated("Replace references to lime.system.BackgroundWorker with lime.system.ThreadPool. As the API is identical, no other changes are necessary.")
typedef BackgroundWorker = ThreadPool;

View File

@@ -139,7 +139,7 @@ class ThreadPool extends WorkOutput
public var onRun(default, null) = new Event<State->Void>();
@:deprecated("Instead pass the callback to ThreadPool.run().")
@:noCompletion @:dox(hide) public var doWork(get, never):{ add: (Dynamic->Void)->Void };
@:noCompletion @:dox(hide) public var doWork(get, never):PseudoEvent;
private var __doWork:WorkFunction<State->WorkOutput->Void>;
private var __activeJobs:JobList = new JobList();
@@ -603,26 +603,42 @@ class ThreadPool extends WorkOutput
// Note the distinction between `doWork` and `__doWork`: the former is for
// backwards compatibility, while the latter is always used.
private function get_doWork():{ add: (Dynamic->Void) -> Void }
private function get_doWork():PseudoEvent
{
return {
add: function(callback:Dynamic->Void)
{
#if html5
if (mode == MULTI_THREADED)
throw "Unsupported operation; instead pass the callback to ThreadPool's constructor.";
#end
__doWork = #if (lime_threads && html5) { func: #end
function(state:State, output:WorkOutput):Void
{
callback(state);
}
#if (lime_threads && html5) } #end;
}
};
return this;
}
}
@:access(lime.system.ThreadPool) @:forward(canceled)
private abstract PseudoEvent(ThreadPool) from ThreadPool {
@:noCompletion @:dox(hide) public var __listeners(get, never):Array<Dynamic>;
private inline function get___listeners():Array<Dynamic> { return []; };
@:noCompletion @:dox(hide) public var __repeat(get, never):Array<Bool>;
private inline function get___repeat():Array<Bool> { return []; };
public function add(callback:Dynamic -> Void):Void {
function callCallback(state:State, output:WorkOutput):Void
{
callback(state);
}
#if (lime_threads && html5)
if (this.mode == MULTI_THREADED)
throw "Unsupported operation; instead pass the callback to ThreadPool's constructor.";
else
this.__doWork = { func: callCallback };
#else
this.__doWork = callCallback;
#end
}
public inline function cancel():Void {}
public inline function dispatch():Void {}
public inline function has(callback:Dynamic -> Void):Bool { return this.__doWork != null; }
public inline function remove(callback:Dynamic -> Void):Void { this.__doWork = null; }
public inline function removeAll():Void { this.__doWork = null; }
}
@:forward
abstract JobList(List<JobData>)
{