Skip to content

Instantly share code, notes, and snippets.

@Stray
Created March 9, 2011 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stray/862548 to your computer and use it in GitHub Desktop.
Save Stray/862548 to your computer and use it in GitHub Desktop.
Use instead of the eventMap in robotlegs mediators
package org.robotlegs.mvcs
{
import org.osflash.signals.ISignal;
import flash.utils.Dictionary;
public class SignalMap implements ISignalMap
{
protected var _handlersBySignal:Dictionary;
public function SignalMap()
{
_handlersBySignal = new Dictionary();
}
public function addToSignal(signal:ISignal, handler:Function):void
{
signal.add(handler);
if(_handlersBySignal[signal] == null)
{
_handlersBySignal[signal] = new <Function>[handler];
}
else
{
_handlersBySignal[signal].push(handler);
}
}
public function removeFromSignal(signal:ISignal, handler:Function):void
{
signal.remove(handler);
if((_handlersBySignal[signal] == null) || (_handlersBySignal[signal].length == 0))
{
return;
}
var handlerIndex:int = _handlersBySignal[signal].indexOf(handler);
if(handlerIndex > -1)
{
_handlersBySignal[signal].splice(handlerIndex, 1);
}
}
public function removeAll():void
{
for( var signal:Object in _handlersBySignal)
{
var knownSignal:ISignal = signal as ISignal;
var handlers:Vector.<Function> = _handlersBySignal[signal];
for each(var handler:Function in handlers)
{
knownSignal.remove(handler);
}
}
_handlersBySignal = new Dictionary();
}
}
}
package org.robotlegs.mvcs
{
import org.robotlegs.mvcs.Mediator;
import org.osflash.signals.ISignal;
public class SignalMediator extends Mediator
{
protected var _signalMap:ISignalMap;
public function SignalMediator()
{
super();
}
override public function preRemove():void
{
signalMap.removeAll();
super.preRemove();
}
protected function get signalMap():ISignalMap
{
return _signalMap ||= new SignalMap();
}
protected function addToSignal(signal:ISignal, handler:Function):void
{
signalMap.addToSignal(signal, handler);
}
}
}
@shirshir
Copy link

shirshir commented Mar 9, 2011

Hi Stray, handlerIndex on line https://gist.github.com/862548#L37 should be an int.

@Stray
Copy link
Author

Stray commented Mar 9, 2011

You are correct!

@robertpenner
Copy link

Just for fun (not actually recommending this):

(_handlersBySignal[signal] ||= new <Function>[]).push(handler)

@Stray
Copy link
Author

Stray commented Mar 10, 2011

Ha! Yes, it's hard to know how much is too much.

I did think about switching to using the length property instead of push - but decided that push is clearer to read and actually adding signals shouldn't be happening too often or the whole mediatorMap deal will crunch your performance anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment