From c3df679449415971d32d7deb01df178d6a315f42 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Wed, 5 Feb 2025 02:33:54 +0000 Subject: [PATCH] Support for add items in custom position --- .../app/src/classes/QueueManager/index.js | 182 +++++++++--------- 1 file changed, 94 insertions(+), 88 deletions(-) diff --git a/packages/app/src/classes/QueueManager/index.js b/packages/app/src/classes/QueueManager/index.js index a3643470..3ba5cd11 100644 --- a/packages/app/src/classes/QueueManager/index.js +++ b/packages/app/src/classes/QueueManager/index.js @@ -1,122 +1,128 @@ export default class QueueManager { - constructor(params = {}) { - this.params = params + constructor(params = {}) { + this.params = params - return this - } + return this + } - prevItems = [] - nextItems = [] + prevItems = [] + nextItems = [] - currentItem = null + currentItem = null - next = ({ random = false } = {}) => { - if (this.nextItems.length === 0) { - return null - } + next = ({ random = false } = {}) => { + if (this.nextItems.length === 0) { + return null + } - if (this.currentItem) { - this.prevItems.push(this.currentItem) - } + if (this.currentItem) { + this.prevItems.push(this.currentItem) + } - if (random) { - const randomIndex = Math.floor(Math.random() * this.nextItems.length) + if (random) { + const randomIndex = Math.floor( + Math.random() * this.nextItems.length, + ) - this.currentItem = this.nextItems.splice(randomIndex, 1)[0] - } else { - this.currentItem = this.nextItems.shift() - } + this.currentItem = this.nextItems.splice(randomIndex, 1)[0] + } else { + this.currentItem = this.nextItems.shift() + } - return this.currentItem - } + return this.currentItem + } - set = (item) => { - if (typeof item === "number") { - item = this.nextItems[item] - } + set = (item) => { + if (typeof item === "number") { + item = this.nextItems[item] + } - if (this.currentItem && this.currentItem.id === item.id) { - return this.currentItem - } + if (this.currentItem && this.currentItem.id === item.id) { + return this.currentItem + } - const itemInNext = this.nextItems.findIndex((i) => i.id === item.id) - const itemInPrev = this.prevItems.findIndex((i) => i.id === item.id) + const itemInNext = this.nextItems.findIndex((i) => i.id === item.id) + const itemInPrev = this.prevItems.findIndex((i) => i.id === item.id) - if (itemInNext === -1 && itemInPrev === -1) { - throw new Error("Item not found in the queue") - } + if (itemInNext === -1 && itemInPrev === -1) { + throw new Error("Item not found in the queue") + } - if (itemInNext > -1) { - if (this.currentItem) { - this.prevItems.push(this.currentItem) - } + if (itemInNext > -1) { + if (this.currentItem) { + this.prevItems.push(this.currentItem) + } - this.prevItems.push(...this.nextItems.splice(0, itemInNext)) + this.prevItems.push(...this.nextItems.splice(0, itemInNext)) - this.currentItem = this.nextItems.shift() - } + this.currentItem = this.nextItems.shift() + } - if (itemInPrev > -1) { - if (this.currentItem) { - this.nextItems.unshift(this.currentItem) - } + if (itemInPrev > -1) { + if (this.currentItem) { + this.nextItems.unshift(this.currentItem) + } - this.nextItems.unshift(...this.prevItems.splice(itemInPrev + 1)) + this.nextItems.unshift(...this.prevItems.splice(itemInPrev + 1)) - this.currentItem = this.prevItems.pop() - } + this.currentItem = this.prevItems.pop() + } - return this.currentItem - } + return this.currentItem + } - previous = () => { - if (this.prevItems.length === 0) { - return this.currentItem - } + previous = () => { + if (this.prevItems.length === 0) { + return this.currentItem + } - if (this.currentItem) { - this.nextItems.unshift(this.currentItem) - } + if (this.currentItem) { + this.nextItems.unshift(this.currentItem) + } - this.currentItem = this.prevItems.pop() + this.currentItem = this.prevItems.pop() - return this.currentItem - } + return this.currentItem + } - add = (items) => { - if (!Array.isArray(items)) { - items = [items] - } + add = (items, position = "end") => { + if (!Array.isArray(items)) { + items = [items] + } - this.nextItems = [...this.nextItems, ...items] + if (position === "start") { + this.nextItems = [...items, ...this.nextItems] + } else { + this.nextItems = [...this.nextItems, ...items] + } - return items - } + return items + } - remove = (item) => { - const indexNext = this.nextItems.findIndex((i) => i.id === item.id) - const indexPrev = this.prevItems.findIndex((i) => i.id === item.id) + remove = (item) => { + const indexNext = this.nextItems.findIndex((i) => i.id === item.id) + const indexPrev = this.prevItems.findIndex((i) => i.id === item.id) - if (indexNext > -1) { - this.nextItems.splice(indexNext, 1) - } + if (indexNext > -1) { + this.nextItems.splice(indexNext, 1) + } - if (indexPrev > -1) { - this.prevItems.splice(indexPrev, 1) - } - } + if (indexPrev > -1) { + this.prevItems.splice(indexPrev, 1) + } + } - flush = () => { - this.nextItems = [] - this.prevItems = [] - this.currentItem = null - } + flush = () => { + this.nextItems = [] + this.prevItems = [] + this.currentItem = null + } - async load(item) { - if (typeof this.params.loadFunction === "function") { - return await this.params.loadFunction(item) - } + async load(item) { + if (typeof this.params.loadFunction === "function") { + return await this.params.loadFunction(item) + } - return item - } + return item + } }