|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('queue-base', function(Y) { |
|
9 |
|
10 /** |
|
11 * <p>The Queue module adds a common data structure for FIFO operations. In its |
|
12 * simplest form, it is little more than an array wrapper. Additional |
|
13 * submodules introduce more functionality such as promotion and removal of |
|
14 * queued items.</p> |
|
15 * |
|
16 * <p>An AsyncQueue class is provided in the queue-run submodule. This class |
|
17 * affords a mechanism to do complex sequential and iterative callback |
|
18 * execution across configured timeouts. |
|
19 * |
|
20 * @module queue |
|
21 */ |
|
22 |
|
23 /** |
|
24 * A simple FIFO queue. Items are added to the Queue with add(1..n items) and |
|
25 * removed using next(). |
|
26 * |
|
27 * @module queue |
|
28 * @submodule queue-base |
|
29 * @class Queue |
|
30 * @param item* {MIXED} 0..n items to seed the queue |
|
31 */ |
|
32 function Queue() { |
|
33 this._init(); |
|
34 this.add.apply(this, arguments); |
|
35 } |
|
36 |
|
37 Queue.prototype = { |
|
38 /** |
|
39 * Initialize the queue |
|
40 * |
|
41 * @method _init |
|
42 * @protected |
|
43 */ |
|
44 _init : function () { |
|
45 /** |
|
46 * The collection of enqueued items |
|
47 * |
|
48 * @property _q |
|
49 * @type {Array} |
|
50 * @protected |
|
51 */ |
|
52 this._q = []; |
|
53 }, |
|
54 |
|
55 /** |
|
56 * Get the next item in the queue. |
|
57 * |
|
58 * @method next |
|
59 * @return {MIXED} the next item in the queue |
|
60 */ |
|
61 next : function () { |
|
62 return this._q.shift(); |
|
63 }, |
|
64 |
|
65 /** |
|
66 * Add 0..n items to the end of the queue |
|
67 * |
|
68 * @method add |
|
69 * @param item* {MIXED} 0..n items |
|
70 */ |
|
71 add : function () { |
|
72 Y.Array.each(Y.Array(arguments,0,true),function (fn) { |
|
73 this._q.push(fn); |
|
74 },this); |
|
75 |
|
76 return this; |
|
77 }, |
|
78 |
|
79 /** |
|
80 * Returns the current number of queued items |
|
81 * |
|
82 * @method size |
|
83 * @return {Number} |
|
84 */ |
|
85 size : function () { |
|
86 return this._q.length; |
|
87 } |
|
88 }; |
|
89 |
|
90 Y.Queue = Queue; |
|
91 |
|
92 |
|
93 }, '3.0.0b1' ); |