PluginProbe
UpdraftCentral Dashboard / 0.6.0
UpdraftCentral Dashboard v0.6.0
0.8.33 0.7.2 0.7.3 0.7.4 0.8.0 0.8.1 0.8.10 0.8.11 0.8.12 0.8.13 0.8.14 0.8.15 0.8.16 0.8.17 0.8.18 0.8.19 0.8.2 0.8.20 0.8.21 0.8.22 0.8.23 0.8.24 0.8.25 0.8.26 0.8.27 All 51 releases
updraftcentral / js / queue.js

queue.js in UpdraftCentral Dashboard 0.6.0, at js/queue.js

142 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /*
2
3 Adapted and extended from the work of Stephen Morley - http://code.stephenmorley.org/javascript/queues/
4
5 Queue.js
6
7 A function to represent a queue
8
9 Created by Stephen Morley - http://code.stephenmorley.org/ - and released under
10 the terms of the CC0 1.0 Universal legal code:
11
12 http://creativecommons.org/publicdomain/zero/1.0/legalcode
13
14 */
15
16 /**
17 * Creates a new queue. A queue is a first-in-first-out (FIFO) data structure -
18 * items are added to the end of the queue and removed from the front.
19 * @returns {void}
20 */
21 function UpdraftCentral_Queue() {
22
23 // initialise the queue and offset
24 var queue = [];
25 var offset = 0;
26 var locked = false;
27
28 /**
29 * Returns the length of the queue.
30 *
31 * @returns {number} - the length of the queue
32 */
33 this.get_length = function() {
34 return (queue.length - offset);
35 }
36
37 /**
38 * Query whether the queue is empty or not
39 *
40 * @returns {boolean} - returns true if the queue is empty, and false otherwise.
41 */
42 this.is_empty = function() {
43 return (queue.length == 0);
44 }
45
46 /**
47 *
48 * Enqueues the specified item. The parameter is:
49 *
50 * @param {*} item - the item to enqueue
51 * @returns {void}
52 */
53 this.enqueue = function(item) {
54 queue.push(item);
55 }
56
57 /**
58 * Returns the queue lock status
59 *
60 * @returns {boolean} - whether the queue is locked or not
61 */
62 this.is_locked = function() {
63 return locked;
64 }
65
66 /**
67 * Attempt to get the queue lock
68 *
69 * @returns {boolean} - whether the attempt succeeded or not
70 */
71 this.get_lock = function() {
72 if (locked) { return false; }
73 this.lock();
74 return true;
75 }
76
77 /**
78 * Dequeues an item and returns it. If the queue is empty, the value
79 * 'undefined' is returned.
80 *
81 * @returns {*} - returns and removes the item at the front of the queue, or undefined if the queue is empty
82 */
83 this.dequeue = function() {
84
85 // if the queue is empty, return immediately
86 if (queue.length == 0) return undefined;
87
88 // store the item at the front of the queue
89 var item = queue[offset];
90
91 // increment the offset and remove the free space if necessary
92 if (++ offset * 2 >= queue.length) {
93 queue = queue.slice(offset);
94 offset = 0;
95 }
96
97 // return the dequeued item
98 return item;
99
100 }
101
102 /**
103 * Lock the queue
104 * @returns {void}
105 */
106 this.lock = function() {
107 locked = true;
108 }
109
110 /**
111 * Unlock the queue
112 * @returns {void}
113 */
114 this.unlock = function() {
115 locked = false;
116 }
117
118 /**
119 * Returns the item at the front of the queue (without dequeuing it). If the
120 * queue is empty then undefined is returned.
121 *
122 * @returns {*} - returns the item at the front of the queue, or undefined if the queue is empty
123 */
124 this.peek = function() {
125 return (queue.length > 0 ? queue[offset] : undefined);
126 }
127
128 /**
129 * Replaces the item at the front of the queue (if any)
130 *
131 * @param {*} item - the item to put at the front of the queue
132 *
133 * @returns {boolean} - whether or not the item was successfully replaced
134 */
135 this.replace_front = function(item) {
136 if (queue.length < 1) { return false; }
137 queue[offset] = item;
138 return true;
139 }
140
141 }
142