Queue.js.html revision eb9b6f817df5c660be9f00fc5d27fe33479fdc98
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>API: Queue Queue.js (YUI Library)</title>
<link rel="stylesheet" type="text/css" href="assets/api.css">
<script type="text/javascript" src="assets/api-js"></script>
<script type="text/javascript" src="assets/ac-js"></script>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<a href="http://developer.yahoo.com/yui/"><h1>Yahoo! UI Library</h1></a>
<h3>Queue&nbsp; <span class="subtitle">3.00PR1</span></h3>
<p>
<a href="/index.html">Yahoo! UI Library</a>
&gt; <a href="/module_queue.html">Queue</a>
&gt; Queue.js (source view)
</p>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<form name="yui-classopts-form">
<span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> Show Private</span>
<span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> Show Protected</span>
<span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> Show Deprecated</span>
</form>
<div id="srcout">
<style>
#doc3 #classopts { display:none; }
</style>
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * Mechanism to execute a series of callbacks in a non-blocking queue. Each</span>
<span class="c"> * callback is executed via setTimout unless configured with a negative</span>
<span class="c"> * timeout, in which case it is run in blocking mode in the same execution</span>
<span class="c"> * thread as the previous callback. Callbacks can be function references or</span>
<span class="c"> * object literals with the following keys:</span>
<span class="c"> * &lt;ul&gt;</span>
<span class="c"> * &lt;li&gt;&lt;code&gt;fn&lt;/code&gt; - {Function} REQUIRED the callback function.&lt;/li&gt;</span>
<span class="c"> * &lt;li&gt;&lt;code&gt;timeout&lt;/code&gt; - {number} millisecond delay to wait after previous callback completion before executing this callback. Negative values cause immediate blocking execution. Default 0.&lt;/li&gt;</span>
<span class="c"> * &lt;li&gt;&lt;code&gt;until&lt;/code&gt; - {Function} boolean function executed before each iteration. Return true to indicate callback completion.&lt;/li&gt;</span>
<span class="c"> * &lt;li&gt;&lt;code&gt;iterations&lt;/code&gt; - {Number} number of times to execute the callback before proceeding to the next callback in the chain. Incompatible with &lt;code&gt;until&lt;/code&gt;.&lt;/li&gt;</span>
<span class="c"> * &lt;/ul&gt;</span>
<span class="c"> *</span>
<span class="c"> * @module Queue</span>
<span class="c"> * @class Queue</span>
<span class="c"> * @constructor</span>
<span class="c"> * @param callback* {Function|Object} Any number of callbacks to initialize the queue</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Queue</span> <span class="o">=</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="c">// Factory or Constructor</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">me</span> <span class="o">=</span> <span class="k">this</span> <span class="k">instanceof</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Queue</span> <span class="o">?</span> <span class="k">this</span> <span class="o">:</span> <span class="k">new</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Queue</span><span class="o">();</span>
<span class="c">/**</span>
<span class="c"> * The callback queue</span>
<span class="c"> * @property q</span>
<span class="c"> * @type {Array}</span>
<span class="c"> * @protected</span>
<span class="c"> */</span>
<span class="nx">me</span><span class="o">.</span><span class="nx">q</span> <span class="o">=</span> <span class="o">[];</span>
<span class="k">return</span> <span class="nx">me</span><span class="o">.</span><span class="nx">add</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">me</span><span class="o">,</span><span class="nx">arguments</span><span class="o">);</span>
<span class="o">};</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Queue</span><span class="o">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * Timeout id used to pause or stop execution and indicate the execution</span>
<span class="c"> * state of the Queue. 0 indicates paused or stopped, negatives indicate</span>
<span class="c"> * blocking execution, and positives indicate non-blocking execution.</span>
<span class="c"> * @property id</span>
<span class="c"> * @type {number}</span>
<span class="c"> * @protected</span>
<span class="c"> */</span>
<span class="nx">id</span> <span class="o">:</span> <span class="m">0</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * Execute the queue callbacks (also resumes paused Queue).</span>
<span class="c"> * @method run</span>
<span class="c"> * @return {Queue} the Queue instance</span>
<span class="c"> */</span>
<span class="nx">run</span> <span class="o">:</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="c">// Grab the first callback in the queue</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">c</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">q</span><span class="o">[</span><span class="m">0</span><span class="o">],</span>
<span class="nx">fn</span><span class="o">;</span>
<span class="c">// If there is no callback in the queue or the Queue is currently</span>
<span class="c"></span> <span class="c">// in an execution mode, return</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(!</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * Event fired when the callback queue is emptied via execution</span>
<span class="c"> * (not via a call to chain.stop()).</span>
<span class="c"> * @event complete</span>
<span class="c"> */</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">&#39;complete&#39;</span><span class="o">);</span>
<span class="k">return</span> <span class="k">this</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">id</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">this</span><span class="o">;</span>
<span class="o">}</span>
<span class="nx">fn</span> <span class="o">=</span> <span class="nx">c</span><span class="o">.</span><span class="nx">fn</span> <span class="o">||</span> <span class="nx">c</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">fn</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">ms</span> <span class="o">=</span> <span class="nx">c</span><span class="o">.</span><span class="nx">timeout</span> <span class="o">||</span> <span class="m">0</span><span class="o">,</span>
<span class="nx">me</span> <span class="o">=</span> <span class="k">this</span><span class="o">;</span>
<span class="c">// Execute immediately if the callback timeout is negative.</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">ms</span> <span class="o">&lt;</span> <span class="m">0</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">id</span> <span class="o">=</span> <span class="nx">ms</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">c</span><span class="o">.</span><span class="nx">until</span><span class="o">)</span> <span class="o">{</span> <span class="c">// test .until condition</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(;!</span><span class="nx">c</span><span class="o">.</span><span class="nx">until</span><span class="o">();)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">_exec</span><span class="o">(</span><span class="nx">fn</span><span class="o">,</span><span class="nx">c</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">c</span><span class="o">.</span><span class="nx">iterations</span><span class="o">)</span> <span class="o">{</span> <span class="c">// test .iterations</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(;</span><span class="nx">c</span><span class="o">.</span><span class="nx">iterations</span><span class="o">--</span> <span class="o">&gt;</span> <span class="m">0</span><span class="o">;)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">_exec</span><span class="o">(</span><span class="nx">fn</span><span class="o">,</span><span class="nx">c</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span> <span class="c">// single shot callback</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">_exec</span><span class="o">(</span><span class="nx">fn</span><span class="o">,</span><span class="nx">c</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">this</span><span class="o">.</span><span class="nx">_shift</span><span class="o">();</span>
<span class="k">this</span><span class="o">.</span><span class="nx">id</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">run</span><span class="o">();</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">c</span><span class="o">.</span><span class="nx">until</span><span class="o">)</span> <span class="o">{</span> <span class="c">// test .until condition</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">c</span><span class="o">.</span><span class="nx">until</span><span class="o">())</span> <span class="o">{</span>
<span class="c">// Move to the next callback</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">_shift</span><span class="o">();</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">run</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(!</span><span class="nx">c</span><span class="o">.</span><span class="nx">iterations</span> <span class="o">||</span> <span class="o">!--</span><span class="nx">c</span><span class="o">.</span><span class="nx">iterations</span><span class="o">)</span> <span class="o">{</span> <span class="c">// .iterations</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">_shift</span><span class="o">();</span>
<span class="o">}</span>
<span class="c">// Set to execute after the configured timeout</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">id</span> <span class="o">=</span> <span class="nx">setTimeout</span><span class="o">(</span><span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="nx">me</span><span class="o">.</span><span class="nx">_exec</span><span class="o">(</span><span class="nx">fn</span><span class="o">,</span><span class="nx">c</span><span class="o">);</span>
<span class="c">// Loop unless the Queue was paused from inside the callback</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">me</span><span class="o">.</span><span class="nx">id</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// Indicate ready to run state</span>
<span class="c"></span> <span class="nx">me</span><span class="o">.</span><span class="nx">id</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
<span class="c">// Start the fun all over again</span>
<span class="c"></span> <span class="nx">me</span><span class="o">.</span><span class="nx">run</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">},</span><span class="nx">ms</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="k">this</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Executes the callback function</span>
<span class="c"> * @method _exec</span>
<span class="c"> * @param fn {Function} the function to execute</span>
<span class="c"> * @param c {Object|Function} the callback as defined during add(c)</span>
<span class="c"> * @protected</span>
<span class="c"> */</span>
<span class="nx">_exec</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">fn</span><span class="o">,</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * Fired before a callback is executed</span>
<span class="c"> * @event beforeCallback</span>
<span class="c"> * @param o {Object} Object literal with the following keys:</span>
<span class="c"> * &lt;dl&gt;</span>
<span class="c"> * &lt;dt&gt;fn&lt;/dt&gt;&lt;dd&gt;The function about to be executed&lt;/dd&gt;</span>
<span class="c"> * &lt;dt&gt;callback&lt;/dt&gt;&lt;dd&gt;The callback as provided to &lt;code&gt;add(..)&lt;/code&gt;&lt;/dd&gt;</span>
<span class="c"> * &lt;/dl&gt;</span>
<span class="c"> */</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">&#39;beforeCallback&#39;</span><span class="o">,{</span><span class="nx">fn</span><span class="o">:</span><span class="nx">fn</span><span class="o">,</span><span class="nx">callback</span><span class="o">:</span><span class="nx">c</span><span class="o">});</span>
<span class="nx">fn</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="k">this</span><span class="o">);</span>
<span class="c">/**</span>
<span class="c"> * Fired before a callback is executed</span>
<span class="c"> * @event afterCallback</span>
<span class="c"> * @param o {Object} Object literal with the following keys:</span>
<span class="c"> * &lt;dl&gt;</span>
<span class="c"> * &lt;dt&gt;fn&lt;/dt&gt;&lt;dd&gt;The function just executed&lt;/dd&gt;</span>
<span class="c"> * &lt;dt&gt;callback&lt;/dt&gt;&lt;dd&gt;The callback as provided to &lt;code&gt;add(..)&lt;/code&gt;&lt;/dd&gt;</span>
<span class="c"> * &lt;/dl&gt;</span>
<span class="c"> */</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">&#39;afterCallback&#39;</span><span class="o">,{</span><span class="nx">fn</span><span class="o">:</span><span class="nx">fn</span><span class="o">,</span><span class="nx">callback</span><span class="o">:</span><span class="nx">c</span><span class="o">});</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Shifts the first callback off the Queue</span>
<span class="c"> * @method _shift</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_shift</span> <span class="o">:</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * Fired after a callback is shifted from the Queue</span>
<span class="c"> * @event shiftCallback</span>
<span class="c"> * @param callback {Function|Object} The callback passed to &lt;code&gt;add(..)&lt;/code&gt;</span>
<span class="c"> */</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">&#39;shiftCallback&#39;</span><span class="o">,</span><span class="k">this</span><span class="o">.</span><span class="nx">q</span><span class="o">.</span><span class="nx">shift</span><span class="o">());</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Add any number of callbacks to the end of the queue</span>
<span class="c"> * @method add</span>
<span class="c"> * @param c {Function|Object}* Any number of callbacks</span>
<span class="c"> * @return {Queue} the Queue instance</span>
<span class="c"> */</span>
<span class="nx">add</span> <span class="o">:</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">callbacks</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span><span class="m">0</span><span class="o">,</span><span class="kc">true</span><span class="o">);</span>
<span class="k">this</span><span class="o">.</span><span class="nx">q</span><span class="o">.</span><span class="nx">splice</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">q</span><span class="o">,[</span><span class="k">this</span><span class="o">.</span><span class="nx">q</span><span class="o">.</span><span class="nx">length</span><span class="o">,</span><span class="m">0</span><span class="o">].</span><span class="nx">concat</span><span class="o">(</span><span class="nx">callbacks</span><span class="o">));</span>
<span class="c">/**</span>
<span class="c"> * Fired from within &lt;code&gt;add(..)&lt;/code&gt; after callbacks are queued</span>
<span class="c"> * @event addCallback</span>
<span class="c"> * @param callbacks {Array} Array of callbacks passed to &lt;code&gt;add(..)&lt;/code&gt;</span>
<span class="c"> */</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">&#39;addCallback&#39;</span><span class="o">,</span><span class="nx">callbacks</span><span class="o">);</span>
<span class="k">return</span> <span class="k">this</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Pause the execution of the Queue after the current execution of the</span>
<span class="c"> * current callback completes. If called interstitially, clears the</span>
<span class="c"> * timeout for the pending callback. Paused Queue can be restarted with</span>
<span class="c"> * chain.run()</span>
<span class="c"> * @method pause</span>
<span class="c"> * @return {Queue} the Queue instance</span>
<span class="c"> */</span>
<span class="nx">pause</span><span class="o">:</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="nx">clearTimeout</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">id</span><span class="o">);</span>
<span class="k">this</span><span class="o">.</span><span class="nx">id</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
<span class="c">/**</span>
<span class="c"> * Fired after Queue is paused</span>
<span class="c"> * @event pause</span>
<span class="c"> */</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">&#39;pause&#39;</span><span class="o">);</span>
<span class="k">return</span> <span class="k">this</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Stop and clear the Queue&#39;s queue after the current execution of the</span>
<span class="c"> * current callback completes.</span>
<span class="c"> * @method stop</span>
<span class="c"> * @return {Queue} the Queue instance</span>
<span class="c"> */</span>
<span class="nx">stop</span> <span class="o">:</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">pause</span><span class="o">();</span>
<span class="k">this</span><span class="o">.</span><span class="nx">q</span> <span class="o">=</span> <span class="o">[];</span>
<span class="c">/**</span>
<span class="c"> * Fired after Queue is stopped</span>
<span class="c"> * @event stop</span>
<span class="c"> */</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">&#39;stop&#39;</span><span class="o">);</span>
<span class="k">return</span> <span class="k">this</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">augment</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">Queue</span><span class="o">,</span><span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">.</span><span class="nx">Target</span><span class="o">);</span>
</pre></div>
</div>
</div>
</div>
<div class="yui-b">
<div class="nav">
<div class="module">
<h4>Modules</h4>
<ul class="content">
<li class=""><a href="module_anim.html">anim</a></li>
<li class=""><a href="module_anim-color.html">anim-color</a></li>
<li class=""><a href="module_anim-curve.html">anim-curve</a></li>
<li class=""><a href="module_anim-easing.html">anim-easing</a></li>
<li class=""><a href="module_anim-scroll.html">anim-scroll</a></li>
<li class=""><a href="module_anim-xy.html">anim-xy</a></li>
<li class=""><a href="module_attribute.html">attribute</a></li>
<li class=""><a href="module_base.html">base</a></li>
<li class=""><a href="module_dd-plugin.html">dd-plugin</a></li>
<li class=""><a href="module_event.html">event</a></li>
<li class=""><a href="module_get.html">get</a></li>
<li class=""><a href="module_io.html">io</a></li>
<li class=""><a href="module_loader.html">loader</a></li>
<li class=""><a href="module_node.html">node</a></li>
<li class=""><a href="module_node-class.html">node-class</a></li>
<li class=""><a href="module_node-region.html">node-region</a></li>
<li class=""><a href="module_node-screen.html">node-screen</a></li>
<li class="selected"><a href="module_Queue.html">Queue</a></li>
<li class=""><a href="module_yui.html">yui</a></li>
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
<li class=""><a href="Queue.html">Queue</a></li>
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
<li class="selected"><a href="Queue.js.html">Queue.js</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright &copy; 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>