mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
fix Queue, tweaks on relay.ts and make relay.test.ts pass.
This commit is contained in:
26
utils.ts
26
utils.ts
@@ -94,11 +94,11 @@ export function insertEventIntoAscendingList(sortedArray: Event[], event: Event)
|
||||
|
||||
export class QueueNode<V> {
|
||||
public value: V
|
||||
public next: QueueNode<V> | null
|
||||
public next: QueueNode<V> | null = null
|
||||
public prev: QueueNode<V> | null = null
|
||||
|
||||
constructor(message: V) {
|
||||
this.value = message
|
||||
this.next = null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,9 +114,17 @@ export class Queue<V> {
|
||||
enqueue(value: V): boolean {
|
||||
const newNode = new QueueNode(value)
|
||||
if (!this.last) {
|
||||
// list is empty
|
||||
this.first = newNode
|
||||
this.last = newNode
|
||||
} else if (this.last === this.first) {
|
||||
// list has a single element
|
||||
this.last = newNode
|
||||
this.last.prev = this.first
|
||||
this.first.next = newNode
|
||||
} else {
|
||||
// list has elements, add as last
|
||||
newNode.prev = this.last
|
||||
this.last.next = newNode
|
||||
this.last = newNode
|
||||
}
|
||||
@@ -126,10 +134,16 @@ export class Queue<V> {
|
||||
dequeue(): V | null {
|
||||
if (!this.first) return null
|
||||
|
||||
let prev = this.first
|
||||
this.first = prev.next
|
||||
prev.next = null
|
||||
if (this.first === this.last) {
|
||||
const target = this.first
|
||||
this.first = null
|
||||
this.last = null
|
||||
return target.value
|
||||
}
|
||||
|
||||
return prev.value
|
||||
const target = this.first
|
||||
this.first = target.next
|
||||
|
||||
return target.value
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user