Skip to content

Commit

Permalink
IRoute now handles only single IActions instance, logic of executing …
Browse files Browse the repository at this point in the history
…queues migrated to IActions
  • Loading branch information
nirth committed Jan 28, 2011
1 parent f8708f1 commit 3204cb2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 46 deletions.
18 changes: 17 additions & 1 deletion src/eu/kiichigo/route/kore/Action.as
Expand Up @@ -23,14 +23,15 @@ package eu.kiichigo.route.kore
*/
public function get route():IRoute
{
return null;
return _route;
}
/**
* @private
*/
public function set route(value:IRoute):void
{
_route = value;
commit();
}


Expand Down Expand Up @@ -64,6 +65,12 @@ package eu.kiichigo.route.kore
return this;
}

/**
* Auxilary function, evaluates whether instance of <code>IAction</code> will be executed or not.
*
* @param percept Incoming from <code>IRoute</code> percept.
* @return <code>Boolean</code> value indicating wether function will or will not be run.
*/
protected function evaluate( percept:Object = null ):Boolean
{
if( _predicate == null )
Expand All @@ -85,5 +92,14 @@ package eu.kiichigo.route.kore
{

}

/**
*
*
*/
protected function commit():void
{

}
}
}
86 changes: 86 additions & 0 deletions src/eu/kiichigo/route/kore/Actions.as
@@ -0,0 +1,86 @@
package eu.kiichigo.route.kore
{
import eu.kiichigo.route.utils.add;

public class Actions extends Action implements IActions
{
/**
* @private
*/
protected const _list:Vector.<IAction> = new Vector.<IAction>;
/**
* @copy eu.kiichigo.route.kore.IActions#list
*/
public function get list():Object
{
return _list;
}
/**
* @private
*/
public function set list( value:Object ):void
{
eu.kiichigo.route.utils.add( _list, process )( value );
commit();
}

/**
* @inheritDoc
*/
override protected function exec( percept:Object ):void
{
for( var i:uint = 0; i < _list.length; i ++ )
_list[i].run( percept );
}

/**
* @private
* Initializes an action with an instance of <code>IRoute</code>. Accepts instances of <code>IAction</code> and <code>Function</code>.
*/
protected function process( action:Object ):IAction
{
return ( action is IAction ? action : new Closure( action as Function ) ) as IAction;
}

/**
* @inheritDoc
*/
override protected function commit():void
{
// if( _route != null && _list.length > 0 )
// _list.
}
}
}


import eu.kiichigo.route.kore.Action;
import eu.kiichigo.route.utils.log;

/**
* @private
*/
class Closure extends Action
{
/**
* @private
*/
public function Closure( closure:Function )
{
this.closure = closure;
}
/**
* @private
*/
protected var closure:Function;
/**
* @private
*/
override protected function exec( percept:Object ):void
{
if( closure.length == 0 )
closure.call();
else if( closure.length == 1 )
closure.call( null, percept );
}
}
4 changes: 2 additions & 2 deletions src/eu/kiichigo/route/kore/IActions.as
Expand Up @@ -5,10 +5,10 @@ package eu.kiichigo.route.kore
/**
* Collection (vector) of <code>IAction</code> instances that will be run once this <code>IActions</code> instance is executed.
*/
function get actions():Vector.<IAction>;
function get list():Object;
/**
*
*/
function set actions( value:Vector.<IAction> ):void;
function set list( value:Object ):void;
}
}
60 changes: 17 additions & 43 deletions src/eu/kiichigo/route/routes/Route.as
@@ -1,6 +1,8 @@
package eu.kiichigo.route.routes
{
import eu.kiichigo.route.kore.Actions;
import eu.kiichigo.route.kore.IAction;
import eu.kiichigo.route.kore.IActions;
import eu.kiichigo.route.kore.IRouter;
import eu.kiichigo.route.pattern.IPattern;
import eu.kiichigo.route.perceive.IPerceiver;
Expand All @@ -24,7 +26,6 @@ package eu.kiichigo.route.routes
protected namespace generator;
protected namespace instance;

//ToDo: see if initialize can be run only once, or it should be transformed to something like commitProperties on IInvalidating.
/**
* @private
* Handles initialization once pattern, perceiver and actions are set.
Expand Down Expand Up @@ -81,7 +82,7 @@ package eu.kiichigo.route.routes
/**
* @private
*/
protected const _actions:Vector.<IAction> = new Vector.<IAction>;
protected var _actions:IActions;
/**
* @copy eu.kiichigo.route.routes.IRoute#actions
*/
Expand All @@ -94,11 +95,16 @@ package eu.kiichigo.route.routes
*/
public function set actions( value:Object ):void
{
//In case single instance of Function or Action is passed, wrap it in an Array.
if( value is Function || value is IAction )
value = [value];

eu.kiichigo.route.utils.add( _actions, process )( value );
if( value is IActions )
_actions = value as IActions;
else
{
if( value is Function || value is IAction )
value = [value];

_actions = new Actions;
_actions.list = value;
}
commit();
}

Expand Down Expand Up @@ -154,50 +160,18 @@ package eu.kiichigo.route.routes
}


/**
* @copy eu.kiichigo.route.routes.IRoute#perceive
*/
public function perceive( percept:Object ):Object
{
if( pattern == null ||
!( pattern is IPattern ? pattern.match( percept ) : pattern( percept ) ) )
return null;

for( var i:uint = 0; i < _actions.length; i ++ )
actions[i].run( percept );
_actions.run( percept );

return percept;
}


/**
* @private
* Initializes an action with an instance of <code>IRoute</code>. Accepts instances of <code>IAction</code> and <code>Function</code>.
*/
protected function process( action:Object ):IAction
{
if( action is IAction )
action.route = this;

return ( action is IAction ? action : new Closure( action as Function ) ) as IAction;
}
}
}
import eu.kiichigo.route.kore.Action;
import eu.kiichigo.route.utils.log;

class Closure extends Action
{
public function Closure( closure:Function )
{
this.closure = closure;
}

protected var closure:Function;

override protected function exec( percept:Object ):void
{
trace( "closure.exec", closure.length );
if( closure.length == 0 )
closure.call();
else if( closure.length == 1 )
closure.call( null, percept );
}
}

0 comments on commit 3204cb2

Please sign in to comment.