Use TheadPool instead of BackgroundWorker to limit Assets.load* requests at once

This commit is contained in:
Joshua Granick
2015-07-10 17:35:06 -07:00
parent 961ef1e594
commit 74604c9077
2 changed files with 34 additions and 16 deletions

View File

@@ -8,7 +8,7 @@ import lime.audio.AudioSource;
import lime.audio.openal.AL;
import lime.audio.AudioBuffer;
import lime.graphics.Image;
import lime.system.BackgroundWorker;
import lime.system.ThreadPool;
import lime.text.Font;
import lime.utils.ByteArray;
import lime.utils.UInt8Array;
@@ -40,6 +40,8 @@ class DefaultAssetLibrary extends AssetLibrary {
public var type (default, null) = new Map <String, AssetType> ();
private var lastModified:Float;
private var loadHandlers:Map<String, Dynamic>;
private var threadPool:ThreadPool;
private var timer:Timer;
@@ -132,6 +134,24 @@ class DefaultAssetLibrary extends AssetLibrary {
}
private function createThreadPool ():Void {
threadPool = new ThreadPool (0, 2);
threadPool.doWork.add (function (id, getMethod) {
threadPool.sendComplete (id, getMethod (id));
});
threadPool.onComplete.add (function (id, data) {
var handler = loadHandlers.get (id);
handler (data);
});
}
public override function exists (id:String, type:String):Bool {
var requestedType = type != null ? cast (type, AssetType) : null;
@@ -548,16 +568,15 @@ class DefaultAssetLibrary extends AssetLibrary {
#else
var worker = new BackgroundWorker ();
worker.doWork.add (function (_) {
if (threadPool == null) {
worker.sendComplete (getBytes (id));
loadHandlers = new Map ();
createThreadPool ();
});
}
worker.onComplete.add (handler);
worker.run ();
loadHandlers.set (id, handler);
threadPool.queue (id, getBytes);
#end
@@ -605,16 +624,15 @@ class DefaultAssetLibrary extends AssetLibrary {
#else
var worker = new BackgroundWorker ();
worker.doWork.add (function (_) {
if (threadPool == null) {
worker.sendComplete (getImage (id));
loadHandlers = new Map ();
createThreadPool ();
});
}
worker.onComplete.add (handler);
worker.run ();
loadHandlers.set (id, handler);
threadPool.queue (id, getImage);
#end