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