Skip to content

Commit

Permalink
Lots of cleanup for proper disposal of modules for garbage collection…
Browse files Browse the repository at this point in the history
…. ModuleCommandMap moved to a per context instance to allow for multiple mappings of commands at the individual context level. Refactored the semantics of the ModuleMediator syntax sugar to be in line with RL mediator
  • Loading branch information
joelhooks committed May 1, 2010
1 parent 3048c4b commit 9bf3c85
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 49 deletions.
21 changes: 21 additions & 0 deletions src/org/robotlegs/utilities/modular/base/ModuleCommandMap.as
Expand Up @@ -6,6 +6,8 @@
*/
package org.robotlegs.utilities.modular.base
{
import flash.utils.Dictionary;

import org.robotlegs.base.CommandMap;
import org.robotlegs.core.IInjector;
import org.robotlegs.core.IReflector;
Expand All @@ -24,5 +26,24 @@ package org.robotlegs.utilities.modular.base
{
super(eventDispatcher, injector, reflector);
}

public function dispose():void
{
for(var key:Object in eventTypeMap);
{
for each(var value:Object in eventTypeMap[key])
{
if(value is Dictionary)
{
for(var innerKey:Object in value)
{
eventDispatcher.removeEventListener(String(key), value[innerKey]);
delete value[innerKey];
}
}
}
delete eventTypeMap[key];
}
}
}
}
Expand Up @@ -11,6 +11,6 @@ package org.robotlegs.utilities.modular.core

public interface IModuleCommandMap extends ICommandMap
{

function dispose():void;
}
}
5 changes: 3 additions & 2 deletions src/org/robotlegs/utilities/modular/core/IModuleContext.as
Expand Up @@ -7,9 +7,10 @@

package org.robotlegs.utilities.modular.core
{
import org.robotlegs.core.IContext;

public interface IModuleContext
public interface IModuleContext extends IContext
{
function setModuleDispatcher(dispatcher:IModuleEventDispatcher):void;
function dispose():void;
}
}
68 changes: 63 additions & 5 deletions src/org/robotlegs/utilities/modular/mvcs/ModuleContext.as
Expand Up @@ -8,7 +8,9 @@
package org.robotlegs.utilities.modular.mvcs
{
import flash.display.DisplayObjectContainer;
import flash.events.Event;

import org.robotlegs.base.ContextEvent;
import org.robotlegs.core.IInjector;
import org.robotlegs.mvcs.Context;
import org.robotlegs.utilities.modular.base.ModuleCommandMap;
Expand All @@ -23,8 +25,32 @@ package org.robotlegs.utilities.modular.mvcs
* @author Joel Hooks
*
*/
public class ModuleContext extends Context
public class ModuleContext extends Context implements IModuleContext
{
private var _moduleDispatcher:IModuleEventDispatcher;

public function get moduleDispatcher():IModuleEventDispatcher
{
return _moduleDispatcher;
}

public function set moduleDispatcher(value:IModuleEventDispatcher):void
{
_moduleDispatcher = value;
}

private var _moduleCommandMap:IModuleCommandMap;

public function get moduleCommandMap():IModuleCommandMap
{
return _moduleCommandMap || (_moduleCommandMap = new ModuleCommandMap(moduleDispatcher, injector, reflector));
}

public function set moduleCommandMap(value:IModuleCommandMap):void
{
_moduleCommandMap = value;
}

public function ModuleContext(contextView:DisplayObjectContainer=null, autoStartup:Boolean=true, injector:IInjector = null)
{
if(injector)
Expand All @@ -38,10 +64,42 @@ package org.robotlegs.utilities.modular.mvcs
override protected function mapInjections():void
{
super.mapInjections();
if(!injector.hasMapping(IModuleEventDispatcher))
injector.mapSingletonOf(IModuleEventDispatcher, ModuleEventDispatcher);
if(!injector.hasMapping(IModuleCommandMap))
injector.mapSingletonOf(IModuleCommandMap, ModuleCommandMap);
initializeModuleEventDispatcher();
injector.mapValue(IModuleCommandMap, moduleCommandMap);
}

protected function initializeModuleEventDispatcher():void
{
if(injector.hasMapping(IModuleEventDispatcher) )
{
moduleDispatcher = injector.getInstance(IModuleEventDispatcher);
}
else
{
moduleDispatcher = new ModuleEventDispatcher(this);
injector.mapValue(IModuleEventDispatcher, moduleDispatcher);
}
}


protected function dispatchToModules(event:Event):void
{
_moduleDispatcher.dispatchEvent(event);
}

public function dispose():void
{
dispatchEvent(new ContextEvent(ContextEvent.SHUTDOWN));
_moduleCommandMap.dispose();
_moduleCommandMap = null;
_moduleDispatcher = null;
_contextView = null;
_injector = null;
_reflector = null;
_commandMap = null;
_mediatorMap = null;
_viewMap = null;
_eventDispatcher = null;
}
}
}
44 changes: 3 additions & 41 deletions src/org/robotlegs/utilities/modular/mvcs/ModuleMediator.as
Expand Up @@ -31,9 +31,9 @@ package org.robotlegs.utilities.modular.mvcs
* @param event
*
*/
protected function mapRedispatchToModules(eventType:String):void
protected function addModuleListener(type:String, listener:Function, eventClass:Class = null, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = true):void
{
eventMap.mapListener(eventDispatcher, eventType, redispatchToModules);
eventMap.mapListener(moduleDispatcher, type, listener, eventClass, useCapture, priority, useWeakReference);
}

/**
Expand All @@ -46,47 +46,9 @@ package org.robotlegs.utilities.modular.mvcs
* @param event
*
*/
protected function redispatchToModules(event:Event):void
protected function dispatchToModules(event:Event):void
{
moduleDispatcher.dispatchEvent(event);
}

/**
* Map an event type to locally redispatch to all modules within an application.
* This is equivelant to using the <code>dispatch</code> method.
*
* <p/>
* @example The following example maps MyEvent.SOME_EVENT to redispatch to THIS module.
* <listing version="3.0">
* mapRedispatchInternally(MyEvent.SOME_EVENT);
* </listing>
*
* @param event
* @see
*
*/
protected function mapRedispatchInternally(eventType:String):void
{
eventMap.mapListener(moduleDispatcher, eventType, redispatchInternally);
}

/**
* Locally redispatch an event to all modules within an application.
* This is equivelant to using the <code>dispatch</code> method.
*
* <p/>
* @example The following example relays directly.
* <listing version="3.0">
* eventMap.mapEvent(view, MyEvent.SOME_EVENT, redispatchInternally);
* </listing>
*
* @param event
* @see
*
*/
protected function redispatchInternally(event:Event):void
{
eventDispatcher.dispatchEvent(event);
}
}
}

0 comments on commit 9bf3c85

Please sign in to comment.