Skip to content

Instantly share code, notes, and snippets.

@mikecann
Created August 16, 2010 09:11
Show Gist options
  • Select an option

  • Save mikecann/526676 to your computer and use it in GitHub Desktop.

Select an option

Save mikecann/526676 to your computer and use it in GitHub Desktop.
package
{
import flash.utils.Dictionary;
public class SimpleDispatcher
{
protected var _listeners : Dictionary;
public function SimpleDispatcher(useWeak:Boolean)
{
_listeners = new Dictionary(useWeak);
}
public function add(f:Function) : void
{
_listeners[f] = true;
}
public function dispatch() : void
{
for (var o:* in _listeners)
{
o();
}
}
}
}
package
{
public class SimpleListener
{
public function SimpleListener(d:SimpleDispatcher)
{
d.add(onPing);
}
protected function onPing() : void
{
trace(this+" - ping");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.controls.List;
protected var _dispatcher : SimpleDispatcher = new SimpleDispatcher(true);
protected var _listener : SimpleListener;
protected function onAddListenerClicked(event:MouseEvent):void
{
_listener = new SimpleListener(_dispatcher);
}
protected function onRunGCClicked(event:MouseEvent):void
{
try
{
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
}
catch (e:*) {}
}
protected function onDispatchClicked(event:MouseEvent):void
{
_dispatcher.dispatch();
}
]]>
</fx:Script>
<s:VGroup width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<s:Button label="Add Listener" click="onAddListenerClicked(event)" />
<s:Button label="Run GC" click="onRunGCClicked(event)" />
<s:Button label="Dispatch" click="onDispatchClicked(event)" />
</s:VGroup>
</s:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment