Skip to content

Commit

Permalink
feat(Queue): Adds Queue datastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaaz committed Feb 20, 2020
1 parent 27d7d9c commit 21b3014
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 9 deletions.
1 change: 1 addition & 0 deletions esmExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export { default as Prompt } from './src/Utility/Discord/Prompt';
export { default as MessageCollector } from './src/Utility/Discord/MessageCollector';
export { default as ReactionCollector } from './src/Utility/Discord/ReactionCollector';
// External
export { default as Queue } from './src/Utility/External/Queue';
export { default as FunctionQueue } from './src/Utility/External/FunctionQueue';
export { default as AutoQueue } from './src/Utility/External/AutoQueue';
export { default as AsyncQueue } from './src/Utility/External/AsyncQueue';
Expand Down
1 change: 1 addition & 0 deletions examples/djs/src/modules/Private/commands/Eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Embed,
Prompt,
MessageCollector,
Queue,
FunctionQueue,
AutoQueue,
AsyncQueue,
Expand Down
1 change: 1 addition & 0 deletions examples/eris/src/modules/Private/commands/Eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Embed,
Prompt,
MessageCollector,
Queue,
FunctionQueue,
AutoQueue,
AsyncQueue,
Expand Down
6 changes: 3 additions & 3 deletions src/Utility/External/AsyncQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class AsyncQueue extends FunctionQueue {
* @memberof AsyncQueue
*/
async exec() {
if (this._functions.length > 0) {
if (this._functions.size > 0) {
this._running = true;

const func = this._functions.shift();
const func = this._functions.dequeue();
try {
await func();
} catch (err) {
Expand Down Expand Up @@ -50,7 +50,7 @@ class AsyncQueue extends FunctionQueue {
add(func, toExec = true, ...args) {
const promise = new Promise( (resolve, reject) => {
const fn = this.createClosure(func, resolve, reject, ...args);
this._functions.push(fn);
this._functions.queue(fn);
} );

if (toExec && !this._running) {
Expand Down
4 changes: 2 additions & 2 deletions src/Utility/External/AutoQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class AutoQueue extends FunctionQueue {
* @memberof AutoQueue
*/
async exec() {
if (this._functions.length > 0) {
if (this._functions.size > 0) {
this._running = true;

const func = this._functions.shift();
const func = this._functions.dequeue();
try {
await func();
} catch (err) {
Expand Down
10 changes: 6 additions & 4 deletions src/Utility/External/FunctionQueue.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Queue from './Queue';

/**
* This default FunctionQueue works in a synchronous fashion.
* It will execute all synchronous functions sequentially.
Expand All @@ -21,7 +23,7 @@ class FunctionQueue {
/**
* @type {Array<Function>}

This comment has been minimized.

Copy link
@bsian03

bsian03 Feb 23, 2020

Contributor

Array<Queue>

This comment has been minimized.

Copy link
@Khaaz

Khaaz Feb 24, 2020

Author Owner

Was already fixed: Queue<Function>

*/
this._functions = [];
this._functions = new Queue();
this._running = false;

this.stopOnError = stopOnError;
Expand All @@ -32,9 +34,9 @@ class FunctionQueue {
* @memberof FunctionQueue
*/
exec() {
if (this._functions.length > 0) {
if (this._functions.size > 0) {
this._running = true;
const func = this._functions.shift();
const func = this._functions.dequeue();

try {
func();
Expand Down Expand Up @@ -63,7 +65,7 @@ class FunctionQueue {
add(func, toExec = true, ...args) {
const fn = this.createClosure(func, ...args);

this._functions.add(fn);
this._functions.queue(fn);

if (toExec && !this._running) {
this.exec();
Expand Down
80 changes: 80 additions & 0 deletions src/Utility/External/Queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Queue class.
*
* @author KhaaZ
*
* @template T
*
* @prop {Array<T>} _elements
* @prop {Boolean} max
* @prop {Boolean} replaceOnMax
*
* @class Queue
*/
class Queue {
/**
* Creates an instance of Queue.
*
* @param {Number} [max=null] - Maximum number of elements that can be added in this queue
* @param {Boolean} [replaceOnMax=true] - Whether to replace value when adding if max is reached (dequeue first element and queue new element)
* @memberof Queue
*/
constructor(max = null, replaceOnMax = true) {
this._elements = [];
this.max = max;
this.replaceOnMax = replaceOnMax;
}

/**
* Returns the Queue size
*
* @readonly
* @type {Number}
* @memberof Queue
*/
get size() {
return this._elements.length;
}

/**
* Return first element of this queue
*
* @returns {T}
* @memberof Queue
*/
first() {
return this._elements[0];
}

/**
* Queue up an element.
*
* @param {T} elem
* @param {Boolean} replaceOnMax

This comment has been minimized.

Copy link
@bsian03

bsian03 Feb 23, 2020

Contributor

Indicate optional param

     * @param {Boolean} [replaceOnMax]

This comment has been minimized.

Copy link
@Khaaz

Khaaz Feb 24, 2020

Author Owner

fixed in 5484271

* @returns {Boolean} Whether element was successfully added
* @memberof Queue
*/
queue(elem, replaceOnMax) {
if (this.max && this._elements.length === this.max) {
if ( (replaceOnMax !== undefined) ? replaceOnMax : this.replaceOnMax) {
this._elements.shift();
} else {
return false;
}
}
this._elements.push(elem);
return true;
}

/**
* Dequeue an element and returns it
*
* @returns {T}
* @memberof Queue
*/
dequeue() {
return this._elements.shift();
}
}

export default Queue;

0 comments on commit 21b3014

Please sign in to comment.