You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@royale.apache.org by Maria Jose Esteve <mj...@iest.com> on 2021/12/02 09:10:34 UTC

IAsyncTask ¿any example?

Hello,
@Harb, is there an example of how to use AsyncTask? I would like to experiment with it, I think I can use it in some of the projects and then include it in the examples project that I maintain [1]

[1] https://github.com/mjesteve/royale-examples-community

Hiedra.


RE: IAsyncTask ¿any example?

Posted by Maria Jose Esteve <mj...@iest.com>.
Thank you very much Harb, I will study your examples this afternoon.

hiedra

-----Mensaje original-----
De: Harbs <ha...@gmail.com> 
Enviado el: jueves, 2 de diciembre de 2021 11:52
Para: dev@royale.apache.org
Asunto: Re: IAsyncTask ¿any example?

And here’s an example I’m using in production which runs more than one task in parallel. As you can see, it’s very easy to use:

public function embedFonts(render:Function):void{
	if(fontsEmbedded()){
		render();
	} else {
		var tasks:Array = [];
		var font:FontVO = getAppliedFont();
		if(!font.embedded){
			tasks.push(new LoadFontTask(font));
		}
		font = getBulletFont();
		if(font && !font.embedded){
			tasks.push(new LoadFontTask(font));
		}
		var task:CompoundAsyncTask = new CompoundAsyncTask(tasks);
		task.done(function(task:AsyncTask):void{
			if(task.completed){
				// we're good
				render();
			} else {
				// status is "failed" -- we're failing silently
			}
		});
		task.run();
	}
	
}

And the LoadFontTask class looks like this:

  public class LoadFontTask extends AsyncTask
  {
    public function LoadFontTask(font:FontVO)
    {
      super();
      this.font = font;
    }
    private var font:FontVO;
    override public function run(data:Object = null):void{
      var fp:FontProxy = getProxy(FontProxy.NAME) as FontProxy;
      fp.loadNow(font,fontLoadDone);
    }
    private function fontLoadDone():void{
      if(font.embedded){
        complete();
      } else {
        fail();
      }
    }
  }

> On Dec 2, 2021, at 12:39 PM, Harbs <ha...@gmail.com> wrote:
> 
> var task: HttpRequestTask = new HttpRequestTask(); task.url = 
> “https://foobaz.com/myapi”; 
> task.done(function(taskReturned:HttpRequestTask):void{
> 	trace(“task is the same and taskReturned: “ + task == taskReturned);// you get the task back in the done callback
> 	trace(task.status);// will be either AsyncTask.COMPLETE, AsyncTask.FAILED or AsyncTask.CANCELED
> 	if(task.completed){
> 		trace(task.httpResult);
> 		trace(task.httpStatus);
> 	} else if(task.failed){// it failed you can get the status to know why
> 		trace(task.httpStatus);
>    } else {
>    	// The only other possibility is it was canceled
>    	if(task.status == AsyncTask.CANCELED){
>    		trace("your task was canceled in the middle!");
>    	} else {
> 	  		throw new Error("This should be impossible. Tasks either complete or fail");
>    	}
>    }
> });
> // you need to run it or nothing happens...
> task.run();
> 
>> On Dec 2, 2021, at 11:10 AM, Maria Jose Esteve <mj...@iest.com> wrote:
>> 
>> Hello,
>> @Harb, is there an example of how to use AsyncTask? I would like to 
>> experiment with it, I think I can use it in some of the projects and 
>> then include it in the examples project that I maintain [1]
>> 
>> [1] https://github.com/mjesteve/royale-examples-community
>> 
>> Hiedra.
>> 
> 


Re: IAsyncTask ¿any example?

Posted by Harbs <ha...@gmail.com>.
And here’s an example I’m using in production which runs more than one task in parallel. As you can see, it’s very easy to use:

public function embedFonts(render:Function):void{
	if(fontsEmbedded()){
		render();
	} else {
		var tasks:Array = [];
		var font:FontVO = getAppliedFont();
		if(!font.embedded){
			tasks.push(new LoadFontTask(font));
		}
		font = getBulletFont();
		if(font && !font.embedded){
			tasks.push(new LoadFontTask(font));
		}
		var task:CompoundAsyncTask = new CompoundAsyncTask(tasks);
		task.done(function(task:AsyncTask):void{
			if(task.completed){
				// we're good
				render();
			} else {
				// status is "failed" -- we're failing silently
			}
		});
		task.run();
	}
	
}

And the LoadFontTask class looks like this:

  public class LoadFontTask extends AsyncTask
  {
    public function LoadFontTask(font:FontVO)
    {
      super();
      this.font = font;
    }
    private var font:FontVO;
    override public function run(data:Object = null):void{
      var fp:FontProxy = getProxy(FontProxy.NAME) as FontProxy;
      fp.loadNow(font,fontLoadDone);
    }
    private function fontLoadDone():void{
      if(font.embedded){
        complete();
      } else {
        fail();
      }
    }
  }

> On Dec 2, 2021, at 12:39 PM, Harbs <ha...@gmail.com> wrote:
> 
> var task: HttpRequestTask = new HttpRequestTask();
> task.url = “https://foobaz.com/myapi”;
> task.done(function(taskReturned:HttpRequestTask):void{
> 	trace(“task is the same and taskReturned: “ + task == taskReturned);// you get the task back in the done callback
> 	trace(task.status);// will be either AsyncTask.COMPLETE, AsyncTask.FAILED or AsyncTask.CANCELED
> 	if(task.completed){
> 		trace(task.httpResult);
> 		trace(task.httpStatus);
> 	} else if(task.failed){// it failed you can get the status to know why
> 		trace(task.httpStatus);
>    } else {
>    	// The only other possibility is it was canceled
>    	if(task.status == AsyncTask.CANCELED){
>    		trace("your task was canceled in the middle!");
>    	} else {
> 	  		throw new Error("This should be impossible. Tasks either complete or fail");
>    	}
>    }
> });
> // you need to run it or nothing happens...
> task.run();
> 
>> On Dec 2, 2021, at 11:10 AM, Maria Jose Esteve <mj...@iest.com> wrote:
>> 
>> Hello,
>> @Harb, is there an example of how to use AsyncTask? I would like to experiment with it, I think I can use it in some of the projects and then include it in the examples project that I maintain [1]
>> 
>> [1] https://github.com/mjesteve/royale-examples-community
>> 
>> Hiedra.
>> 
> 


Re: IAsyncTask ¿any example?

Posted by Harbs <ha...@gmail.com>.
var task: HttpRequestTask = new HttpRequestTask();
task.url = “https://foobaz.com/myapi”;
task.done(function(taskReturned:HttpRequestTask):void{
	trace(“task is the same and taskReturned: “ + task == taskReturned);// you get the task back in the done callback
	trace(task.status);// will be either AsyncTask.COMPLETE, AsyncTask.FAILED or AsyncTask.CANCELED
	if(task.completed){
		trace(task.httpResult);
		trace(task.httpStatus);
	} else if(task.failed){// it failed you can get the status to know why
		trace(task.httpStatus);
    } else {
    	// The only other possibility is it was canceled
    	if(task.status == AsyncTask.CANCELED){
    		trace("your task was canceled in the middle!");
    	} else {
	  		throw new Error("This should be impossible. Tasks either complete or fail");
    	}
    }
});
// you need to run it or nothing happens...
task.run();

> On Dec 2, 2021, at 11:10 AM, Maria Jose Esteve <mj...@iest.com> wrote:
> 
> Hello,
> @Harb, is there an example of how to use AsyncTask? I would like to experiment with it, I think I can use it in some of the projects and then include it in the examples project that I maintain [1]
> 
> [1] https://github.com/mjesteve/royale-examples-community
> 
> Hiedra.
>