From 3204cb29a3951f121b97fc72ccb92db86dc5434e Mon Sep 17 00:00:00 2001 From: nirth Date: Fri, 28 Jan 2011 22:40:36 +0200 Subject: [PATCH] IRoute now handles only single IActions instance, logic of executing queues migrated to IActions --- src/eu/kiichigo/route/kore/Action.as | 18 +++++- src/eu/kiichigo/route/kore/Actions.as | 86 ++++++++++++++++++++++++++ src/eu/kiichigo/route/kore/IActions.as | 4 +- src/eu/kiichigo/route/routes/Route.as | 60 +++++------------- 4 files changed, 122 insertions(+), 46 deletions(-) create mode 100644 src/eu/kiichigo/route/kore/Actions.as diff --git a/src/eu/kiichigo/route/kore/Action.as b/src/eu/kiichigo/route/kore/Action.as index ca8c64c..be1c091 100644 --- a/src/eu/kiichigo/route/kore/Action.as +++ b/src/eu/kiichigo/route/kore/Action.as @@ -23,7 +23,7 @@ package eu.kiichigo.route.kore */ public function get route():IRoute { - return null; + return _route; } /** * @private @@ -31,6 +31,7 @@ package eu.kiichigo.route.kore public function set route(value:IRoute):void { _route = value; + commit(); } @@ -64,6 +65,12 @@ package eu.kiichigo.route.kore return this; } + /** + * Auxilary function, evaluates whether instance of IAction will be executed or not. + * + * @param percept Incoming from IRoute percept. + * @return Boolean value indicating wether function will or will not be run. + */ protected function evaluate( percept:Object = null ):Boolean { if( _predicate == null ) @@ -85,5 +92,14 @@ package eu.kiichigo.route.kore { } + + /** + * + * + */ + protected function commit():void + { + + } } } \ No newline at end of file diff --git a/src/eu/kiichigo/route/kore/Actions.as b/src/eu/kiichigo/route/kore/Actions.as new file mode 100644 index 0000000..6263405 --- /dev/null +++ b/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. = new Vector.; + /** + * @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 IRoute. Accepts instances of IAction and Function. + */ + 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 ); + } +} \ No newline at end of file diff --git a/src/eu/kiichigo/route/kore/IActions.as b/src/eu/kiichigo/route/kore/IActions.as index 3f23f0b..5f89600 100644 --- a/src/eu/kiichigo/route/kore/IActions.as +++ b/src/eu/kiichigo/route/kore/IActions.as @@ -5,10 +5,10 @@ package eu.kiichigo.route.kore /** * Collection (vector) of IAction instances that will be run once this IActions instance is executed. */ - function get actions():Vector.; + function get list():Object; /** * */ - function set actions( value:Vector. ):void; + function set list( value:Object ):void; } } \ No newline at end of file diff --git a/src/eu/kiichigo/route/routes/Route.as b/src/eu/kiichigo/route/routes/Route.as index 1126bbf..7b2eba4 100644 --- a/src/eu/kiichigo/route/routes/Route.as +++ b/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; @@ -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. @@ -81,7 +82,7 @@ package eu.kiichigo.route.routes /** * @private */ - protected const _actions:Vector. = new Vector.; + protected var _actions:IActions; /** * @copy eu.kiichigo.route.routes.IRoute#actions */ @@ -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(); } @@ -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 IRoute. Accepts instances of IAction and Function. - */ - 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 ); } } \ No newline at end of file