yikulju

front-end development

Chaining in AS3 - part 2

Nov 16th 2008
No Comments
respond
trackback

Last time I showed you the first iteration of my chaining code which was working very well but it had some parts which could be optimized

To start with, I go back to the beginning.
One of the main advantages of using some kind of chaining is the fact that it describes asynchron method callings in a clear, and concise way. It is also helpful to describe synchron processes.

Here is a basic example:
1
2
3
4
5
6
7
8
9
10
First
getFont("arial")
then
getFont("verdana")
then
getFont("myriad")
then
getResource(12)
and finally, when everything finished
getPresentation()
Here is how it looks in as3
1
2
3
4
5
6
7
(new Chain(this)
  .begin
    .getFont({ fontName: "arial", fontURL: "/fonts/arial.otf" })
    .getFont({ fontName: "verdana", fontURL: "/fonts/verdana.otf" })
    .getFont({ fontName: "myriad", fontURL: "/fonts/myriad.otf" })
    .getResource({ id: 12 })
  .end(getPresentation)
We can reduce the three getFont calls by using a for-each style loop.
1
2
3
4
5
6
7
8
9
(new Chain(this)
 .begin
   .forEach([{ fontName: "arial", fontURL: "/fonts/arial.otf" }, 
                 { fontName: "verdana", fontURL: "/fonts/verdana.otf" }, 
                 { fontName: "myriad", fontURL: "/fonts/myriad.otf" } ])
	.loadFont()
   .end
   .getResource({ id: 12 })
  .end(getPresentation)

To have the big picture, let’s presume we have a class which has 4 methods.

init() loads the fonts, the resources and finally the presentation
getFont() loads one particular font
getResource() loads a resource
getPresentation() loads one presentation

and the code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public function init():void
{			
  (new Chain(this)
    .begin
	.forEach([{ fontName: "arial", fontURL: "/fonts/arial.otf" }, 
                      { fontName: "verdana", fontURL: "/fonts/verdana.otf" }, 
                      { fontName: "myriad", fontURL: "/fonts/myriad.otf" } ])
	        .loadFont()
	.end
	.getResource({ id: 12 })
    .end(getPresentation)
}
 
private function getFont(parameters:Object):void
{
	var fontName:String = parameters.fontName;
	var fontURL:String = parameters.fontURL;
 
	var callback:Function = parameters.completeHandler;
 
	...
 
	callback();
}
 
 
private function getResources(parameters:Object):void
{
	var resourceId:String = parameters.id;
 
	var callback:Function = parameters.completeHandler;
 
	...
 
	callback();
}
 
private function getPresentation(parameters:Object):void
{
	var preziId:String = parameters.id;
 
	var callback:Function = parameters.completeHandler;
 
	...
 
	callback();
}

The Chain has only one parameter which is the context (from which some methods will be invoked).

The methods get an object as a parameter from which they can access the parameters they require. For instance, the getFont gets a fontName and a fontURL parameters.

The completeHandler is kind of a special parameter, it filled by the Chain and should be invoked when the method finishes.

It starts to look like a DSL, what do you think?

I hope, soon I put the code and it’s documentation to the google code.

You can download the code from here


This post is tagged , ,

No Comments