@nxtedition/shared 1.4.0 → 2.0.0
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 +13 -37
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -62,15 +62,15 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
62
62
|
const state = new Int32Array(sharedState)
|
|
63
63
|
const buffer = Buffer.from(sharedBuffer)
|
|
64
64
|
const size = buffer.byteLength
|
|
65
|
-
const queue = []
|
|
66
65
|
|
|
67
66
|
let readPos = 0
|
|
68
67
|
let writePos = 0
|
|
69
|
-
let flushing = null
|
|
70
68
|
|
|
71
69
|
function tryWrite(maxLen, fn, opaque) {
|
|
72
70
|
readPos = Atomics.load(state, READ_INDEX)
|
|
73
71
|
|
|
72
|
+
maxLen += 4 // len
|
|
73
|
+
|
|
74
74
|
if (size - writePos < maxLen + 4) {
|
|
75
75
|
if (readPos < maxLen + 4) {
|
|
76
76
|
return false
|
|
@@ -103,35 +103,16 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
103
103
|
return true
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
function
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
return flushing
|
|
111
|
-
}
|
|
106
|
+
async function _write(len, fn, opaque) {
|
|
107
|
+
const buf = Buffer.allocUnsafe(len)
|
|
108
|
+
buf.subarray(0, fn(buf, opaque))
|
|
112
109
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
!tryWrite(
|
|
118
|
-
buf.byteLength,
|
|
119
|
-
(dst, buf) => {
|
|
120
|
-
dst.set(buf)
|
|
121
|
-
return buf.byteLength
|
|
122
|
-
},
|
|
123
|
-
queue[0]
|
|
124
|
-
)
|
|
125
|
-
) {
|
|
126
|
-
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
127
|
-
if (async) {
|
|
128
|
-
await value
|
|
129
|
-
}
|
|
110
|
+
while (!tryWrite(len, (dst, buf) => buf.copy(dst), buf)) {
|
|
111
|
+
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
112
|
+
if (async) {
|
|
113
|
+
await value
|
|
130
114
|
}
|
|
131
|
-
queue.shift()
|
|
132
115
|
}
|
|
133
|
-
|
|
134
|
-
flushing = null
|
|
135
116
|
}
|
|
136
117
|
|
|
137
118
|
function write(...args) {
|
|
@@ -152,7 +133,7 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
152
133
|
args = args[0]
|
|
153
134
|
}
|
|
154
135
|
|
|
155
|
-
len =
|
|
136
|
+
len = 0
|
|
156
137
|
for (const buf of args) {
|
|
157
138
|
len += buf.byteLength ?? buf.length * 3
|
|
158
139
|
}
|
|
@@ -173,18 +154,13 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
173
154
|
opaque = args
|
|
174
155
|
}
|
|
175
156
|
|
|
176
|
-
if (!
|
|
177
|
-
return
|
|
157
|
+
if (!tryWrite(len, fn, opaque)) {
|
|
158
|
+
return _write(len, fn, opaque)
|
|
178
159
|
}
|
|
179
|
-
|
|
180
|
-
const buf = Buffer.allocUnsafe(len)
|
|
181
|
-
queue.push(buf.subarray(0, fn(0, buf)))
|
|
182
|
-
|
|
183
|
-
return flush()
|
|
184
160
|
}
|
|
185
161
|
|
|
186
162
|
write.write = write
|
|
187
|
-
write.flush =
|
|
163
|
+
write.flush = () => {}
|
|
188
164
|
|
|
189
165
|
return write
|
|
190
166
|
}
|