Skip to content

Commit

Permalink
EventMap opens up routeEventToListener. Some minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
darscan committed Jun 22, 2013
1 parent 1a8578c commit 0a9e40b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
51 changes: 19 additions & 32 deletions src/robotlegs/bender/extensions/localEventMap/impl/EventMap.as
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,37 @@ package robotlegs.bender.extensions.localEventMap.impl
{
eventClass ||= Event;

const currentListeners:Vector.<EventMapConfig> = _suspended ? _suspendedListeners : _listeners;
const currentListeners:Vector.<EventMapConfig> = _suspended
? _suspendedListeners
: _listeners;

var eventConfig:EventMapConfig;
var config:EventMapConfig;

var i:int = currentListeners.length;
while (i--)
{
eventConfig = currentListeners[i];
if (eventConfig.dispatcher == dispatcher
&& eventConfig.eventString == eventString
&& eventConfig.listener == listener
&& eventConfig.useCapture == useCapture
&& eventConfig.eventClass == eventClass)
config = currentListeners[i];
if (config.equalTo(dispatcher, eventString, listener, eventClass, useCapture))
{
return;
}
}

var callback:Function;

if (eventClass != Event)
{
callback = function(event:Event):void
const callback:Function = eventClass == Event
? listener
: function(event:Event):void
{
routeEventToListener(event, listener, eventClass);
};
}
else
{
callback = listener;
}

eventConfig = new EventMapConfig(dispatcher,
config = new EventMapConfig(dispatcher,
eventString,
listener,
eventClass,
callback,
useCapture);

currentListeners.push(eventConfig);
currentListeners.push(config);

if (!_suspended)
{
Expand All @@ -103,24 +94,20 @@ package robotlegs.bender.extensions.localEventMap.impl
useCapture:Boolean = false):void
{
eventClass ||= Event;
var eventConfig:EventMapConfig;

const currentListeners:Vector.<EventMapConfig> = _suspended ? _suspendedListeners : _listeners;
const currentListeners:Vector.<EventMapConfig> = _suspended
? _suspendedListeners
: _listeners;

var i:int = currentListeners.length;
while (i--)
{
eventConfig = currentListeners[i];
// todo: move test to EventMapConfig
if (eventConfig.dispatcher == dispatcher
&& eventConfig.eventString == eventString
&& eventConfig.listener == listener
&& eventConfig.useCapture == useCapture
&& eventConfig.eventClass == eventClass)
var config:EventMapConfig = currentListeners[i];
if (config.equalTo(dispatcher, eventString, listener, eventClass, useCapture))
{
if (!_suspended)
{
dispatcher.removeEventListener(eventString, eventConfig.callback, useCapture);
dispatcher.removeEventListener(eventString, config.callback, useCapture);
}
currentListeners.splice(i, 1);
return;
Expand Down Expand Up @@ -188,7 +175,7 @@ package robotlegs.bender.extensions.localEventMap.impl
}

/*============================================================================*/
/* Private Functions */
/* Protected Functions */
/*============================================================================*/

/**
Expand All @@ -198,7 +185,7 @@ package robotlegs.bender.extensions.localEventMap.impl
* @param listener
* @param originalEventClass
*/
private function routeEventToListener(event:Event, listener:Function, originalEventClass:Class):void
protected function routeEventToListener(event:Event, listener:Function, originalEventClass:Class):void
{
if (event is originalEventClass)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ package robotlegs.bender.extensions.localEventMap.impl
/**
* @private
*/
public function EventMapConfig(dispatcher:IEventDispatcher,
public function EventMapConfig(
dispatcher:IEventDispatcher,
eventString:String,
listener:Function,
eventClass:Class,
Expand All @@ -100,5 +101,19 @@ package robotlegs.bender.extensions.localEventMap.impl
_callback = callback;
_useCapture = useCapture;
}

public function equalTo(
dispatcher:IEventDispatcher,
eventString:String,
listener:Function,
eventClass:Class,
useCapture:Boolean):Boolean
{
return _eventString == eventString
&& _eventClass == eventClass
&& _dispatcher == dispatcher
&& _listener == listener
&& _useCapture == useCapture;
}
}
}

0 comments on commit 0a9e40b

Please sign in to comment.