Last active
December 20, 2015 13:19
-
-
Save Ondina/6138055 to your computer and use it in GitHub Desktop.
ModuleConnectionConfigurator
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 robotlegs.bender.extensions.modularity.impl | |
{ | |
import flash.events.Event; | |
import flash.events.IEventDispatcher; | |
import flash.utils.Dictionary; | |
import robotlegs.bender.framework.impl.UID; | |
public class ModularRouter | |
{ | |
private var _localDispatcher:IEventDispatcher; | |
private var _parentDispatcher:IEventDispatcher; | |
private var _received:Dictionary; | |
private var _receivingEventTypes:Array; | |
private var _relayingEventTypes:Array; | |
private var _sender:ModularSender; | |
private var _active:Boolean; | |
private const _uid:String = UID.create(this); | |
public function ModularRouter(localDispatcher:IEventDispatcher, parentDispatcher:IEventDispatcher) | |
{ | |
_localDispatcher = localDispatcher; | |
_parentDispatcher = parentDispatcher; | |
_received = new Dictionary(); | |
_receivingEventTypes = []; | |
_relayingEventTypes = []; | |
_sender = new ModularSender(); | |
} | |
private function interceptEvent(event:Event):void | |
{ | |
if (_received[event.type]) | |
{ | |
_received[event.type] = false; | |
} | |
else | |
{ | |
_sender.uid = _uid; | |
_sender.eventClass = event; | |
_parentDispatcher.dispatchEvent(new ModularRouterEvent(ModularRouterEvent.LOCAL_TO_PARENT, _sender)); | |
} | |
} | |
private function distributeEvent(event:ModularRouterEvent):void | |
{ | |
_sender = event.sender; | |
if (_uid != _sender.uid && _receivingEventTypes.indexOf(_sender.eventClass.type) > -1) | |
{ | |
_received[_sender.eventClass.type] = true; | |
_localDispatcher.dispatchEvent(_sender.eventClass); | |
} | |
} | |
public function start():void | |
{ | |
if (!_active) | |
{ | |
_active = true; | |
addListeners(); | |
} | |
} | |
public function stop():void | |
{ | |
if (_active) | |
{ | |
_active = false; | |
removeListeners(); | |
} | |
} | |
public function destroy():void | |
{ | |
removeListeners(); | |
removeReceived(); | |
_relayingEventTypes = []; | |
_receivingEventTypes = []; | |
_parentDispatcher = null; | |
_localDispatcher = null; | |
_sender = null; | |
} | |
public function addRelayType(type:String):void | |
{ | |
if (_relayingEventTypes.indexOf(type) == -1) | |
{ | |
_relayingEventTypes.push(type); | |
addListener(type); | |
} | |
} | |
private function addListeners():void | |
{ | |
for each (var type:String in _relayingEventTypes) | |
{ | |
addListener(type); | |
} | |
_parentDispatcher.addEventListener(ModularRouterEvent.LOCAL_TO_PARENT, distributeEvent); | |
} | |
public function addListener(type:String):void | |
{ | |
_received[type] = false; | |
_localDispatcher.addEventListener(type, interceptEvent); | |
} | |
public function addReceiveType(type:String):void | |
{ | |
if (_receivingEventTypes.indexOf(type) == -1) | |
_receivingEventTypes.push(type); | |
} | |
private function removeListeners():void | |
{ | |
for each (var type:String in _relayingEventTypes) | |
{ | |
_received[type] = false; | |
_localDispatcher.removeEventListener(type, interceptEvent); | |
} | |
_parentDispatcher.removeEventListener(ModularRouterEvent.LOCAL_TO_PARENT, distributeEvent); | |
} | |
private function removeReceived():void | |
{ | |
for each (var type:String in _received) | |
{ | |
delete _received[type]; | |
} | |
_received = null; | |
} | |
} | |
} |
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
//------------------------------------------------------------------------------ | |
// Copyright (c) 2009-2013 the original author or authors. All Rights Reserved. | |
// | |
// NOTICE: You are permitted to use, modify, and distribute this file | |
// in accordance with the terms of the license agreement accompanying it. | |
//------------------------------------------------------------------------------ | |
package robotlegs.bender.extensions.modularity.impl | |
{ | |
import flash.events.IEventDispatcher; | |
import robotlegs.bender.extensions.modularity.dsl.IModuleConnectionAction; | |
/** | |
* @private | |
*/ | |
public class ModuleConnectionConfigurator implements IModuleConnectionAction | |
{ | |
/*============================================================================*/ | |
/* Private Properties */ | |
/*============================================================================*/ | |
private var _modularRouter:ModularRouter; | |
/*============================================================================*/ | |
/* Constructor */ | |
/*============================================================================*/ | |
/** | |
* @private | |
*/ | |
public function ModuleConnectionConfigurator(localDispatcher:IEventDispatcher, parentDispatcher:IEventDispatcher) | |
{ | |
_modularRouter = new ModularRouter(localDispatcher, parentDispatcher); | |
_modularRouter.start(); | |
} | |
/*============================================================================*/ | |
/* Public Functions */ | |
/*============================================================================*/ | |
/** | |
* @inheritDoc | |
*/ | |
public function relayEvent(eventType:String):IModuleConnectionAction | |
{ | |
_modularRouter.addRelayType(eventType); | |
return this; | |
} | |
public function receiveEvent(eventType:String):IModuleConnectionAction | |
{ | |
_modularRouter.addReceiveType(eventType); | |
return this; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function suspend():void | |
{ | |
_modularRouter.stop(); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function resume():void | |
{ | |
_modularRouter.start(); | |
} | |
public function destroy():void | |
{ | |
_modularRouter.destroy(); | |
_modularRouter = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment