Improve clarity.

This commit is contained in:
Joseph Cloutier
2022-03-25 17:36:10 -04:00
parent 05322f0dfa
commit 01ed2c8c58
3 changed files with 33 additions and 30 deletions

View File

@@ -419,26 +419,26 @@ import lime.utils.Log;
private static function singleThreadPool_onComplete(result:Dynamic):Void
{
singleThreadPool.eventData.state.promise.complete(result);
singleThreadPool.jobData.state.promise.complete(result);
}
private static function singleThreadPool_onError(error:Dynamic):Void
{
singleThreadPool.eventData.state.promise.error(error);
singleThreadPool.jobData.state.promise.error(error);
}
#if lime_threads
private static function multiThreadPool_onComplete(result:Dynamic):Void
{
var promise:Promise<Dynamic> = promises[multiThreadPool.eventData.state];
promises.remove(multiThreadPool.eventData.state);
var promise:Promise<Dynamic> = promises[multiThreadPool.jobData.state];
promises.remove(multiThreadPool.jobData.state);
promise.complete(result);
}
private static function multiThreadPool_onError(error:Dynamic):Void
{
var promise:Promise<Dynamic> = promises[multiThreadPool.eventData.state];
promises.remove(multiThreadPool.eventData.state);
var promise:Promise<Dynamic> = promises[multiThreadPool.jobData.state];
promises.remove(multiThreadPool.jobData.state);
promise.error(error);
}
#end

View File

@@ -33,17 +33,22 @@ import lime.system.ThreadPool;
abstract BackgroundWorker(ThreadPool)
{
private static var __doWorkWrapper:WorkFunction<State->WorkOutput->Void>;
private static var __eventData:EventData = new EventData();
private static var __jobData:JobData = new JobData();
@:deprecated("Instead pass the callback to BackgroundWorker.run().")
@:noCompletion @:dox(hide) public var doWork(get, never):{ add: (Dynamic->Void) -> Void };
public var eventData(get, never):EventData;
/**
Additional information about the job that triggered the current
`onComplete`, `onError`, or `onProgress` event. Will only be available
when one of those events is ongoing.
**/
public var jobData(get, never):JobData;
/**
__Call this only from the main thread.__
@param mode Defaults to `MULTI_THREAEDED` on most targets, but
@param mode Defaults to `MULTI_THREADED` on most targets, but
`SINGLE_THREADED` in HTML5. In HTML5, `MULTI_THREADED` mode uses web
workers, which impose additional restrictions.
@param workLoad (Single-threaded mode only) A rough estimate of how much
@@ -65,10 +70,8 @@ abstract BackgroundWorker(ThreadPool)
Cancels one active or queued job.
**/
// A copy of `ThreadPool.cancelJob()` with replacements:
// - Find "__activeJobs", replace with "this.__activeJobs".
// - Find "__idleThreads", replace with "this.__idleThreads".
// - Find "__jobQueue", replace with "this.__jobQueue".
// - Find ".state", replace with ".state.state".
// - Find the string "__", replace with "this.__".
// - Find the string ".state", replace with ".state.state".
// Other than that, keep the functions exactly in sync.
public function cancelJob(state:State):Bool
{
@@ -131,15 +134,15 @@ abstract BackgroundWorker(ThreadPool)
// Getters & Setters
private function get_eventData():EventData
private function get_jobData():JobData
{
if (this.eventData.state != null)
if (this.jobData.state != null)
{
__eventData.state = this.eventData.state.state;
__jobData.state = this.jobData.state.state;
}
__eventData.duration = this.eventData.duration;
__jobData.duration = this.jobData.duration;
return __eventData;
return __jobData;
}
private function get_doWork()

View File

@@ -133,10 +133,10 @@ class ThreadPool extends WorkOutput
/**
Additional information about the job that triggered the current
`onComplete`, `onError`, or `onProgress` event. Think of this as an
additional argument to the event listener.
`onComplete`, `onError`, or `onProgress` event. Will only be available
when one of those events is ongoing.
**/
public var eventData(default, null):EventData = new EventData();
public var jobData(default, null):JobData = new JobData();
@:deprecated("Instead pass the callback to ThreadPool's constructor.")
@:noCompletion @:dox(hide) public var doWork(get, never):{ add: (Dynamic->Void) -> Void };
@@ -144,8 +144,8 @@ class ThreadPool extends WorkOutput
#if lime_threads
/**
A list of idle threads. Not to be confused with `idleThreads`, which is
`__idleThreads.length`.
A list of idle threads. Not to be confused with `idleThreads`, a public
variable equal to `__idleThreads.length`.
**/
private var __idleThreads:List<Thread> = new List();
#end
@@ -217,7 +217,7 @@ class ThreadPool extends WorkOutput
if (error != null)
{
eventData.state = job.workEvent.state;
jobData.state = job.workEvent.state;
onError.dispatch(error);
}
}
@@ -236,14 +236,14 @@ class ThreadPool extends WorkOutput
{
for (job in __jobQueue)
{
eventData.state = job.state;
jobData.state = job.state;
onError.dispatch(error);
}
}
__jobQueue.clear();
__jobComplete.value = false;
eventData.clear();
jobData.clear();
completed = false;
canceled = true;
}
@@ -487,8 +487,8 @@ class ThreadPool extends WorkOutput
continue;
}
eventData.state = threadEvent.associatedJob.workEvent.state;
eventData.duration = mode == MULTI_THREADED ? timestamp() - threadEvent.jobStartTime : threadEvent.associatedJob.workTime;
jobData.state = threadEvent.associatedJob.workEvent.state;
jobData.duration = mode == MULTI_THREADED ? timestamp() - threadEvent.jobStartTime : threadEvent.associatedJob.workTime;
switch (threadEvent.event)
{
@@ -531,7 +531,7 @@ class ThreadPool extends WorkOutput
default:
}
eventData.clear();
jobData.clear();
}
if (completed)
@@ -616,7 +616,7 @@ class ThreadPool extends WorkOutput
**/
@:allow(lime.system.ThreadPool)
@:allow(lime.system.BackgroundWorker)
class EventData
class JobData
{
/**
The original `State` object passed to the job.