Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeList.forEach #27

Closed
Kalinovych opened this issue Jul 1, 2013 · 0 comments
Closed

NodeList.forEach #27

Kalinovych opened this issue Jul 1, 2013 · 0 comments

Comments

@Kalinovych
Copy link

Hi Richard,
how about to add forEach() method to the NodeList?

public function forEach( callback : Function ) : void
{
    for( var node : Node = head; node; node = node.next )
    {
        callback( node );
    }
}

simple example of use in a System:

override public function addToEngine( engine:Engine ):void
{
    nodeList = engine.getNodeList( MyNode );
    nodeList.forEach( handleNodeAdded );
    nodeList.nodeAdded.add( handleNodeAdded );
    nodeList.nodeRemoved.add( handleNodeRemoved );
}

override public function removeFromEngine( engine:Engine ):void
{
    nodeList.nodeAdded.remove( handleNodeAdded );
    nodeList.nodeRemoved.remove( handleNodeRemoved );
    nodeList.forEach( handleNodeRemoved );
    nodeList = null;
}

Maybe somebody will like it to use in the update function.
Of course for() iteration will be a bit faster than forEach() but we can live this decision to a developer.

override public function update( time:Number ):void
{
    nodeList.forEach( processNode );
}

private function processNode( node:MyNode ):void
{

}

Sometimes we need to pass additional params like a "time" in the update func.
Here is the extended implementation:

public function forEach( callback : Function, ...params ) : void
{
    if ( head )
    {
        params.unshift( null );
        for( var node : Node = head; node; node = node.next )
        {
            params[0] = node;
            callback.apply( null, params );
        }
    }
}

Now update function can look like:

override public function update( time:Number ):void
{
    nodeList.forEach( processNode, time );

    // nodeList.forEach( processNode ); - will work fine too
}

private function processNode( node:MyNode, time:Number ):void
{

}

Also can be an implementation with checking a value returned by callback.apply( null, params ); and if this value == false break forEach loop :)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant