queue.js.html revision 88f849e5723c03ee1476b48a345c791034ab5681
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>queue <span class="subtitle">3.0.0pr2</span></h3>
> Queue.js (source view)
<form onsubmit="return false">
<div id="propertysearch">
Search: <input autocomplete="off" id="searchinput" />
<div id="searchresults">
</div>
</div>
</form>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<form action="#" name="yui-classopts-form" method="get" id="yui-classopts-form">
<fieldset>
<legend>Filters</legend>
<span class="classopts"><input type="checkbox" name="show_private" id="show_private" /> <label for="show_private">Show Private</label></span>
<span class="classopts"><input type="checkbox" name="show_protected" id="show_protected" /> <label for="show_protected">Show Protected</label></span>
<span class="classopts"><input type="checkbox" name="show_deprecated" id="show_deprecated" /> <label for="show_deprecated">Show Deprecated</label></span>
</fieldset>
</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 queue. 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 after the last queued callback is executed. Not</span>
<span class="c"> * @event end</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">'end'</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 callback* {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 execution of the current</span>
<span class="c"> * callback completes. If called from code outside of a queued callback,</span>
<span class="c"> * clears the timeout for the pending callback. Paused Queue can be</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 id="moduleList" class="module">
<h4>Modules</h4>
<ul class="content">
<li class=""><a href="module_classnamemanager.html" title="classnamemanager">classnamemanager</a></li>
<li class=""><a href="module_widget-position-ext.html" title="widget-position-ext">widget-position-ext</a></li>
</ul>
</div>
<div id="classList" class="module">
<h4>Classes</h4>
<ul class="content">
</ul>
</div>
<div id="fileList" class="module">
<h4>Files</h4>
<ul class="content">
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright © 2009 Yahoo! Inc. All rights reserved.
</div>
</div>
<script type="text/javascript">
ALL_YUI_PROPS = [{"access": "", "host": "Queue", "name": "add", "url": "Queue.html#method_add", "type": "method"}, {"access": "", "host": "Queue", "name": "add", "url": "Queue.html#method_add", "type": "method"}, {"access": "", "host": "Queue", "name": "addCallback", "url": "Queue.html#event_addCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "addCallback", "url": "Queue.html#event_addCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "afterCallback", "url": "Queue.html#event_afterCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "afterCallback", "url": "Queue.html#event_afterCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "beforeCallback", "url": "Queue.html#event_beforeCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "beforeCallback", "url": "Queue.html#event_beforeCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "end", "url": "Queue.html#event_end", "type": "event"}, {"access": "", "host": "Queue", "name": "end", "url": "Queue.html#event_end", "type": "event"}, {"access": "protected", "host": "Queue", "name": "_exec", "url": "Queue.html#method__exec", "type": "method"}, {"access": "protected", "host": "Queue", "name": "_exec", "url": "Queue.html#method__exec", "type": "method"}, {"access": "protected", "host": "Queue", "name": "id", "url": "Queue.html#property_id", "type": "property"}, {"access": "protected", "host": "Queue", "name": "id", "url": "Queue.html#property_id", "type": "property"}, {"access": "", "host": "Queue", "name": "pause", "url": "Queue.html#method_pause", "type": "method"}, {"access": "", "host": "Queue", "name": "pause", "url": "Queue.html#event_pause", "type": "event"}, {"access": "", "host": "Queue", "name": "pause", "url": "Queue.html#method_pause", "type": "method"}, {"access": "", "host": "Queue", "name": "pause", "url": "Queue.html#event_pause", "type": "event"}, {"access": "protected", "host": "Queue", "name": "q", "url": "Queue.html#property_q", "type": "property"}, {"access": "protected", "host": "Queue", "name": "q", "url": "Queue.html#property_q", "type": "property"}, {"access": "", "host": "Queue", "name": "run", "url": "Queue.html#method_run", "type": "method"}, {"access": "", "host": "Queue", "name": "run", "url": "Queue.html#method_run", "type": "method"}, {"access": "private", "host": "Queue", "name": "_shift", "url": "Queue.html#method__shift", "type": "method"}, {"access": "private", "host": "Queue", "name": "_shift", "url": "Queue.html#method__shift", "type": "method"}, {"access": "", "host": "Queue", "name": "shiftCallback", "url": "Queue.html#event_shiftCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "shiftCallback", "url": "Queue.html#event_shiftCallback", "type": "event"}, {"access": "", "host": "Queue", "name": "stop", "url": "Queue.html#method_stop", "type": "method"}, {"access": "", "host": "Queue", "name": "stop", "url": "Queue.html#event_stop", "type": "event"}, {"access": "", "host": "Queue", "name": "stop", "url": "Queue.html#method_stop", "type": "method"}, {"access": "", "host": "Queue", "name": "stop", "url": "Queue.html#event_stop", "type": "event"}];
</script>
</body>
</html>