Skip to content

Commit

Permalink
Added useWeakReference property to Listener tag to improve garbage co…
Browse files Browse the repository at this point in the history
…llection. True by default.

ResponseHandlers now use weakReference internally to improve garbage collection.

Fixed bug in GlobalDispatcher that was firing twice when the event with bubbles true property is dispatched from the application.
  • Loading branch information
yulaar committed Sep 4, 2008
1 parent 0f5765a commit d90043e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/com/asfusion/mate/core/GlobalDispatcher.as
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ Author: Nahuel Foronda, Principal Architect
package com.asfusion.mate.core
{
import com.asfusion.mate.utils.SystemManagerFinder;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventPhase;
import flash.events.IEventDispatcher;

import mx.core.Application;

/**
Expand Down Expand Up @@ -77,8 +79,8 @@ package com.asfusion.mate.core
{
if(!useCapture)
{
popupDispatcher.addEventListener(type, interceptorEventHandler, useCapture, 1000, useWeakReference);
popupDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
popupDispatcher.addEventListener(type, interceptorEventHandler, useCapture, -100, useWeakReference);
popupDispatcher.addEventListener(type, listener, useCapture, -101, useWeakReference);
}
}
}
Expand Down Expand Up @@ -145,7 +147,8 @@ package com.asfusion.mate.core
*/
protected function interceptorEventHandler(event:Event):void
{
if(event.target is DisplayObject && (applicationDispatcher as Sprite).contains(event.target as DisplayObject))
var isApplicationChild:Boolean = (event.target is DisplayObject && (applicationDispatcher as Sprite).contains(event.target as DisplayObject));
if(event.target == applicationDispatcher || isApplicationChild)
{
if(event.eventPhase == EventPhase.BUBBLING_PHASE)
{
Expand Down
31 changes: 27 additions & 4 deletions src/com/asfusion/mate/events/Listener.as
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ package com.asfusion.mate.events
{
dispatcher.removeEventListener(type, _method);
}
dispatcher.addEventListener(type, value, false, priority);
dispatcher.addEventListener(type, value, false, priority, useWeakReference);
methodRegistered = true;
}
_method = value;
Expand All @@ -138,6 +138,29 @@ package com.asfusion.mate.events
updateListeners(type, type);
}

/*-.........................................useWeakReference..........................................*/
private var _useWeakReference:Boolean = true;
/**
* (default = true) — Determines whether the reference to the listener is strong or weak.
* A strong reference (the default) prevents your listener from being garbage-collected.
* A weak reference does not.
* When using modules, it is recomended to use weak references to garbage-collect unused modules
*
* @default true
* */
public function get useWeakReference():Boolean
{
return _useWeakReference;
}
public function set useWeakReference(value:Boolean):void
{
if (_useWeakReference !== value)
{
_useWeakReference = value;
updateListeners(type, type);
}
}

/*-----------------------------------------------------------------------------------------------------------
* Constructor
-------------------------------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -197,7 +220,7 @@ package com.asfusion.mate.events
* Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
* You can register event listeners on all nodes in the display list for a specific type of event, phase, and priority.
*/
public function addEventListener(eventType:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void
public function addEventListener(eventType:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=true):void
{
if(eventType == tagEventType)
{
Expand Down Expand Up @@ -270,15 +293,15 @@ package com.asfusion.mate.events
*/
if(method != null)
{
dispatcher.addEventListener(newType, method,false, priority);
dispatcher.addEventListener(newType, method,false, priority, useWeakReference);
methodRegistered = true;
}
/*
* Checking if we need to register a method defined inline in the tag with the new type and register it if we need it
*/
if(mxmlFunction != null)
{
dispatcher.addEventListener(newType, mxmlFunction,false, priority);
dispatcher.addEventListener(newType, mxmlFunction,false, priority, useWeakReference);
mxmlRegistered = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/asfusion/mate/responses/ResponseHandler.as
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ package com.asfusion.mate.responses
public function addReponderListener(type:String, dispatcher:IEventDispatcher, owner:Dispatcher):void
{
this.owner = owner;
dispatcher.addEventListener(type, response);
dispatcher.addEventListener(type, response, false, 0, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/asfusion/mate/responses/ServiceResponseHandler.as
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ package com.asfusion.mate.responses
public function addReponderListener(type:String, dispatcher:IEventDispatcher, owner:Dispatcher):void
{
this.owner = owner;
dispatcher.addEventListener(type, response);
dispatcher.addEventListener(type, response, false, 0, true);
}

/*-.........................................removeResponder..........................................*/
Expand Down

0 comments on commit d90043e

Please sign in to comment.