Skip to content

Commit

Permalink
Added a method that sets up the null listener automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stray committed Nov 13, 2010
1 parent 26e2703 commit 7eb9cb2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
Binary file modified bin/RelaxedEventMapRunner.swf
Binary file not shown.
20 changes: 19 additions & 1 deletion src/org/robotlegs/base/RelaxedEventMap.as
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ package org.robotlegs.base {
public class RelaxedEventMap extends EventMap implements IRelaxedEventMap {

protected var eventsReceivedByClass:Dictionary;
protected var emptyListeners:Array;

public function RelaxedEventMap(eventDispatcher:IEventDispatcher) {
super(eventDispatcher);
eventsReceivedByClass = new Dictionary();
emptyListeners = [];
}

public function mapRelaxedListener(type:String, listener:Function, eventClass:Class = null, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = true):void
Expand All @@ -29,14 +31,30 @@ package org.robotlegs.base {

mapListener(this.eventDispatcher, type, listener, eventClass, useCapture, priority, useWeakReference);
}

public function rememberEvent(type:String, eventClass:Class = null):void
{
var emptyListener:Function = function():void { };
emptyListeners.push(emptyListener);
mapListener(this.eventDispatcher, type, emptyListener, eventClass);
}

override protected function routeEventToListener(event:Event, listener:Function, originalEventClass:Class):void
{
if (event is originalEventClass)
{
eventsReceivedByClass[originalEventClass] ||= new Dictionary();
eventsReceivedByClass[originalEventClass][event.type] = event;
listener(event);

if(emptyListeners.indexOf(listener) == -1)
{
listener(event);
}
else
{
unmapListener(this.eventDispatcher, event.type, listener, originalEventClass);
emptyListeners.splice(emptyListeners.indexOf(listener), 1);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/org/robotlegs/core/IRelaxedEventMap.as
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import flash.events.IEventDispatcher;
public interface IRelaxedEventMap
{
function mapRelaxedListener(type:String, listener:Function, eventClass:Class = null, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = true):void;

function rememberEvent(type:String, eventClass:Class = null):void;
}

}
17 changes: 17 additions & 0 deletions test/org/robotlegs/base/RelaxedEventMapTest.as
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ package org.robotlegs.base {
eventDispatcher.dispatchEvent(new SampleEventA(SampleEventA.SOMETHING_HAPPENED));
}

public function test_rememberEvent_stores_the_event():void {
instance.rememberEvent(SampleEventA.SOMETHING_HAPPENED, SampleEventA);
eventDispatcher.dispatchEvent(new SampleEventA(SampleEventA.SOMETHING_HAPPENED));
var handler:Function = addAsync(check_past_event_fired_when_listener_added, 100);
instance.mapRelaxedListener(SampleEventA.SOMETHING_HAPPENED, handler, SampleEventA);
}

public function test_rememberEvent_listener_is_cleaned_up():void {
instance.rememberEvent(SampleEventA.SOMETHING_HAPPENED, SampleEventA);
eventDispatcher.dispatchEvent(new SampleEventA(SampleEventA.SOMETHING_HAPPENED));
eventDispatcher.dispatchEvent(new SampleEventA(SampleEventA.SOMETHING_HAPPENED));
// unfortunately because the listener is by design empty, I can't find a way to test this automatically
// so it's a trace based confirmation
assertTrue("Manually verified that when a trace is placed in the null function used it doesn't trace", true);
}


private function somethingHappenedHandler(e:SampleEventA):void
{
// nada
Expand Down

0 comments on commit 7eb9cb2

Please sign in to comment.