2
2
3
3
const { WebsocketFrameSend } = require ( './frame' )
4
4
const { opcodes, sendHints } = require ( './constants' )
5
- const FixedQueue = require ( '../../dispatcher/fixed-queue' )
6
5
7
6
/** @type {typeof Uint8Array } */
8
7
const FastBuffer = Buffer [ Symbol . species ]
9
8
10
9
/**
11
- * @typedef {object } QueueNode
10
+ * @typedef {object } SendQueueNode
11
+ * @property {SendQueueNode | null } next
12
12
* @property {Promise<void> | null } promise
13
13
* @property {((...args: any[]) => any) } callback
14
14
* @property {Buffer | null } frame
15
15
*/
16
16
17
17
class SendQueue {
18
18
/**
19
- * @type {FixedQueue | null }
19
+ * @type {SendQueueNode | null }
20
20
*/
21
- #queue = null
21
+ #head = null
22
+ /**
23
+ * @type {SendQueueNode | null }
24
+ */
25
+ #tail = null
22
26
23
27
/**
24
28
* @type {boolean }
@@ -39,19 +43,24 @@ class SendQueue {
39
43
// fast-path
40
44
this . #socket. write ( frame , cb )
41
45
} else {
42
- /** @type {QueueNode } */
46
+ /** @type {SendQueueNode } */
43
47
const node = {
48
+ next : null ,
44
49
promise : null ,
45
50
callback : cb ,
46
51
frame
47
52
}
48
- ( this . #queue ??= new FixedQueue ( ) ) . push ( node )
53
+ if ( this . #tail !== null ) {
54
+ this . #tail. next = node
55
+ }
56
+ this . #tail = node
49
57
}
50
58
return
51
59
}
52
60
53
- /** @type {QueueNode } */
61
+ /** @type {SendQueueNode } */
54
62
const node = {
63
+ next : null ,
55
64
promise : item . arrayBuffer ( ) . then ( ( ab ) => {
56
65
node . promise = null
57
66
node . frame = createFrame ( ab , hint )
@@ -60,7 +69,13 @@ class SendQueue {
60
69
frame : null
61
70
}
62
71
63
- ( this . #queue ??= new FixedQueue ( ) ) . push ( node )
72
+ if ( this . #tail === null ) {
73
+ this . #tail = node
74
+ }
75
+
76
+ if ( this . #head === null ) {
77
+ this . #head = node
78
+ }
64
79
65
80
if ( ! this . #running) {
66
81
this . #run( )
@@ -69,10 +84,9 @@ class SendQueue {
69
84
70
85
async #run ( ) {
71
86
this . #running = true
72
- /** @type {FixedQueue } */
73
- const queue = this . #queue
74
- while ( ! queue . isEmpty ( ) ) {
75
- const node = queue . shift ( )
87
+ /** @type {SendQueueNode | null } */
88
+ let node = this . #head
89
+ while ( node !== null ) {
76
90
// wait pending promise
77
91
if ( node . promise !== null ) {
78
92
await node . promise
@@ -81,7 +95,11 @@ class SendQueue {
81
95
this . #socket. write ( node . frame , node . callback )
82
96
// cleanup
83
97
node . callback = node . frame = null
98
+ // set next
99
+ node = node . next
84
100
}
101
+ this . #head = null
102
+ this . #tail = null
85
103
this . #running = false
86
104
}
87
105
}
0 commit comments