Fix errors with Future in HTML5.

This `Map` isn't needed on other targets, but the performance impact is
miniscule, so I opted for simplicity.
This commit is contained in:
Joseph Cloutier
2022-03-25 00:12:13 -04:00
parent 4589773cec
commit f5676afa58
2 changed files with 25 additions and 6 deletions

View File

@@ -353,6 +353,8 @@ import lime.utils.Log;
private static var singleThreadPool:ThreadPool;
#if lime_threads
private static var multiThreadPool:ThreadPool;
// It isn't safe to pass a promise object to a web worker.
private static var promises:Map<{}, Promise<Dynamic>> = new Map();
#end
public static var minThreads(default, set):Int = 0;
public static var maxThreads(default, set):Int = 1;
@@ -381,7 +383,21 @@ import lime.utils.Log;
@:allow(lime.app.Future)
private static function queue<T>(work:WorkFunction<State->Null<T>>, state:State, promise:Promise<T>, mode:ThreadMode = MULTI_THREADED):Void
{
getPool(mode).queue({work: work, state: state, promise: promise});
var bundle = {work: work, state: state, promise: promise};
#if lime_threads
if (mode == MULTI_THREADED)
{
#if html5
work.makePortable();
#end
promises[bundle] = promise;
bundle.promise = null;
}
#end
getPool(mode).queue(bundle);
}
// Event Handlers
@@ -414,12 +430,16 @@ import lime.utils.Log;
#if lime_threads
private static function multiThreadPool_onComplete(result:Dynamic):Void
{
multiThreadPool.eventData.state.promise.complete(result);
var promise:Promise<Dynamic> = promises[multiThreadPool.eventData.state];
promises.remove(multiThreadPool.eventData.state);
promise.complete(result);
}
private static function multiThreadPool_onError(error:Dynamic):Void
{
multiThreadPool.eventData.state.promise.error(error);
var promise:Promise<Dynamic> = promises[multiThreadPool.eventData.state];
promises.remove(multiThreadPool.eventData.state);
promise.error(error);
}
#end

View File

@@ -499,12 +499,9 @@ class ThreadPool extends WorkOutput
onProgress.dispatch(threadEvent.state);
case COMPLETE, ERROR:
// Remember that the listener could queue a new job.
if (threadEvent.event == COMPLETE)
{
onComplete.dispatch(threadEvent.state);
completed = activeJobs == 0 && __jobQueue.isEmpty();
}
else
{
@@ -529,6 +526,8 @@ class ThreadPool extends WorkOutput
}
#end
completed = threadEvent.event == COMPLETE && activeJobs == 0 && __jobQueue.isEmpty();
default:
}