JavaScript 生成器:实现票队列系统

JavaScript Generator: implement a ticket queue system

我正在尝试实现一个票证队列系统,默认情况下,它将有 3 个不同的队列来持有分别具有严重性 1、严重性 2 和严重性 3 的票。我有一个方法 getTicketAtHighestSeverity 其中 return 是最高严重性队列中最旧的票证,因此它从第一个队列开始并查找队列中的第一个项目并移动到下一个队列如果当前队列为空并且另一种方法 getTicketBySeverity 遍历所有队列 return 票从最高严重性开始

这是我的实现。

class ticketQueues {
    constructor(numOfQueues = 3) {
        this.queues = Array.from({length: numOfQueues}).fill([])
    }

    addSev1(ticket) {
        this.queues[0].push(ticket)
    }

    addSev2(ticket) {
        this.queues[1].push(ticket)
    }

    addSev3(ticket) {
        this.queues[2].push(ticket)
    }

    *getTicketBySeverity() {
        for(const queue of this.queues) {
            for(const ticket of queue) {
                yield ticket
            }
        }

        return null
    }

    getTicketAtHighestSeverity() {
        for(const queue of this.queues) {
            for(const ticket of queue) {
                return ticket
            }
        }
        
        return null
    }

}

但是 getTicketBySeverity 似乎没有正常工作。


const queues = new ticketQueues()
queues.addSev1({timestamp: Date(), name: 't1'})
queues.addSev2({timestamp: Date(), name: 't2'})
queues.addSev3({timestamp: Date(), name: 't3'})

for(let i = 2; i >= 0; i--) {
    console.log(queues.getTicketBySeverity().next().value) //  this keeps returning the first item from the queue
}


因为它不会移动到下一张票,因为它只是 return 第一张票。我选择 Generator 来实现这个方法的原因是我想利用惰性评估模型,因为数据集可能很大,我不想一次获得所有的票。

有人可以用 getTicketBySeverity 修复我的实现吗?关于这里的命名有什么建议吗?我觉得这里的命名即 getTicketBySeveritygetTicketAtHighestSeverity 可能不是最佳选择。另外,如果您认为这可能不是一个合法的用例,请随时评论我对 Generator 的使用。

一个问题是

this.queues = Array.from({length: numOfQueues}).fill([])

.fill 不适用于非基元(通常),因为新数组中的每个项目都将是对 相同对象 的引用。您只创建了一个数组。问题与以下内容无法按预期工作的原因相同:

const subarr = [];
arr.push(subarr);
arr.push(subarr);

因为只有一个 subarr

使用带有 Array.from 的映射器函数为每次迭代显式创建一个新数组:

this.queues = Array.from({length: numOfQueues}, () => []);

此外,要遍历迭代器,请使用 for..of - 或者在找到时从数组中删除找到的项目(否则,每次调用时,它都会 return相同的项目)。

您可以使用 for..of 通过将参数传递给生成器并跟踪生成的元素数量来控制一次删除的票证数量:

class ticketQueues {
    constructor(numOfQueues = 3) {
        this.queues = Array.from({length: numOfQueues}, () => []);
    }

    addSev1(ticket) {
        this.queues[0].push(ticket)
    }

    addSev2(ticket) {
        this.queues[1].push(ticket)
    }

    addSev3(ticket) {
        this.queues[2].push(ticket)
    }
    *getTicketsBySeverity(limit) {
        let count = 0;
        for(const queue of this.queues) {
            while (queue.length) {
                yield queue.shift();
                count++;
                if (count === limit) {
                    return null;
                }
            }
        }
        return null
    }
}



const queues = new ticketQueues()
queues.addSev1({timestamp: Date(), name: 't1'})
queues.addSev1({timestamp: Date(), name: 't1-2'})
queues.addSev2({timestamp: Date(), name: 't2'})
queues.addSev3({timestamp: Date(), name: 't3'})

for (const ticket of queues.getTicketsBySeverity(3)) {
  console.log(ticket);
}
console.log(queues.queues);