Queue.js.html revision eb9b6f817df5c660be9f00fc5d27fe33479fdc98
<html>
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>Queue <span class="subtitle">3.00PR1</span></h3>
<p>
> 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"> * <ul></span>
<span class="c"> * <li><code>fn</code> - {Function} REQUIRED the callback function.</li></span>
<span class="c"> * <li><code>timeout</code> - {number} millisecond delay to wait after previous callback completion before executing this callback. Negative values cause immediate blocking execution. Default 0.</li></span>
<span class="c"> * <li><code>until</code> - {Function} boolean function executed before each iteration. Return true to indicate callback completion.</li></span>
<span class="c"> * <li><code>iterations</code> - {Number} number of times to execute the callback before proceeding to the next callback in the chain. Incompatible with <code>until</code>.</li></span>
<span class="c"> * </ul></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"> * @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">'complete'</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">'function'</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"><</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">></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"> * <dl></span>
<span class="c"> * <dt>fn</dt><dd>The function about to be executed</dd></span>
<span class="c"> * <dt>callback</dt><dd>The callback as provided to <code>add(..)</code></dd></span>
<span class="c"> * </dl></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">'beforeCallback'</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"> * <dl></span>
<span class="c"> * <dt>fn</dt><dd>The function just executed</dd></span>
<span class="c"> * <dt>callback</dt><dd>The callback as provided to <code>add(..)</code></dd></span>
<span class="c"> * </dl></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">'afterCallback'</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 <code>add(..)</code></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">'shiftCallback'</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 <code>add(..)</code> after callbacks are queued</span>
<span class="c"> * @event addCallback</span>
<span class="c"> * @param callbacks {Array} Array of callbacks passed to <code>add(..)</code></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">'addCallback'</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"> * @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">'pause'</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'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">'stop'</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">
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright © 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>