Skip to content

Instantly share code, notes, and snippets.

@Stray
Created January 19, 2011 21:42
Show Gist options
  • Save Stray/786925 to your computer and use it in GitHub Desktop.
Save Stray/786925 to your computer and use it in GitHub Desktop.
The actual service - map as a singleton and use getInstance to make it come to life
public class InterestingEventTypesLookup {
protected var _eventClassesByType:Dictionary;
public function get eventClassesByType():Dictionary
{
return _eventClassesByType ||= new Dictionary();
}
public function appendEventInterest(eventClass:Class, eventType:String):void
{
eventClassesByType[eventType] = eventClass;
}
}
public class ConfigureLoggingCommand {
[Inject]
public var interestingEventTypes:InterestingEventTypesLookup;
public function execute():void
{
// for each event you care about
interestingEventTypes.appendEventInterest(SomeEvent, SomeEvent.SOMETHING_WENT_DOWN);
// then bring the actual logger to life
injector.getInstance(LogginService);
}
}
public class LoggingService extends Actor {
[Inject]
public var interestingEventTypes:InterestingEventTypesLookup;
protected var eventMap:EventMap; // copy the event map imp from Mediator
public function LoggingService()
{
registerListeners();
}
protected function registerListeners():void
{
var eventClassesByType:Dictionary = interestingEventTypes.eventClassesByType;
for (var eventType:Object in eventClassesByType)
{
var eventTypeStr:String = eventType as String;
var eventClass:String = eventClassesByType[eventType];
eventMap.mapListener(eventDispatcher, eventTypeStr, logEvent, eventClass);
}
}
protected function logEvent(e:Event):void
{
// do your thang with the event
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment