Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SortedList): abstract SortedList #69

Merged
merged 8 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions esmExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export { default as Collector } from './src/Utility/Discord/Collectors/Collector
export { default as MessageCollector } from './src/Utility/Discord/Collectors/MessageCollector';
export { default as ReactionCollector } from './src/Utility/Discord/Collectors/ReactionCollector';
// External
export { default as SortedList } from './src/Utility/External/SortedList';
export { default as Stack } from './src/Utility/External/Stack';
export { default as Queue } from './src/Utility/External/Queue';
export { default as FunctionQueue } from './src/Utility/External/FunctionQueue';
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,
SortedList,
Stack,
Queue,
FunctionQueue,
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,
SortedList,
Stack,
Queue,
FunctionQueue,
Expand Down
13 changes: 7 additions & 6 deletions src/Utility/Discord/Collectors/Collector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from 'events';
import NotImplementedException from '../../../Errors/NotImplementedException';
import Collection from '../../Collection';
import TimeoutQueue from './TimeoutQueue';
import SortedList from '../../External/SortedList';
import AxonError from '../../../Errors/AxonError';

/**
Expand All @@ -10,6 +10,7 @@ import AxonError from '../../../Errors/AxonError';
* @typedef {{
* id: String, collected: Map<String, T>, options: Object, resolve: (T) => Promise<T>, reject: (T) => Promise<T>),
* }} CollectorContainer<T>
* @typedef {{ id: String, timeout: Number }} Timeout
*/

/**
Expand All @@ -26,7 +27,7 @@ import AxonError from '../../../Errors/AxonError';
* @extends {EventEmitter}
* @prop {AxonClient} _axon - The AxonClient instance
* @prop {Collection<CollectorContainer<T>>} collectors - Collection of CollectorContainer
* @prop {TimeoutQueue} timeoutQueue - The current timeout queue sorted with the first timeout due at the top of the queue
* @prop {SortedList<Timeout>} timeoutQueue - The current timeout sorted with the first timeout due at the top of the queue
* @prop {Number} _INCREMENT - Unique increment count used to generate ids
* @prop {Boolean} running - Whether the Collector is currently running
* @prop {String} _intervalID - setInterval ID used to clear setinterval
Expand All @@ -43,7 +44,7 @@ class Collector extends EventEmitter {
this._axon = axonClient;

this.collectors = new Collection();
this.timeoutQueue = new TimeoutQueue();
this.timeoutQueue = new SortedList( ( (toInsert, baseElement) => toInsert.timeout >= baseElement.timeout) );

this._INCREMENT = 0;
this.running = false;
Expand Down Expand Up @@ -191,7 +192,7 @@ class Collector extends EventEmitter {
} );

const timestamp = Date.now() + options.timeout;
this.timeoutQueue.add(id, timestamp);
this.timeoutQueue.add( { id, timeout: timestamp } );

if (!this.running) {
this.setListeners();
Expand Down Expand Up @@ -219,8 +220,8 @@ class Collector extends EventEmitter {
}

this._intervalID = setInterval( () => {
while (this.timeoutQueue.peek() && Date.now() > this.timeoutQueue.peek().timeout) {
const { id } = this.timeoutQueue.getNext();
while (this.timeoutQueue.first() && Date.now() > this.timeoutQueue.first().timeout) {
const { id } = this.timeoutQueue.shift();
this.emit('timeout', id);
}
}, 100);
Expand Down
65 changes: 0 additions & 65 deletions src/Utility/Discord/Collectors/TimeoutQueue.js

This file was deleted.

104 changes: 104 additions & 0 deletions src/Utility/External/SortedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* SortedList Object. Sorted Array of element (ascending or descending order)
*
* @author KhaaZ
*
* @template T
*
* @class SortedList
* @prop {Boolean} acending
* @prop {(toInsert: T, baseElement: T) => Boolean)} comparator - Whether toInsert is bigger than baseElement
* @prop {Array<T>} list
*/
class SortedList {
constructor(comparator = null, ascending = true) {
this.comparator = comparator || ( (toInsert, baseElement) => toInsert >= baseElement);
this.ascending = ascending;
this.list = [];
}

/**
* Size of the list
*
* @readonly
* @type {Number}
* @memberof SortedList
*/
get size() {
return this.list.length;
}

/**
* Whether the list is empty or not
*
* @returns {Boolean}
* @memberof SortedList
*/
isEmpty() {
return this.list.length === 0;
}

/**
* Returns the first element of the list without removing it
*
* @returns {T}
* @memberof SortedList
*/
first() {
return this.list[0];
}

/**
* Returns the last element of the list without removing it
*
* @returns {T}
* @memberof SortedList
*/
last() {
return this.list[this.list.length - 1];
}

/**
* Adds an element in the queue, sorting by ascending element
*
* @param {T} element
* @param {(toInsert: T, baseElement: T) => Boolean} comparator
* @memberof SortedList
*/
add(element, comparator = null) {
const comp = comparator || this.comparator;

let i;
for (i = 0; i < this.list.length; i++) {
// Next condition is optimised for: this.ascending ? !comp(element, this.list[i] ) : comp(element, this.list[i] )
if (!(this.ascending === comp(element, this.list[i] ) ) ) {
break;
}
}


this.list.splice(i, 0, element);
}

/**
* Get the first element in the list and removes it
*
* @returns {T}
* @memberof SortedList
*/
shift() {
return this.list.shift();
}

/**
* Get the last element in the list and removes it
*
* @returns {T}
* @memberof SortedList
*/
pop() {
return this.list.pop();
}
}

export default SortedList;