Chat Queue Persistence (#33)
* fix: workflow env * update: center title on readme * update: readme goals and format * add: icons in readme * fix: plus margin * update: environment variables in contr. * add: queue for chat history * add: set -e for workflow failure * update: version increment * fix: client null info * fix: shutoff issues
This commit is contained in:
70
src/queues/queue.ts
Normal file
70
src/queues/queue.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
// Queue interfaces for any queue class to follow
|
||||
interface IQueue<T> {
|
||||
enqueue(item: T): void
|
||||
dequeue(): T | undefined
|
||||
size(): number
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue for UserMessages
|
||||
* When the limit for messages is met, we want to clear
|
||||
* out the oldest message in the queue
|
||||
*/
|
||||
export class Queue<T> implements IQueue<T> {
|
||||
private storage: T[] = []
|
||||
|
||||
/**
|
||||
* Set up Queue
|
||||
* @param capacity max length of queue
|
||||
*/
|
||||
constructor(private capacity: number = 5) {}
|
||||
|
||||
/**
|
||||
* Put item in front of queue
|
||||
* @param item object of type T to add into queue
|
||||
*/
|
||||
enqueue(item: T): void {
|
||||
if (this.size() === this.capacity)
|
||||
throw Error('Queue has reached max capacity, you cannot add more items.')
|
||||
this.storage.push(item)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove item at end of queue
|
||||
* @returns object of type T in queue
|
||||
*/
|
||||
dequeue(): T | undefined {
|
||||
return this.storage.shift()
|
||||
}
|
||||
|
||||
/**
|
||||
* Size of the queue
|
||||
* @returns length of queue as a int/number
|
||||
*/
|
||||
size(): number {
|
||||
return this.storage.length
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the front of the queue, typically for errors
|
||||
*/
|
||||
pop(): void {
|
||||
this.storage.pop()
|
||||
}
|
||||
|
||||
/**
|
||||
* Geet the queue as an array
|
||||
* @returns a array of T items
|
||||
*/
|
||||
getItems(): T[] {
|
||||
return this.storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Get capacity of the queue
|
||||
* @returns capacity of queue
|
||||
*/
|
||||
getCapacity(): number {
|
||||
return this.capacity
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user