<?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>Destroy Today &#187; Code</title>
	<atom:link href="http://destroytoday.com/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://destroytoday.com/blog</link>
	<description>Jonnie is loving SF.</description>
	<lastBuildDate>Fri, 27 Aug 2010 17:03:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>KonamiCode added to DestroyFramework</title>
		<link>http://destroytoday.com/blog/2010/08/konamicode-added-to-destroyframework/</link>
		<comments>http://destroytoday.com/blog/2010/08/konamicode-added-to-destroyframework/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 06:59:05 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Announcement]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesome]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1994</guid>
		<description><![CDATA[Sure, I could have spent tonight working on useful features for DestroyTwitter, but no, I wrote a KonamiCode class. To use it, simply instantiate it with a stage and you&#8217;re good to go. On a related note, I realized I&#8217;ve been neglecting GitHub for months, but now I&#8217;m back! I also plan to return to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/destroytoday/DestroyFramework/blob/master/src/com/destroytoday/desktop/KonamiCode.as"><img src="http://destroytoday.com/blog/wp-content/uploads/2010/08/konami_code_2.gif" alt="" title="konami_code_2" width="645" height="480" class="alignnone size-full wp-image-2001" /></a></p>
<p>Sure, I could have spent tonight working on useful features for DestroyTwitter, but no, I wrote a <a href="http://github.com/destroytoday/DestroyFramework/blob/master/src/com/destroytoday/desktop/KonamiCode.as" target="_blank">KonamiCode class</a>. To use it, simply instantiate it with a stage and you&#8217;re good to go.</p>
<p><script src="http://gist.github.com/547084.js?file=KonamiCodeTest.as"></script></p>
<p>On a related note, I realized I&#8217;ve been neglecting <a href="http://github.com/destroytoday" target="_blank">GitHub</a> for months, but now I&#8217;m back! I also plan to return to the experimentation game after reminiscing about my <a href="http://destroytoday.com/v2/bin/lab/convex_hull/convex_hull.swf" target="_blank">convex hull</a>, <a href="http://destroytoday.com/v2/bin/lab/delaunay/delaunay.swf" target="_blank">delaunay triangulation, and voronoi</a> studies at work today. When you develop apps all day, writing experiments can be a breath of fresh air, allowing a level of creativity you simply can&#8217;t get with apps.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/08/konamicode-added-to-destroyframework/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Unit testing Signal implementation</title>
		<link>http://destroytoday.com/blog/2010/03/unit-testing-signal-implementation/</link>
		<comments>http://destroytoday.com/blog/2010/03/unit-testing-signal-implementation/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 14:30:49 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Quick Fix]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1785</guid>
		<description><![CDATA[Robert Penner&#8217;s Signals has made my life so much easier these past few months. It avoids what I hate about Events while including features Events desperately need. There is one downside I have come across, which is mainly due to human error—making sure to dispatch a Signal with the correct value objects. If you set [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.robertpenner.com/" target="_blank">Robert Penner&#8217;s</a> <a href="http://github.com/robertpenner/as3-signals" target="_blank">Signals</a> has made my life so much easier these past few months. It avoids what I hate about Events while including features Events desperately need. There is one downside I have come across, which is mainly due to human error—making sure to dispatch a Signal with the correct value objects. If you set a Signal&#8217;s value classes like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> const timeChanged<span style="color: #000000; font-weight: bold;">:</span>Signal = <span style="color: #0033ff; font-weight: bold;">new</span> Signal<span style="color: #000000;">&#40;</span><span style="color: #004993;">int</span>, <span style="color: #004993;">int</span>, <span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// hour, minute, second</span></pre></div></div>

<p>&#8230;and dispatch it with incorrect value objects:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">timeChanged.dispatch<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1269874749</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// unix timestamp</span></pre></div></div>

<p>&#8230;Flash Builder throws an argument RTE. Since the RTE is thrown on call of dispatch and not at compile time, there is a possibility of missing the error, leaving your users with an unfortunate experience.</p>
<p>Now, this is easy to avoid by simply double checking your code, but if you&#8217;re working on a team, you can&#8217;t ensure this way that everything remains perfectly intact. You can, however, guarantee each signal fires correctly by unit testing. Sure, Robert has unit tested Signals to the max, but he hasn&#8217;t unit tested your implementation.</p>
<p>To do this, simply call dispatch of each Signal within a test method. The Signal will notify the runner if something is not right, indicating the line number and expected argument type as well. If you use a Signal bus to communicate throughout your app, your test will look something like this:</p>
<p><script src="http://gist.github.com/347842.js?file=ApplicationSignalBusTest.as"></script></p>
<p>It&#8217;s that easy. Now you will know for sure if your Signal implementation is in tip top shape. All you need to do is run your unit test.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/03/unit-testing-signal-implementation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Displaying a context menu via hotkey crashes AIR</title>
		<link>http://destroytoday.com/blog/2010/03/displaying-a-context-menu-via-hotkey-crashes-air/</link>
		<comments>http://destroytoday.com/blog/2010/03/displaying-a-context-menu-via-hotkey-crashes-air/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 16:05:20 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1786</guid>
		<description><![CDATA[I love hotkeys and I try to build them into all of my apps. They greatly improve the desktop app experience, especially for the anti-mouse crowd. I recently thought of a handy way of using a hotkey—display a context menu upon pressing the spacebar with an item selected. This feature would be so sweet&#8230; if [...]]]></description>
			<content:encoded><![CDATA[<p>I love hotkeys and I try to build them into all of my apps. They greatly improve the desktop app experience, especially for the anti-mouse crowd. I recently thought of a handy way of using a hotkey—display a context menu upon pressing the spacebar with an item selected. This feature would be so sweet&#8230; if it worked.</p>
<p>I quickly typed up the implementation and tried it. Expecting to see a context menu, I was dumbfounded to see ADL crash without a single RTE stack trace. Here&#8217;s an example of the code that causes the crash every time:</p>
<p><script src="http://gist.github.com/346817.js?file=ContextMenuHotkeyBug.as"></script></p>
<p>At first, I thought it might be an ADL issue, so I compiled the AIR file, installed it, and the insanity continued. Instead of crashing instantly as the app did running on ADL, it first displayed this alert:</p>
<p><img class="alignnone size-full wp-image-1787" title="contextmenuhotkeybug" src="http://destroytoday.com/blog/wp-content/uploads/2010/03/contextmenuhotkeybug1.gif" alt="contextmenuhotkeybug" width="645" height="212" /></p>
<p>Let me known, I&#8217;m running the most recent build of AIR. Upon clicking &#8216;OK&#8217;, the app quits. I can run the app all day and a soon as I trigger the context menu to display, this alert appears. I checked the Console and found this:</p>
<p><script src="http://gist.github.com/346817.js?file=ContextMenuHotkeyBug.log"></script></p>
<p>I also added the full crash reports in the <a href="http://gist.github.com/346817" target="_blank">Gist</a>. I&#8217;m not sure what it all means, but I filed the bug with Adobe and can only hope it&#8217;s fixed sooner than later—this feature would be too cool.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/03/displaying-a-context-menu-via-hotkey-crashes-air/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using setter injection to provide a view for base mediators in Robotlegs</title>
		<link>http://destroytoday.com/blog/2010/03/using-setter-injection-to-provide-a-view-for-base-mediators-in-robotlegs/</link>
		<comments>http://destroytoday.com/blog/2010/03/using-setter-injection-to-provide-a-view-for-base-mediators-in-robotlegs/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 16:46:39 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1777</guid>
		<description><![CDATA[Yesterday, I came across an issue with an app I&#8217;m developing with Robotlegs. The app uses what I call &#8220;canvases&#8221; to represent each section. There&#8217;s some similar functionality between the canvases, so I decided to create a base mediator and extend it for the more specific functionality. A problem arose when I needed to access [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I came across an issue with an app I&#8217;m developing with Robotlegs. The app uses what I call &#8220;canvases&#8221; to represent each section. There&#8217;s some similar functionality between the canvases, so I decided to create a base mediator and extend it for the more specific functionality. A problem arose when I needed to access the view in the base mediator, while injecting it as the subclass in the extending mediator. At first, I tried this ghetto solution, since I only needed the view for one handler:</p>
<p><script src="http://gist.github.com/337787.js?file=GhettoBaseMediator.as"></script></p>
<p>But as time went on, I added more handlers to the base mediator. The more handlers I added, the uglier the code became, with the AS2-reminiscent <em>this['view']</em> in each function. I remembered reading something about injection types other than property-based, so I ventured out and discovered setter injection. With it, I can inject the subclass view into the subclass mediator, but still access the base view from the base mediator—classy! Here&#8217;s the implementation:</p>
<p><script src="http://gist.github.com/337787.js?file=ClassyBaseMediator.as"></script></p>
<p><script src="http://gist.github.com/337787.js?file=ClassyExtendingMediator.as"></script></p>
<p>Though this works perfectly fine when extending a base mediator once, I already came across an issue—what do you do when your base mediator is extended twice? I suppose you could use a private getter in the in-between subclass, but if anyone has suggestions, feel free to drop them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/03/using-setter-injection-to-provide-a-view-for-base-mediators-in-robotlegs/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Encoding for OAuth using AS3</title>
		<link>http://destroytoday.com/blog/2010/02/encoding-for-oauth-using-as3/</link>
		<comments>http://destroytoday.com/blog/2010/02/encoding-for-oauth-using-as3/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 02:00:08 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1754</guid>
		<description><![CDATA[AS3 has a few useful URL-related methods, like escape/unescape and encodeURI/decodeURI, but it doesn&#8217;t have a plain-and-simple encodeUTF8 function. The encodeURIComponent method encodes a string to UTF-8, but URL-encodes it as well, requiring the unescape method to wrap the call. Many developers have resorted to writing their own encodeUTF8 method, looping through each character and [...]]]></description>
			<content:encoded><![CDATA[<p>AS3 has a few useful URL-related methods, like <em>escape</em>/<em>unescape</em> and <em>encodeURI</em>/<em>decodeURI</em>, but it doesn&#8217;t have a plain-and-simple <em>encodeUTF8</em> function. The <em>encodeURIComponent</em> method encodes a string to UTF-8, but URL-encodes it as well, requiring the <em>unescape</em> method to wrap the call. Many developers have resorted to writing their own <em>encodeUTF8</em> method, looping through each character and converting with bitwise. I&#8217;ve found this to be unreliable at times after pinpointing an issue with the OAuth lib I use(d) with <a href="http://github.com/destroytoday/TwitterAspirin" target="_blank">TwitterAspirin</a>. I came across a number of problems, specifically dealing with <em>POST</em> methods and incorrect signatures, so I decided to take a night to write my own OAuth request signer.</p>
<p>In a search to better understand the <a href="http://oauth.net/core/1.0/" target="_blank">OAuth spec</a>, I came across <a href="http://hueniverse.com/2008/10/beginners-guide-to-oauth-part-iv-signing-requests/" target="_blank">this terrific tutorial</a> on OAuth by <a href="http://hueniverse.com/author/eran/" target="_blank">Eran Hammer-Lahav</a> on <a href="http://hueniverse.com/" target="_blank">Hueniverse</a>. It is hands-down the most useful tutorial I have found on the subject, translating the OAuth spec for the &#8220;English-speaking&#8221; and providing a foolproof explanation for each step. The tutorial also provides input fields for each step, so you can manually input your keys, secrets, and tokens, and see if your result matches up.</p>
<p>The main reason this tutorial is so helpful is its emphasis on the UTF-8 encoding. Sure, the OAuth spec indicates the unreserved characters, but this tutorial stresses that most languages do not share the same unreserved characters—this is true for AS3. The following characters are listed as unreserved in the OAuth Core 1.0 spec:</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
<span style="">0</span> <span style="">1</span> <span style="">2</span> <span style="">3</span> <span style="">4</span> <span style="">5</span> <span style="">6</span> <span style="">7</span> <span style="">8</span> <span style="">9</span> - . _ ~</pre></div></div>

<p>Compare this with the unreserved characters of AS3&#8242;s escape method:</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
<span style="">0</span> <span style="">1</span> <span style="">2</span> <span style="">3</span> <span style="">4</span> <span style="">5</span> <span style="">6</span> <span style="">7</span> <span style="">8</span> <span style="">9</span> @ - _ . * + /</pre></div></div>

<p>And again with the unreserved characters of AS3&#8242;s encodeURIComponent method:</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
<span style="">0</span> <span style="">1</span> <span style="">2</span> <span style="">3</span> <span style="">4</span> <span style="">5</span> <span style="">6</span> <span style="">7</span> <span style="">8</span> <span style="">9</span> - _ . ! ~ * ' <span class="br0">&#40;</span> <span class="br0">&#41;</span></pre></div></div>

<p>As you can see, a solid amount of work is needed to match the unreserved characters listed in the OAuth spec. At the moment, this is the gargantuan series of method calls I use to properly encode the parameters used in an OAuth-related request:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> encode<span style="color: #000000;">&#40;</span>str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">escape</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">unescape</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">encodeURIComponent</span><span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/%</span>7E<span style="color: #000000; font-weight: bold;">/</span>g, <span style="color: #990000;">'~'</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span>@<span style="color: #000000; font-weight: bold;">/</span>g, <span style="color: #990000;">'%40'</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span>\<span style="color: #000000; font-weight: bold;">*/</span>g, <span style="color: #990000;">'%2A'</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span>\<span style="color: #000000; font-weight: bold;">+/</span>g, <span style="color: #990000;">'%2B'</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span>\<span style="color: #009900;">//g, '%2F');</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I know what you&#8217;re thinking—Jonnie has lost his marbles and the proof is that <em>unescape</em> method call wrapped in the <em>escape</em> method call. Believe me when I tell you it&#8217;s necessary. Why?—because of the additional unreserved characters in the <em>encodeURIComponent</em> method. The <em>unescape</em> method removes the URL encoding, so it&#8217;s just UTF-8 encoded at this point. Then, the <em>escape</em> method re-URL-encodes the string, but this time converts the additional unreserved characters as well. Now, I&#8217;m left with a half dozen replace method calls to convert the last few characters that aren&#8217;t in the OAuth spec. (If you know a way to combine all of these replace method calls into one, let me know! Regular expressions are slow in AS3)</p>
<p>I really hope this helps those venturing into the dark world of OAuth, and as always, my OAuth request signer can be found among the changes pushed to TwitterAspirin on GitHub. I also updated <a href="http://github.com/destroytoday/TwitterAspirinDemo" target="_blank">TwitterAspirinDemo</a> to show how to use the library. Feel free to watch or fork either repo—both will see a lot of activity over the next few weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/02/encoding-for-oauth-using-as3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>EventMap.mapListener bug (and fix!) in RobotLegs</title>
		<link>http://destroytoday.com/blog/2010/01/eventmap-maplistener-bug-and-fix-in-robotlegs/</link>
		<comments>http://destroytoday.com/blog/2010/01/eventmap-maplistener-bug-and-fix-in-robotlegs/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 02:38:31 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quick Fix]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1708</guid>
		<description><![CDATA[This past week, I experienced a bug with RobotLegs where the EventMap.mapListener method would add the listener multiple times if called multiple times. Yesterday, I had some free time to dig a little deeper and managed to pinpoint the problematic code. It turns out the method didn&#8217;t check to see if the listener existed prior [...]]]></description>
			<content:encoded><![CDATA[<p>This past week, I experienced a bug with <a href="http://robotlegs.org/" target="_blank">RobotLegs</a> where the <em>EventMap.mapListener</em> method would add the listener multiple times if called multiple times. Yesterday, I had some free time to dig a little deeper and managed to pinpoint the problematic code. It turns out the method didn&#8217;t check to see if the listener existed prior to adding to the array. This explains why a button click handler in my code would respond twice.</p>
<p>Thanks to the oh-so-wonderful <a href="http://github.com" target="_blank">GitHub</a>, I was able to fork the repository, implement the fix, write a unit test to verify it&#8217;s safe, and find in the morning that it has been integrated into the actual <a href="http://github.com/robotlegs/robotlegs-framework" target="_blank">RobotLegs repository</a>—just like that. If you experienced the bug, be sure to pull the latest commit.</p>
<p>Now, if you&#8217;re not on GitHub, do yourself a favor and join today. In the few months I&#8217;ve been a member, I&#8217;ve met a handful of incredible developers and now feel compelled to share code when I can. Everyone benefits when you share what you know.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/01/eventmap-maplistener-bug-and-fix-in-robotlegs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing TwitterAspirin: an AS3 Twitter API painkiller</title>
		<link>http://destroytoday.com/blog/2010/01/introducing-twitteraspirin-an-as3-twitter-api-painkiller/</link>
		<comments>http://destroytoday.com/blog/2010/01/introducing-twitteraspirin-an-as3-twitter-api-painkiller/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 15:01:58 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Announcement]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[DestroyTwitter]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1704</guid>
		<description><![CDATA[A couple months ago, I started working on a Twitter component for my current project at Adobe. I went into this knowing I&#8217;d have to finally face the beast&#8230; OAuth. Just about every well-known Twitter client out there uses Basic Auth—and for a reason. It&#8217;s easy, what the user expects, and gives your app more [...]]]></description>
			<content:encoded><![CDATA[<p>A couple months ago, I started working on a Twitter component for my current project at Adobe. I went into this knowing I&#8217;d have to finally face the beast&#8230; OAuth. Just about every well-known Twitter client out there uses Basic Auth—and for a reason. It&#8217;s easy, what the user expects, and gives your app more credibility—there&#8217;s no requirement to leave to authenticate through the browser like with OAuth.</p>
<p>About five or six months ago, Twitter decided to enforce the transition. From then on, any application that uses the API must use OAuth in order to see &#8220;via [your app]&#8221; on tweets published with it—otherwise, it would display &#8220;via API.&#8221; Since &#8220;via&#8221; is where apps get probably 90% of their referrals, this was a big deal. Luckily for me, <a href="http://destroytwitter.com" target="_blank">DestroyTwitter</a> existed before that time and Twitter decided not to push the change on the veteran apps. Recently, however, the bad news spread that Basic Auth would be deprecated in June. This means every Twitter app must transition to the pain that is OAuth.</p>
<p>After developing the <a href="http://destroytoday.com/blog/2009/10/adobe-max-companion-launched/" target="_blank">MAX Companion</a> this past fall and now the more generalized version, I found myself rewriting the Twitter component each time. After a while, the Twitter API code I wrote for DestroyTwitter began to merge with the actual implementation, so it was no longer a standalone library that could easily be used by other projects. These past few months, I&#8217;ve been learning a great deal about framework architecture and design patterns. It has led me to realize I need to start fresh.</p>
<p>With all that being said, I&#8217;d like introduce a library I started working on two days ago. I call it <a href="http://github.com/destroytoday/TwitterAspirin" target="_blank">TwitterAspirin</a>. It&#8217;s an AS3 Twitter API library that eases the pain, providing developers with a very powerful tool for communicating with Twitter. Though it&#8217;s still a newborn at the moment, I see potential already. The library is built on <a href="http://robotlegs.org/" target="_blank">RobotLegs</a> and uses <a href="http://github.com/robertpenner/as3-signals" target="_blank">AS3 Signals</a> instead of events. It&#8217;s hosted on <a href="http://github.com" target="_blank">GitHub</a>, so the source code will always be available to the public. And, after last night&#8217;s commit, its OAuth functionality is complete. Here&#8217;s how to use it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com.destroytoday.twitteraspirin.Twitter;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Test extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">// set application consumer key and secret</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> twitter<span style="color: #000000; font-weight: bold;">:</span>Twitter = <span style="color: #0033ff; font-weight: bold;">new</span> Twitter<span style="color: #000000;">&#40;</span>consumerKey, consumerSecret<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Test<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// add signal listeners</span>
			twitter.oauth.requestTokenSignal.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>requestTokenHandler<span style="color: #000000;">&#41;</span>;
			twitter.oauth.accessTokenSignal.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>accessTokenHandler<span style="color: #000000;">&#41;</span>;
			twitter.oauth.verifyAccessTokenSignal.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>verifyAccessTokenHandler<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">// click the 'Authorize' button to get the request token</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> authorizeClickHandler<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			twitter.oauth.getRequestToken<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">// upon receiving the request token, open Twitter in the browser to authorize</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> requestTokenHandler<span style="color: #000000;">&#40;</span>oauth<span style="color: #000000; font-weight: bold;">:</span>OAuth, token<span style="color: #000000; font-weight: bold;">:</span>OAuthToken<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">navigateToURL</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span>oauth.getAuthorizeURL<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">// return with the provided pin and click the 'Activate' button to get the access token</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> activateClickHandler<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			twitter.oauth.getAccessToken<span style="color: #000000;">&#40;</span>pin<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">// upon receiving the access token, verify it</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> accessTokenHandler<span style="color: #000000;">&#40;</span>oauth<span style="color: #000000; font-weight: bold;">:</span>OAuth, token<span style="color: #000000; font-weight: bold;">:</span>OAuthToken<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			oauth.verifyAccessToken<span style="color: #000000;">&#40;</span>token<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">// done</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> verifyAccessTokenHandler<span style="color: #000000;">&#40;</span>oauth<span style="color: #000000; font-weight: bold;">:</span>OAuth, token<span style="color: #000000; font-weight: bold;">:</span>OAuthToken<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, it&#8217;s extremely easy to use. Not only that, it provides great flexibility. Many libraries are simple to implement, but don&#8217;t allow the developer access to certain aspects of the process. With TwitterAspirin, each method returns the loader involved with the operation, giving developers the ability to listen for errors, cancel the operation, or attain the raw API data. The library also uses loader pools to recycle instances, so you can submit a tweet and, while it&#8217;s loading, submit another—you don&#8217;t have to wait for the first operation to finish. Then, once the operation is complete, the loader is disposed and returned to the pool, which optimizes performance and memory usage.</p>
<p>I&#8217;m really excited to see where TwitterAspirin goes and I have nothing but great expectations. Be sure to follow along with development and fork whenever you like.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/01/introducing-twitteraspirin-an-as3-twitter-api-painkiller/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Introducing the XMLLoader class</title>
		<link>http://destroytoday.com/blog/2010/01/introducing-the-xmlloader-class/</link>
		<comments>http://destroytoday.com/blog/2010/01/introducing-the-xmlloader-class/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 14:43:45 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Announcement]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1700</guid>
		<description><![CDATA[I started the XMLLoader class a few months back, but didn&#8217;t post right away because it needed a lot of cleanup. Not cleanup in the sense of performance improvement or refactoring—it simply used my old programming style, full of dollar signs and underscores. If anyone remembers seeing my code in that form, I deeply apologize [...]]]></description>
			<content:encoded><![CDATA[<p>I started the <a href="http://github.com/destroytoday/DestroyFramework/blob/master/src/com/destroytoday/net/XMLLoader.as" target="_blank">XMLLoader</a> class a few months back, but didn&#8217;t post right away because it needed a lot of cleanup. Not cleanup in the sense of performance improvement or refactoring—it simply used my old programming style, full of dollar signs and underscores. If anyone remembers seeing my code in that form, I deeply apologize for the pain it must have caused your eyes.</p>
<p>So why an <em>XMLLoader</em> class?—because 99% of the data I load with AS3/AIR is in XML format. I stay far from JSON when possible because in AS3 it&#8217;s slower than a slug on its day off. XML is native and uses E4X, which lets developers navigate its information just like you would an AS3 tree. I load XML so often, I found myself copying and pasting the same code each time I&#8217;d need it—this is the clearest indicator that a class must be written.</p>
<p>Parsing String data to XML poses an issue that many don&#8217;t know about. I only discovered it because of the Twitter API. The API is so janked, it sometimes returns the HTML error page instead of the XML response. It wouldn&#8217;t be so bad if it weren&#8217;t for an image tag in the HTML that isn&#8217;t closed. This immediately throws an &#8220;XML parser failure: element is malformed&#8221; error. Using try/catch is the only way to avoid this. That&#8217;s why I wrote it into <em>XMLLoader</em>. It automatically handles the data, attempts to parse it, and, if there are any errors, the load stops and dispatches an error signal.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com.destroytoday.net.XMLLoader;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Test extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span>XMLLoader;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Test<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">loader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> XMLLoader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">loader</span>.retryCount = <span style="color: #000000; font-weight:bold;">2</span>;
			<span style="color: #004993;">loader</span>.includeResponseInfo = <span style="color: #0033ff; font-weight: bold;">true</span>;
&nbsp;
			<span style="color: #004993;">loader</span>.openSignal.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>loaderOpenHandler<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">loader</span>.completeSignal.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>loaderCompleteHandler<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">loader</span>.errorSignal.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>loaderErrorHandler<span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">loader</span>.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://search.twitter.com/search.atom?q=destroytoday&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> loaderOpenHandler<span style="color: #000000;">&#40;</span><span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span>XMLLoader<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loader</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> loaderCompleteHandler<span style="color: #000000;">&#40;</span><span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span>XMLLoader, <span style="color: #004993;">data</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loader</span>, <span style="color: #004993;">data</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> loaderErrorHandler<span style="color: #000000;">&#40;</span><span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span>XMLLoader, <span style="color: #004993;">error</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, <span style="color: #004993;">message</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loader</span>, <span style="color: #004993;">error</span>, <span style="color: #004993;">message</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loader</span>.responseStatus<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Speaking of signals, <em>XMLLoader</em> is the first class in <a href="http://github.com/destroytoday/DestroyFramework" target="_blank">DestroyFramework</a> to use <a href="http://robertpenner.com/flashblog/" target="_blank">Robert Penner&#8217;s</a> <a href="http://github.com/robertpenner/as3-signals" target="_blank">AS3 Signals</a> instead of events. If you have to ask why, you haven&#8217;t been programming in AS3 long enough. Each <em>XMLLoader</em> instance has signals for <em>open</em>, <em>complete</em>, <em>error</em>, and <em>cancel</em>. The class also includes a <em>retryCount</em> property that indicates how many times to attempt to load a URL before calling it quits. Since some APIs can be down one second, then up the next, this feature really comes in handy. It&#8217;s mainly intended for handling the beloved fail whale.</p>
<p>The last two features include a <em>cancel</em> method and an <em>includeResponseInfo</em> property. Sure, <em>URLLoader</em> has a cancel method, <em>close</em>, but an exception is thrown if you call <em>close</em> when no operation is in progress. This makes sense, but the runtime shouldn&#8217;t close down shop when it happens. <em>XMLLoader</em> instead cancels and dispatches the <em>cancel</em> signal when the cancel method is called, and if no operations are in progress, it simply does nothing.</p>
<p>The <em>includeResponseInfo</em> property is an incredibly easy way to tell the loader to return the response status code and headers upon success or fail. Without <em>XMLLoader</em>, you could get this information with an event listener and the appropriate handler, but it&#8217;s far easier to flick a switch. In all honesty, I&#8217;ve neglected to retrieve this info using <em>URLLoader</em> in the past simply because it&#8217;s such a verbose process. Now that it only requires setting the property to <em>true</em>, I know I&#8217;ll use it more often than not.</p>
<p>As always, the <a href="http://github.com/destroytoday/DestroyFramework/blob/master/src/com/destroytoday/net/XMLLoader.as" target="_blank">source code</a> is available on <a href="http://github.com" target="_blank">GitHub</a>. Be sure to keep watch—my account will be pretty active these next few weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2010/01/introducing-the-xmlloader-class/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Introducing the AsyncLoop class</title>
		<link>http://destroytoday.com/blog/2009/12/introducing-the-asyncloop-class/</link>
		<comments>http://destroytoday.com/blog/2009/12/introducing-the-asyncloop-class/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 06:17:02 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Announcement]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1645</guid>
		<description><![CDATA[Out of my recent work, I&#8217;m especially proud of and excited about the AsyncLoop class. It&#8217;s a serious performance enhancer that takes heavy processes and spreads them out over time, preventing stalls and possible lockups. I originally wrote it to deal with a for loop that contained a complex process and iterated 1000 times. Needless [...]]]></description>
			<content:encoded><![CDATA[<p>Out of my recent work, I&#8217;m especially proud of and excited about the <a href="http://github.com/destroytoday/DestroyFramework/blob/master/src/com/destroytoday/timer/AsyncLoop.as" target="_blank">AsyncLoop</a> class. It&#8217;s a serious performance enhancer that takes heavy processes and spreads them out over time, preventing stalls and possible lockups. I originally wrote it to deal with a <code>for</code> loop that contained a complex process and iterated 1000 times. Needless to say, the beach ball made an extended stay each time the loop ran. After implementing <code>AsyncLoop</code>, the beach ball disappeared and animations played smooth throughout.</p>
<p><code>AsyncLoop</code> works around a timer limit. The developer sets the maximum number of milliseconds to loop through the process function. Once the timer exceeds that limit, it carries over to the next frame and repeats. The loop can be ended a number of ways, providing great flexibility. A limit can be placed on the loop count, mimicking common <code>for</code> loop usage. The loop can return the <code>AsyncLoopAction.BREAK</code> constant. When using that method, <code>AsyncLoopAction.CONTINUE</code> is used to tell the loop to continue to the next tick. Each loop provides analytics, keeping track of its duration, loop count, and frame count. Here are two ways you can use it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">// new AsyncLoop(callback:Function, countLimit:int = -1, timerLimit:int = 20);</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loop</span><span style="color: #000000; font-weight: bold;">:</span>AsyncLoop = <span style="color: #0033ff; font-weight: bold;">new</span> AsyncLoop<span style="color: #000000;">&#40;</span>tick, <span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">loop</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">OPEN</span>, loopOpenHandler<span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">loop</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">CHANGE</span>, loopChangeHandler<span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">loop</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, loopCompleteHandler<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">loop</span>.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> tick<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// heavy process</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> loopOpenHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// loop started</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #339966; font-weight: bold;">function</span> loopChangeHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// loop carries over to next frame</span>
&nbsp;
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loop</span>.duration<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// incomplete loop running time</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loop</span>.<span style="color: #004993;">currentCount</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// number of callback calls</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #339966; font-weight: bold;">function</span> loopCompleteHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// loop complete</span>
&nbsp;
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loop</span>.duration<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// completed loop duration</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">loop</span>.frameCount<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// frames needed to complete</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loop</span><span style="color: #000000; font-weight: bold;">:</span>AsyncLoop = <span style="color: #0033ff; font-weight: bold;">new</span> AsyncLoop<span style="color: #000000;">&#40;</span>tick<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">loop</span>.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, clickHandler<span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">DOUBLE_CLICK</span>, doubleClickHandler<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> tick<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// heavy process</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>loopShouldEnd<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> AsyncLoopAction.BREAK;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">return</span> AsyncLoopAction.CONTINUE;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">loop</span>.<span style="color: #004993;">running</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">loop</span>.<span style="color: #004993;">pause</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">loop</span>.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #339966; font-weight: bold;">function</span> doubleClickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">loop</span>.<span style="color: #004993;">cancel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m really happy with the class and how far it has come since its original form as <code>fLoop</code>. As always, the <a href="http://github.com/destroytoday/DestroyFramework/blob/master/src/com/destroytoday/timer/AsyncLoop.as" target="_blank">source code</a> is on GitHub, so take it and run! If you find it useful, feel free to share how you used it, or simply let me know what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2009/12/introducing-the-asyncloop-class/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Introducing the ApplicationUtil class</title>
		<link>http://destroytoday.com/blog/2009/12/introducing-the-applicationutil-class/</link>
		<comments>http://destroytoday.com/blog/2009/12/introducing-the-applicationutil-class/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 14:23:05 +0000</pubDate>
		<dc:creator>jonhallman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Announcement]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://destroytoday.com/blog/?p=1639</guid>
		<description><![CDATA[Yesterday, during my weekly football &#8220;service&#8221;, I spent a few minutes starting the ApplicationUtil class. So far, it consists of only two methods, getVersion and closeOpenWindows. The first accesses the application descriptor and returns the application&#8217;s version. closeOpenWindows is a necessity I learned in the Apollo days from Christian Cantrell. I had issues with my [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, during my weekly football &#8220;service&#8221;, I spent a few minutes starting the <a href="http://github.com/destroytoday/DestroyFramework/blob/master/src/com/destroytoday/util/ApplicationUtil.as" target="_blank">ApplicationUtil</a> class. So far, it consists of only two methods, <code>getVersion</code> and <code>closeOpenWindows</code>. The first accesses the application descriptor and returns the application&#8217;s version. <code>closeOpenWindows</code> is a necessity I learned in the Apollo days from <a href="http://blogs.adobe.com/cantrell/" target="_blank">Christian Cantrell</a>. I had issues with my first AIR app, <a href="http://destroytoday.com/releases/DestroyFlickr108B.zip" target="_blank">DestroyFlickr</a>, where it wouldn&#8217;t quit, even if all of the visible windows were closed. I commonly use window visibility to show/hide utility windows, so the invisible ones were hanging around, keeping the app open. This method runs a quick loop to close all open windows, visible or not. It also includes an <code>andQuit</code> argument that, when true, sets <code>autoExit</code> to true, which quits the app upon close of the windows. Here&#8217;s some unnecessary example code to give this post more character:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">version</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = ApplicationUtil.getVersion<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">version</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// v1</span>
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>NativeApplication.nativeApplication.openedWindows.<span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 3</span>
ApplicationUtil.closeOpenWindows<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>NativeApplication.nativeApplication.openedWindows.<span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 0</span>
&nbsp;
<span style="color: #009900;">// suppose three windows are open again</span>
ApplicationUtil.closeOpenWindows<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// closes windows and quits before trace is called</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>NativeApplication.nativeApplication.openedWindows.<span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://destroytoday.com/blog/2009/12/introducing-the-applicationutil-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
