Skip to content

Commit

Permalink
removes double mapping of event as typed and untyped
Browse files Browse the repository at this point in the history
  • Loading branch information
creynders committed Apr 18, 2013
1 parent 44b074e commit e76e15f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ package robotlegs.bender.extensions.eventCommandMap.impl
{
import flash.events.Event;
import flash.events.IEventDispatcher;

import org.swiftsuspenders.Injector;

import robotlegs.bender.extensions.commandCenter.api.ICommandExecutor;
import robotlegs.bender.extensions.commandCenter.api.ICommandMappingList;
import robotlegs.bender.extensions.commandCenter.api.ICommandTrigger;
Expand Down Expand Up @@ -41,7 +39,7 @@ package robotlegs.bender.extensions.eventCommandMap.impl

private var _mappings:ICommandMappingList;

private var _executor: ICommandExecutor;
private var _executor:ICommandExecutor;

/*============================================================================*/
/* Constructor */
Expand Down Expand Up @@ -96,15 +94,21 @@ package robotlegs.bender.extensions.eventCommandMap.impl
private function eventHandler(event:Event):void
{
const eventConstructor:Class = event["constructor"] as Class;
if (_eventClass && eventConstructor != _eventClass)
var payloadEventClass:Class;
//not pretty, but optimized to avoid duplicate checks and shortest paths
if (eventConstructor == _eventClass || (!_eventClass))
{
payloadEventClass = eventConstructor;
}
else if (_eventClass == Event)
{
payloadEventClass = _eventClass;
}
else
{
return;
}
const payload : CommandPayload = new CommandPayload([event], [Event]);
if (eventConstructor != Event)
payload.addPayload(event, _eventClass || eventConstructor);

_executor.executeCommands(_mappings.getList(), payload);
_executor.executeCommands(_mappings.getList(), new CommandPayload([event], [payloadEventClass]));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ package robotlegs.bender.extensions.eventCommandMap.impl
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

import org.hamcrest.assertThat;
import org.hamcrest.collection.array;
import org.hamcrest.core.not;
import org.hamcrest.object.equalTo;
import org.hamcrest.object.instanceOf;
import org.swiftsuspenders.Injector;

import robotlegs.bender.extensions.commandCenter.dsl.ICommandMapper;
import robotlegs.bender.extensions.commandCenter.dsl.ICommandUnmapper;
import robotlegs.bender.extensions.commandCenter.support.CallbackCommand;
Expand Down Expand Up @@ -132,7 +134,7 @@ package robotlegs.bender.extensions.eventCommandMap.impl
{
injectedEvent = command.event;
});
subject.map(SupportEvent.TYPE1)
subject.map(SupportEvent.TYPE1, Event)
.toCommand(EventInjectedCallbackCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
Expand All @@ -142,22 +144,20 @@ package robotlegs.bender.extensions.eventCommandMap.impl
[Test]
public function event_is_passed_to_execute_method():void
{
var actualEvent:Event;
var actualTypedEvent:SupportEvent;
injector.map(Function, 'executeCallback').toValue(function(event:Event, typedEvent:SupportEvent):void
var actualEvent:SupportEvent;
injector.map(Function, 'executeCallback').toValue(function(event:SupportEvent):void
{
actualEvent = event;
actualTypedEvent = typedEvent;
});
subject.map(SupportEvent.TYPE1, SupportEvent)
.toCommand(EventParametersCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat([actualEvent, actualTypedEvent], array(event, event));
assertThat(actualEvent, equalTo(event));
}

[Test]
public function specified_typed_event_is_injected_into_command():void
public function concretely_specified_typed_event_is_injected_into_command_as_typed_event():void
{
var injectedEvent:SupportEvent = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
Expand All @@ -172,7 +172,22 @@ package robotlegs.bender.extensions.eventCommandMap.impl
}

[Test]
public function unspecified_typed_event_is_injected_into_command():void
public function abstractly_specified_typed_event_is_injected_into_command_as_untyped_event():void
{
var injectedEvent:Event = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.untypedEvent;
});
subject.map(SupportEvent.TYPE1, Event)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}

[Test]
public function unspecified_typed_event_is_injected_into_command_as_typed_event():void
{
var injectedEvent:SupportEvent = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
Expand All @@ -186,6 +201,38 @@ package robotlegs.bender.extensions.eventCommandMap.impl
assertThat(injectedEvent, equalTo(event));
}

[Test]
public function unspecified_untyped_event_is_injected_into_command_as_untyped_event():void
{
const eventType : String = 'eventType';
var injectedEvent:Event = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.untypedEvent;
});
subject.map(eventType)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:Event = new Event(eventType);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}

[Test]
public function specified_untyped_event_is_injected_into_command_as_untyped_event():void
{
const eventType : String = 'eventType';
var injectedEvent:Event = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.untypedEvent;
});
subject.map(eventType, Event)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:Event = new Event(eventType);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}

[Test]
public function command_does_not_execute_when_incorrect_eventType_dispatched():void
{
Expand Down Expand Up @@ -319,7 +366,7 @@ package robotlegs.bender.extensions.eventCommandMap.impl
injectedEvent = guard.event;
});
subject
.map(SupportEvent.TYPE1)
.map(SupportEvent.TYPE1, Event)
.toCommand(NullCommand)
.withGuards(EventInjectedCallbackGuard);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
Expand Down Expand Up @@ -431,10 +478,10 @@ class SupportEventTriggeredSelfReportingCallbackCommand
/* Public Properties */
/*============================================================================*/

[Inject]
public var event:Event;
[Inject(optional=true)]
public var untypedEvent:Event;

[Inject]
[Inject(optional=true)]
public var typedEvent:SupportEvent;

[Inject(name="executeCallback")]
Expand Down Expand Up @@ -464,8 +511,8 @@ class EventParametersCommand
/* Public Functions */
/*============================================================================*/

public function execute(event:Event, typedEvent:SupportEvent):void
public function execute(event:SupportEvent):void
{
callback(event, typedEvent);
callback(event);
}
}

0 comments on commit e76e15f

Please sign in to comment.