Created
March 9, 2011 17:03
-
-
Save Stray/862548 to your computer and use it in GitHub Desktop.
Use instead of the eventMap in robotlegs mediators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
You are correct!
Just for fun (not actually recommending this):
(_handlersBySignal[signal] ||= new <Function>[]).push(handler)
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
Hi Stray, handlerIndex on line https://gist.github.com/862548#L37 should be an int.