diff --git a/lime/system/BackgroundWorker.hx b/lime/system/BackgroundWorker.hx index fee183da9..29074a6e0 100644 --- a/lime/system/BackgroundWorker.hx +++ b/lime/system/BackgroundWorker.hx @@ -16,9 +16,13 @@ import neko.vm.Thread; class BackgroundWorker { + private static var MESSAGE_COMPLETE = "__COMPLETE__"; + private static var MESSAGE_ERROR = "__ERROR__"; + public var canceled (default, null):Bool; public var doWork = new EventVoid> (); public var onComplete = new EventVoid> (); + public var onError = new EventVoid> (); public var onProgress = new EventVoid> (); private var __runMessage:Dynamic; @@ -74,7 +78,7 @@ class BackgroundWorker { #if (cpp || neko) - __messageQueue.add ("__DONE__"); + __messageQueue.add (MESSAGE_COMPLETE); __messageQueue.add (message); #else @@ -91,6 +95,27 @@ class BackgroundWorker { } + public function sendError (message:Dynamic):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):Void { #if (cpp || neko) @@ -116,7 +141,7 @@ class BackgroundWorker { #if (cpp || neko) - __messageQueue.add ("__DONE__"); + __messageQueue.add (MESSAGE_COMPLETE); #else @@ -140,15 +165,18 @@ class BackgroundWorker { if (message != null) { - if (message != "__DONE__") { + if (message == MESSAGE_ERROR) { + + Application.current.onUpdate.remove (__update); if (!canceled) { - onProgress.dispatch (message); + canceled = true; + onError.dispatch (__messageQueue.pop (false)); } - } else { + } else if (message == MESSAGE_COMPLETE) { Application.current.onUpdate.remove (__update); @@ -159,6 +187,14 @@ class BackgroundWorker { } + } else { + + if (!canceled) { + + onProgress.dispatch (message); + + } + } } diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 3171bdeee..71c610829 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -556,7 +556,7 @@ class DefaultAssetLibrary extends AssetLibrary { }); - worker.onComplete.add (function (bytes) handler (bytes)); + worker.onComplete.add (handler); worker.run (); #end @@ -613,7 +613,7 @@ class DefaultAssetLibrary extends AssetLibrary { }); - worker.onComplete.add (function (image) handler (image)); + worker.onComplete.add (handler); worker.run (); #end