@nxtedition/shared 1.4.1 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +14 -36
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -27,6 +27,7 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
|
|
|
27
27
|
|
|
28
28
|
while (readPos !== writePos) {
|
|
29
29
|
const len = buffer.readInt32LE(readPos)
|
|
30
|
+
assert(len > 0)
|
|
30
31
|
|
|
31
32
|
if (len === END_OF_PACKET) {
|
|
32
33
|
readPos = 0
|
|
@@ -62,11 +63,9 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
62
63
|
const state = new Int32Array(sharedState)
|
|
63
64
|
const buffer = Buffer.from(sharedBuffer)
|
|
64
65
|
const size = buffer.byteLength
|
|
65
|
-
const queue = []
|
|
66
66
|
|
|
67
67
|
let readPos = 0
|
|
68
68
|
let writePos = 0
|
|
69
|
-
let flushing = null
|
|
70
69
|
|
|
71
70
|
function tryWrite(maxLen, fn, opaque) {
|
|
72
71
|
readPos = Atomics.load(state, READ_INDEX)
|
|
@@ -105,35 +104,16 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
105
104
|
return true
|
|
106
105
|
}
|
|
107
106
|
|
|
108
|
-
function
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
return flushing
|
|
113
|
-
}
|
|
107
|
+
async function _write(len, fn, opaque) {
|
|
108
|
+
const buf = Buffer.allocUnsafe(len)
|
|
109
|
+
buf.subarray(0, fn(buf, opaque))
|
|
114
110
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
!tryWrite(
|
|
120
|
-
buf.byteLength,
|
|
121
|
-
(dst, buf) => {
|
|
122
|
-
dst.set(buf)
|
|
123
|
-
return buf.byteLength
|
|
124
|
-
},
|
|
125
|
-
buf
|
|
126
|
-
)
|
|
127
|
-
) {
|
|
128
|
-
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
129
|
-
if (async) {
|
|
130
|
-
await value
|
|
131
|
-
}
|
|
111
|
+
while (!tryWrite(len, (dst, buf) => buf.copy(dst), buf)) {
|
|
112
|
+
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
113
|
+
if (async) {
|
|
114
|
+
await value
|
|
132
115
|
}
|
|
133
|
-
queue.shift()
|
|
134
116
|
}
|
|
135
|
-
|
|
136
|
-
flushing = null
|
|
137
117
|
}
|
|
138
118
|
|
|
139
119
|
function write(...args) {
|
|
@@ -149,6 +129,9 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
149
129
|
len = args[0]
|
|
150
130
|
fn = args[1]
|
|
151
131
|
opaque = args[2]
|
|
132
|
+
|
|
133
|
+
assert(len > 0)
|
|
134
|
+
assert(typeof fn === 'function')
|
|
152
135
|
} else {
|
|
153
136
|
if (Array.isArray(args[0])) {
|
|
154
137
|
args = args[0]
|
|
@@ -175,18 +158,13 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
175
158
|
opaque = args
|
|
176
159
|
}
|
|
177
160
|
|
|
178
|
-
if (!
|
|
179
|
-
return
|
|
161
|
+
if (!tryWrite(len, fn, opaque)) {
|
|
162
|
+
return _write(len, fn, opaque)
|
|
180
163
|
}
|
|
181
|
-
|
|
182
|
-
const buf = Buffer.allocUnsafe(len)
|
|
183
|
-
queue.push(buf.subarray(0, fn(0, buf)))
|
|
184
|
-
|
|
185
|
-
return flush()
|
|
186
164
|
}
|
|
187
165
|
|
|
188
166
|
write.write = write
|
|
189
|
-
write.flush =
|
|
167
|
+
write.flush = () => {}
|
|
190
168
|
|
|
191
169
|
return write
|
|
192
170
|
}
|