break
}
await waitWorkerExit
+ this.worker.removeAllListeners()
}
private closeMessageChannel (): void {
/** @inheritdoc */
public clear (): void {
+ if (this.size > 0) {
+ let index = this.start
+ for (let i = 0; i < this.size; i++) {
+ this.nodeArray[index] = undefined
+ ++index
+ if (index === this.capacity) {
+ index = 0
+ }
+ }
+ }
this.start = 0
this.size = 0
}
return undefined
}
const index = this.start
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ const data = this.nodeArray[index]!.data
+ this.nodeArray[index] = undefined
++this.start
if (this.start === this.capacity) {
this.start = 0
}
--this.size
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return this.nodeArray[index]!.data
+ return data
}
/** @inheritdoc */
let prev: PriorityQueueNode<T> | undefined
while (node != null) {
if (node.delete(data)) {
- if (node.empty() && this.head !== this.tail) {
- if (node === this.tail) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.tail = node.next!
- } else {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- prev!.next = node.next
- if (node === this.head) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.head = prev!
- }
- }
+ if (node.empty()) {
+ this.removePriorityQueueNode(node, prev)
}
--this.size
return true
const data = targetNode!.dequeue()
--this.size
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- if (targetNode!.empty() && this.head !== this.tail) {
- if (targetNode === this.tail) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.tail = this.tail.next!
- } else {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- prev!.next = targetNode!.next
- if (targetNode === this.head) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.head = prev!
- }
- }
+ if (targetNode!.empty()) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.removePriorityQueueNode(targetNode!, prev)
}
return data
}
}
return fixedQueue
}
+
+ private removePriorityQueueNode (
+ nodeToRemove: PriorityQueueNode<T>,
+ previousNode?: PriorityQueueNode<T>
+ ): void {
+ if (this.head === this.tail) {
+ return
+ }
+
+ if (nodeToRemove === this.tail) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.tail = nodeToRemove.next!
+ } else if (nodeToRemove === this.head) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.head = previousNode!
+ this.head.next = undefined
+ } else {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ previousNode!.next = nodeToRemove.next
+ }
+
+ nodeToRemove.next = undefined
+ }
}
>()
this.checkWorkerOptions(this.opts)
if (!this.isMain) {
- // Should be once() but Node.js on windows has a bug that prevents it from working
- this.getMainWorker().on('message', this.handleReadyMessage.bind(this))
+ if (process.platform === 'win32') {
+ // Node.js on windows has a bug at worker side message counting
+ this.getMainWorker().on('message', this.handleReadyMessage.bind(this))
+ } else {
+ this.getMainWorker().once('message', this.handleReadyMessage.bind(this))
+ }
}
}
expect(fixedPriorityQueue.size).toBe(2)
expect(rtItem).toBe(2)
expect(fixedPriorityQueue.nodeArray).toMatchObject([
- { data: 2, priority: -1 },
+ undefined,
{ data: 1, priority: 0 },
{ data: 3, priority: 0 },
])
expect(fixedPriorityQueue.size).toBe(1)
expect(rtItem).toBe(1)
expect(fixedPriorityQueue.nodeArray).toMatchObject([
- { data: 2, priority: -1 },
- { data: 1, priority: 0 },
+ undefined,
+ undefined,
{ data: 3, priority: 0 },
])
expect(fixedPriorityQueue.capacity).toBe(queueSize)
expect(fixedPriorityQueue.size).toBe(0)
expect(rtItem).toBe(3)
expect(fixedPriorityQueue.nodeArray).toMatchObject([
- { data: 2, priority: -1 },
- { data: 1, priority: 0 },
- { data: 3, priority: 0 },
+ undefined,
+ undefined,
+ undefined,
])
expect(fixedPriorityQueue.capacity).toBe(queueSize)
rtItem = fixedPriorityQueue.dequeue()
expect(fixedPriorityQueue.size).toBe(0)
expect(rtItem).toBe(undefined)
expect(fixedPriorityQueue.nodeArray).toMatchObject([
- { data: 2, priority: -1 },
- { data: 1, priority: 0 },
- { data: 3, priority: 0 },
+ undefined,
+ undefined,
+ undefined,
])
expect(fixedPriorityQueue.capacity).toBe(queueSize)
})
})
it('Verify clear() behavior', () => {
- const fixedPriorityQueue = new FixedPriorityQueue()
+ const fixedPriorityQueue = new FixedPriorityQueue(2)
fixedPriorityQueue.start = 1
fixedPriorityQueue.size = 2
fixedPriorityQueue.nodeArray = [
fixedPriorityQueue.clear()
expect(fixedPriorityQueue.start).toBe(0)
expect(fixedPriorityQueue.size).toBe(0)
- expect(fixedPriorityQueue.nodeArray).toMatchObject([
- { data: 2, priority: 0 },
- { data: 3, priority: 0 },
- ])
+ expect(fixedPriorityQueue.nodeArray).toStrictEqual([undefined, undefined])
})
})
expect(fixedQueue.size).toBe(2)
expect(rtItem).toBe(1)
expect(fixedQueue.nodeArray).toMatchObject([
- { data: 1, priority: 0 },
+ undefined,
{ data: 2, priority: -1 },
{ data: 3, priority: 0 },
])
expect(fixedQueue.size).toBe(1)
expect(rtItem).toBe(2)
expect(fixedQueue.nodeArray).toMatchObject([
- { data: 1, priority: 0 },
- { data: 2, priority: -1 },
+ undefined,
+ undefined,
{ data: 3, priority: 0 },
])
expect(fixedQueue.capacity).toBe(queueSize)
expect(fixedQueue.size).toBe(0)
expect(rtItem).toBe(3)
expect(fixedQueue.nodeArray).toMatchObject([
- { data: 1, priority: 0 },
- { data: 2, priority: -1 },
- { data: 3, priority: 0 },
+ undefined,
+ undefined,
+ undefined,
])
expect(fixedQueue.capacity).toBe(queueSize)
rtItem = fixedQueue.dequeue()
expect(fixedQueue.size).toBe(0)
expect(rtItem).toBe(undefined)
expect(fixedQueue.nodeArray).toMatchObject([
- { data: 1, priority: 0 },
- { data: 2, priority: -1 },
- { data: 3, priority: 0 },
+ undefined,
+ undefined,
+ undefined,
])
expect(fixedQueue.capacity).toBe(queueSize)
})
})
it('Verify clear() behavior', () => {
- const fixedQueue = new FixedQueue()
+ const fixedQueue = new FixedQueue(2)
fixedQueue.start = 1
fixedQueue.size = 2
fixedQueue.nodeArray = [
fixedQueue.clear()
expect(fixedQueue.start).toBe(0)
expect(fixedQueue.size).toBe(0)
- expect(fixedQueue.nodeArray).toMatchObject([
- { data: 2, priority: 0 },
- { data: 3, priority: 0 },
- ])
+ expect(fixedQueue.nodeArray).toStrictEqual([undefined, undefined])
})
})