<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog.betabong.com &#187; EventDispatcher</title>
	<atom:link href="http://blog.betabong.com/tag/eventdispatcher/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.betabong.com</link>
	<description></description>
	<lastBuildDate>Thu, 02 Feb 2012 10:45:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Nested inner functions can be evil</title>
		<link>http://blog.betabong.com/2008/07/21/nested-inner-functions-can-be-evil/</link>
		<comments>http://blog.betabong.com/2008/07/21/nested-inner-functions-can-be-evil/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 07:53:29 +0000</pubDate>
		<dc:creator>betabong</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Frontend]]></category>
		<category><![CDATA[EventDispatcher]]></category>

		<guid isPermaLink="false">http://blog.betabong.com/?p=39</guid>
		<description><![CDATA[<p>Sometimes it would be quite comfortable (from a programmer&#8217;s perspective) to use nested inner functions. But they are a potential source for nasty problems. Let&#8217;s take the following simple scenario:<span id="more-39"></span></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&#60;?xml version=<span style="color: #ff0000;">&#34;1.0&#34;</span> encoding=<span style="color: #ff0000;">&#34;utf-8&#34;</span>?<span style="color: #7400FF;">&#62;</span></span>
<span</pre></div></div><p>&#8230; <a href="http://blog.betabong.com/2008/07/21/nested-inner-functions-can-be-evil/" class="read_more">Read more</a></p>
Related posts:<ol>
<li><a href='http://blog.betabong.com/2008/09/26/weak-method-closure/' rel='bookmark' title='Weak Method Closure'>Weak Method Closure</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Sometimes it would be quite comfortable (from a programmer&#8217;s perspective) to use nested inner functions. But they are a potential source for nasty problems. Let&#8217;s take the following simple scenario:<span id="more-39"></span></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;create()&quot;</span> click=<span style="color: #ff0000;">&quot;destroy()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">		&lt;![CDATA[</span>
<span style="color: #339933;">			private var dispatcher : Sprite;</span>
&nbsp;
<span style="color: #339933;">			private function create() : void {</span>
<span style="color: #339933;">				dispatcher = new Sprite();</span>
<span style="color: #339933;">				// listen to inner function</span>
<span style="color: #339933;">				dispatcher.addEventListener( Event.ENTER_FRAME , function( e : Event ) : void { trace( e.type ); } );</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">			private function destroy() : void {</span>
<span style="color: #339933;">				// this should free created dispatcher from memory</span>
<span style="color: #339933;">				dispatcher = null;</span>
<span style="color: #339933;">				// what we would need is:</span>
<span style="color: #339933;">				// dispatcher.destroy() or dispatcher.removeEventListener( Event.ENTER_FRAME )</span>
<span style="color: #339933;">				// or dispatcher.removeAllEventListeners()</span>
<span style="color: #339933;">			}</span>
<span style="color: #339933;">		]]&gt;</span>
<span style="color: #339933;">	&lt;/mx:Script&gt;</span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The dispatcher will just live on. The mean thing is that in method «destroy» we don&#8217;t even have a chance to get rid of the sprite anymore – it just lives on and on (and dispatches its event every time it enters a frame to the listener function.) So 3 objects will just not be freed in memory because of our inner function listener: 1. the dispatcher object, 2. the listener function (the inner function) and 3. the creating object, because the listener function (or its method closure) is bound to the creating method create() scope.</p>
<p>We also can&#8217;t use the addEventListener&#8217;s flag to use weak references, because the listener method wouldn&#8217;t have any other references and would «dye» instantely. So what you should do right now is something like that:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;create()&quot;</span> click=<span style="color: #ff0000;">&quot;destroy()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">		&lt;![CDATA[</span>
<span style="color: #339933;">			private var dispatcher : Sprite;</span>
&nbsp;
<span style="color: #339933;">			private function create() : void {</span>
<span style="color: #339933;">				dispatcher = new Sprite();</span>
<span style="color: #339933;">				dispatcher.addEventListener( Event.ENTER_FRAME , handler );</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">			private function destroy() : void {</span>
<span style="color: #339933;">				dispatcher.removeEventListener( Event.ENTER_FRAME , handler );</span>
<span style="color: #339933;">				dispatcher = null;</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">			private function handler( event : Event ) : void {</span>
<span style="color: #339933;">				trace( e.type );</span>
<span style="color: #339933;">			}</span>
<span style="color: #339933;">		]]&gt;</span>
<span style="color: #339933;">	&lt;/mx:Script&gt;</span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>That&#8217;s clean code. Good bye dispatcher, good bye sprite! But sometimes it can be quite difficult to maintain this. I personally would wish that actionscript objects, especially the EventDispatcher class, would provide a destroy() method. Calling it gets rid of all event listeners and also would unlisten to everything it has listened to. And for DisplayObjects it would also perform other operations like removing itself from its parent and destroying all children.</p>
<p>I&#8217;d also like to see the following method like this in EventDispatcher:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">EventDispatcher.<span style="color: #0066CC;">removeListener</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">type</span> : <span style="color: #0066CC;">String</span> = <span style="color: #000000; font-weight: bold;">null</span> , listener : <span style="color: #000000; font-weight: bold;">Function</span> = <span style="color: #000000; font-weight: bold;">null</span> , useCapture : <span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// so this would remove all listeners</span>
EventDispatcher.<span style="color: #0066CC;">removeListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// and this all listeners to enter frame</span>
EventDispatcher.<span style="color: #0066CC;">removeListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">ENTER_FRAME</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I get around many issues by implementing something like a IEventListener interface:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">interface</span> IEventListener <span style="color: #66cc66;">&#123;</span>
<span style="color: #000000; font-weight: bold;">function</span> listenTo<span style="color: #66cc66;">&#40;</span> dispatcher : IEventDispatcher , <span style="color: #0066CC;">type</span> : <span style="color: #0066CC;">String</span> , listener : <span style="color: #000000; font-weight: bold;">Function</span> , useCapture : <span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>;
functon unlistenTo<span style="color: #66cc66;">&#40;</span> dispatcher : IEventDispatcher , <span style="color: #0066CC;">type</span> : <span style="color: #0066CC;">String</span> , listener : <span style="color: #000000; font-weight: bold;">Function</span> , useCapture : <span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>;
<span style="color: #000000; font-weight: bold;">function</span> destroy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>; <span style="color: #808080; font-style: italic;">// will remove myself as event listener from all registered dispatchers</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Way to go :-)</p>
<p>Related posts:<ol>
<li><a href='http://blog.betabong.com/2008/09/26/weak-method-closure/' rel='bookmark' title='Weak Method Closure'>Weak Method Closure</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.betabong.com/2008/07/21/nested-inner-functions-can-be-evil/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: blog.betabong.com @ 2012-02-05 07:35:13 -->
