PluginProbe
UpdraftCentral Dashboard / 0.8.30
UpdraftCentral Dashboard v0.8.30
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.8.30, at js/queue.js

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