<?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>The efnx code blog. &#187; todo</title>
	<atom:link href="http://efnx.com/tag/todo/feed/" rel="self" type="application/rss+xml" />
	<link>http://efnx.com</link>
	<description>code. blog.</description>
	<lastBuildDate>Wed, 18 Jan 2012 20:48:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>AS3 doLater/todo Function Queue</title>
		<link>http://efnx.com/as3-dolatertodo-function-queue/</link>
		<comments>http://efnx.com/as3-dolatertodo-function-queue/#comments</comments>
		<pubDate>Sat, 08 Dec 2007 08:31:59 +0000</pubDate>
		<dc:creator>Schell</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[doLater]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[queue]]></category>
		<category><![CDATA[todo]]></category>

		<guid isPermaLink="false">http://blog.efnx.com/?p=37</guid>
		<description><![CDATA[While working on my windowing system I ran into a problem. Every time I tried to set the width of my window class instance I&#8217;d get an error from my bitmap resource handler telling me the resources hadn&#8217;t been loaded. This makes it impossible to set widths for a window before it&#8217;s loaded, and since [...]]]></description>
			<content:encoded><![CDATA[<p>While working on my windowing system I ran into a problem. Every time I tried to set the width of my window class instance I&#8217;d get an error from my bitmap resource handler telling me the resources hadn&#8217;t been loaded. This makes it impossible to set widths for a window before it&#8217;s loaded, and since the entire system is supposed to be deployable online, loading time is not anything I can count on. I wrote this small class to take care of executing functions at a given time (like after loading is finished). The class is called Queue and here is some usage:</p>
<div class="codecolorer-container actionscript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">import</span> com.<span style="color: #006600;">efnx</span>.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Queue</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> blah<span style="color: #66cc66;">&#40;</span>val1:<span style="color: #0066CC;">int</span>, val2:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>val1+val2<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> blah2<span style="color: #66cc66;">&#40;</span>val1:<span style="color: #0066CC;">int</span>, val2:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>val1+val2<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> queue:Queue = <span style="color: #000000; font-weight: bold;">new</span> Queue<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; queue.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>blah, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; queue.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>blah, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; queue.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>blah, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; queue.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>blah2, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>queue<span style="color: #66cc66;">&#41;</span>;<br />
<br />
queue.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>That code does this -> First we define two different functions, blah and blah2. Then we push the functions to be executed into the queue. Here&#8217;s the interesting part. <s>Since the Dictionary Object inside the class used to list functions is not weakly referenced</s> Duplicate entries cannot be made by the same function regardless of it&#8217;s parameters. In the the example above I push blah into the queue three times, but the only one that fires and traces is the third because the queue&#8217;s key is the function itself [or the "closure object (ie. the "behind the scenes" object that facilitates method closure by maintaining a reference back to the method and its scope)" as described by <a href="http://gskinner.com/blog/archives/2006/07/as3_dictionary.html">Grant Skinner</a>], so the function replaces itself in the queue. This is nice for doing things like setting widths and resizing, although not so great for things like sending different strings to some handler. As of now calling start() applies the functions asynchronously and nullifies anything left in the queue. You&#8217;ll see the output traces this:</p>
<div class="codecolorer-container actionscript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">object</span> <span style="color: #66cc66;">&#91;</span>Queue<span style="color: #66cc66;">&#93;</span><br />
-<span style="color: #66cc66;">&gt;</span>&nbsp; <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span> &nbsp;<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">5</span><br />
-<span style="color: #66cc66;">&gt;</span>&nbsp; <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span> &nbsp;<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><br />
<span style="color: #cc66cc;">8</span><br />
<span style="color: #cc66cc;">9</span></div></div>
<p>First it traces the Queue Object which contains references to the two functions and their parameters (not in the order we gave them) then the output. Bam! Done. Oops! Not quite&#8230; &#8230;At any point you can flush the queue without executing the functions by calling abort(). There, done.</p>
<p>This class can be easily extended or modified to include directives to execute functions in a given order or multiple entries of one function, so get crackin!</p>
<p><b>Source</b><br />
<a href='http://blog.efnx.com/wp-content/uploads/2007/12/blogefnxcom-queue.zip' title='Queue Class Source Code'>Queue Class Source Code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://efnx.com/as3-dolatertodo-function-queue/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

