From bd776c5066c04b6117b977bd7a1a8a9bd17d4a41 Mon Sep 17 00:00:00 2001 From: JaoodxD Date: Tue, 14 May 2024 22:39:42 +0300 Subject: [PATCH] Fix emptying head with remove lead to wrong isEmpty behavior --- src/fixed-queue.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/fixed-queue.ts b/src/fixed-queue.ts index d0410c12..bb57a066 100644 --- a/src/fixed-queue.ts +++ b/src/fixed-queue.ts @@ -153,7 +153,7 @@ export default class FixedQueue implements TaskQueue { } remove (task: Task) { - let prev = null; + let prev: FixedCircularBuffer | null = null; let buffer = this.tail; while (true) { if (buffer.list.includes(task)) { @@ -166,11 +166,17 @@ export default class FixedQueue implements TaskQueue { buffer = buffer.next; } if (buffer.isEmpty()) { - if (prev !== null) { - prev.next = buffer.next; + // removing tail + if (prev === null) { + // if tail is not the last buffer + if (buffer.next !== null) this.tail = buffer.next; } else { - if (buffer.next) { - this.tail = buffer.next; + // removing head + if (buffer.next === null) { + this.head = prev; + } else { + // removing buffer from middle + prev.next = buffer.next; } } }