开发者

is there any cool Deferred library in ActionScript?

开发者 https://www.devze.com 2023-02-09 03:29 出处:网络
JSD开发者_运维知识库effered is so cool: https://github.com/cho45/jsdeferred/blob/master/test-jsdeferred.js

JSD开发者_运维知识库effered is so cool: https://github.com/cho45/jsdeferred/blob/master/test-jsdeferred.js

we can write simplest async call chain .

next(function () { // this `next` is global function
    alert("1");
}).
next(function () { // this `next` is Deferred#next
    alert("2");
}).
next(function () {
    alert("3");
});

our code is so spagetti code like that new Execute1(nextFunction); ....

is there any cool Deferred library in ActionScript? or Which script are you using?


I just came across this:

https://github.com/CodeCatalyst/promise-as3

I haven't tried it yet, but it looks.. promising. It's modelled on jQuery's Deferred, follows the CommonJS Promise/A spec (I assume), and has a decent looking set of unit tests.


It is very simple to create this syntax yourself. Every function should return the instance of the class itself (return this).

Create an as3 class called Chainer

package  
{
    public class Chainer 
    {
        public static function create():Chainer
        {
            return new Chainer();
        }

        public function next(func:Function, ...rest):Chainer
        {
            func.call(this, rest); // call the function with params
            return this; // returns itself to enable chaing
        }
    }

}

Now use the class with your next-function. You could call it like this:

Chainer.create()
    .next(function():void { 
        trace("1") 
    } )
    .next(function():void { 
        trace("2") 
    } );

There could be problems if you want to extend the Chainer class, since you cannot change the return type:
OOP problem: Extending a class, override functions and jQuery-like syntax

I have used this type of code to create a little helper class:
http://blog.stroep.nl/2010/10/chain-tween/
http://blog.stroep.nl/2009/11/delayed-function-calling-chain/

BTW this tween library is based on jQuery like syntax too:
http://code.google.com/p/eaze-tween/


I think most tweening library will do exactly what you ask for. For instance TweenLite and TimelineLite (https://www.greensock.com/timelinelite/) should do the job perfectly.


I'm not sure this is what you're looking for, but there's quite a nice port of LINQ for AS3 here: https://bitbucket.org/briangenisio/actionlinq/wiki/Home

0

精彩评论

暂无评论...
验证码 换一张
取 消