Skip to content

Instantly share code, notes, and snippets.

@jmreidy
Created December 15, 2008 15:17
Show Gist options
  • Save jmreidy/35980 to your computer and use it in GitHub Desktop.
Save jmreidy/35980 to your computer and use it in GitHub Desktop.
Flex debug tool that allows you to set a value which will force the app to enter the debugger. For example, when iterating through an array, have Flex always enter the debugger when the value is second object in the array.
package util
{
import flash.debugger.enterDebugger;
public class BreakingObserver extends Observer
{
private var _handler : Function;
private var _source : Object;
//optional value to check for entering the debugger
public var checkValue:Object;
[Inspectable( enumeration="true,false" ) ]
public var ignoreNulls:Boolean = true;
public function BreakingObserver()
{
super();
setHandler();
}
override public function get handler() : Function
{
return _handler;
}
private function breakpoint( sourceValue:Object ):void
{
//if a checkValue has been provided, use its value to determine whether we enter the debugger
if ( checkValue != null )
{
if ( checkValue === sourceValue )
enterDebugger();
}
//else, enter the debugger automatically
else
{
enterDebugger();
}
}
private function setHandler() : void
{
_handler = breakpoint;
isHandlerInitialized = true;
if( isHandlerInitialized && isSourceInitialized )
{
callHandler();
}
}
override public function get source() : Object
{
return _source;
}
public function set source( value:Object ) : void
{
if ( ignoreNulls && value == null ) return;
_source = value;
isSourceInitialized = true;
if( isHandlerInitialized && isSourceInitialized )
{
callHandler();
}
}
}
}
package util
{
import mx.core.Application;
import mx.core.UIComponent;
public class Observer
{
protected var isHandlerInitialized : Boolean = false;
protected var isSourceInitialized : Boolean = false;
public function get handler() : Function
{
return null;
}
public function get source() : Object
{
return null;
}
public function execute( method : Function, ...params : Array ) : Object
{
var returnValue : Object;
try
{
returnValue = method.apply( null, params );
}
catch( e : Error )
{
delay( e );
}
return returnValue;
}
protected function callHandler() : void
{
try
{
handler.call( null, source );
}
catch( e : Error )
{
delay( e );
}
}
protected function delay( e : Error ) : void
{
UIComponent( Application.application ).callLater( throwException, [ e ] );
}
private function throwException( e : Error ) : void
{
throw e;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:local="*" creationComplete="handleCreationComplete( event )"
height="100%" width="100%" xmlns:util="util.*">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.controls.Alert;
private var testStringArray:Array = [ "testString1", "testString2", "testString3" ];
[Bindable] private var selectedObject:Object;
public function handleCreationComplete( event:FlexEvent ):void
{
for each ( var test:String in testStringArray )
{
textBox.text = test;
}
for each ( var obj:Object in testObjectArray )
{
selectedObject = obj;
}
}
]]>
</mx:Script>
<mx:Array id="testObjectArray" >
<mx:Object id="testObj1" text="testObj1" />
<mx:Object id="testObj2" text="testObj2" />
<mx:Object id="testObj3" text="testObj3" />
</mx:Array>
<util:BreakingObserver id="break1" source="{ textBox.text }" checkValue="testString2" />
<util:BreakingObserver id="break2" source="{ selectedObject }" checkValue="{ testObj2 }" ignoreNulls="false" />
<mx:Text id="textBox" />
</mx:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment