Skip to content

Commit a88052b

Browse files
committed
use FixedQueue
1 parent aacd26c commit a88052b

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

lib/web/websocket/sender.js

+13-26
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@
22

33
const { WebsocketFrameSend } = require('./frame')
44
const { opcodes, sendHints } = require('./constants')
5+
const FixedQueue = require('../../dispatcher/fixed-queue')
56

67
/** @type {typeof Uint8Array} */
78
const FastBuffer = Buffer[Symbol.species]
89

910
/**
1011
* @typedef {object} SendQueueNode
11-
* @property {SendQueueNode | null} next
1212
* @property {Promise<void> | null} promise
1313
* @property {((...args: any[]) => any)} callback
1414
* @property {Buffer | null} frame
1515
*/
1616

1717
class SendQueue {
1818
/**
19-
* @type {SendQueueNode | null}
19+
* @type {FixedQueue | null}
2020
*/
21-
#head = null
22-
/**
23-
* @type {SendQueueNode | null}
24-
*/
25-
#tail = null
21+
#queue = null
2622

2723
/**
2824
* @type {boolean}
@@ -45,22 +41,20 @@ class SendQueue {
4541
} else {
4642
/** @type {SendQueueNode} */
4743
const node = {
48-
next: null,
4944
promise: null,
5045
callback: cb,
5146
frame
5247
}
53-
if (this.#tail !== null) {
54-
this.#tail.next = node
48+
if (this.#queue === null) {
49+
this.#queue = new FixedQueue()
5550
}
56-
this.#tail = node
51+
this.#queue.push(node)
5752
}
5853
return
5954
}
6055

6156
/** @type {SendQueueNode} */
6257
const node = {
63-
next: null,
6458
promise: item.arrayBuffer().then((ab) => {
6559
node.promise = null
6660
node.frame = createFrame(ab, hint)
@@ -69,15 +63,11 @@ class SendQueue {
6963
frame: null
7064
}
7165

72-
if (this.#head === null) {
73-
this.#head = node
66+
if (this.#queue === null) {
67+
this.#queue = new FixedQueue()
7468
}
7569

76-
if (this.#tail === null) {
77-
this.#tail = node
78-
} else {
79-
this.#tail.next = node
80-
}
70+
this.#queue.push(node)
8171

8272
if (!this.#running) {
8373
this.#run()
@@ -86,9 +76,10 @@ class SendQueue {
8676

8777
async #run () {
8878
this.#running = true
89-
/** @type {SendQueueNode | null} */
90-
let node = this.#head
91-
while (node !== null) {
79+
/** @type {FixedQueue} */
80+
const queue = this.#queue
81+
while (!queue.isEmpty()) {
82+
const node = queue.shift()
9283
// wait pending promise
9384
if (node.promise !== null) {
9485
await node.promise
@@ -97,11 +88,7 @@ class SendQueue {
9788
this.#socket.write(node.frame, node.callback)
9889
// cleanup
9990
node.callback = node.frame = null
100-
// set next
101-
node = node.next
10291
}
103-
this.#head = null
104-
this.#tail = null
10592
this.#running = false
10693
}
10794
}

0 commit comments

Comments
 (0)