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:
@@ -353,6 +353,8 @@ import lime.utils.Log;
|
|||||||
private static var singleThreadPool:ThreadPool;
|
private static var singleThreadPool:ThreadPool;
|
||||||
#if lime_threads
|
#if lime_threads
|
||||||
private static var multiThreadPool:ThreadPool;
|
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
|
#end
|
||||||
public static var minThreads(default, set):Int = 0;
|
public static var minThreads(default, set):Int = 0;
|
||||||
public static var maxThreads(default, set):Int = 1;
|
public static var maxThreads(default, set):Int = 1;
|
||||||
@@ -381,7 +383,21 @@ import lime.utils.Log;
|
|||||||
@:allow(lime.app.Future)
|
@:allow(lime.app.Future)
|
||||||
private static function queue<T>(work:WorkFunction<State->Null<T>>, state:State, promise:Promise<T>, mode:ThreadMode = MULTI_THREADED):Void
|
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
|
// Event Handlers
|
||||||
@@ -414,12 +430,16 @@ import lime.utils.Log;
|
|||||||
#if lime_threads
|
#if lime_threads
|
||||||
private static function multiThreadPool_onComplete(result:Dynamic):Void
|
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
|
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
|
#end
|
||||||
|
|
||||||
|
|||||||
@@ -499,12 +499,9 @@ class ThreadPool extends WorkOutput
|
|||||||
onProgress.dispatch(threadEvent.state);
|
onProgress.dispatch(threadEvent.state);
|
||||||
|
|
||||||
case COMPLETE, ERROR:
|
case COMPLETE, ERROR:
|
||||||
// Remember that the listener could queue a new job.
|
|
||||||
if (threadEvent.event == COMPLETE)
|
if (threadEvent.event == COMPLETE)
|
||||||
{
|
{
|
||||||
onComplete.dispatch(threadEvent.state);
|
onComplete.dispatch(threadEvent.state);
|
||||||
|
|
||||||
completed = activeJobs == 0 && __jobQueue.isEmpty();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -529,6 +526,8 @@ class ThreadPool extends WorkOutput
|
|||||||
}
|
}
|
||||||
#end
|
#end
|
||||||
|
|
||||||
|
completed = threadEvent.event == COMPLETE && activeJobs == 0 && __jobQueue.isEmpty();
|
||||||
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user