@nxtedition/shared 1.3.0 → 1.4.2

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.
Files changed (2) hide show
  1. package/index.js +18 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -71,6 +71,8 @@ export function writer({ sharedState, sharedBuffer }) {
71
71
  function tryWrite(maxLen, fn, opaque) {
72
72
  readPos = Atomics.load(state, READ_INDEX)
73
73
 
74
+ maxLen += 4 // len
75
+
74
76
  if (size - writePos < maxLen + 4) {
75
77
  if (readPos < maxLen + 4) {
76
78
  return false
@@ -103,7 +105,14 @@ export function writer({ sharedState, sharedBuffer }) {
103
105
  return true
104
106
  }
105
107
 
106
- async function flush() {
108
+ function flush() {
109
+ if (queue.length && !flushing) {
110
+ flushing = _flush()
111
+ }
112
+ return flushing
113
+ }
114
+
115
+ async function _flush() {
107
116
  while (queue.length) {
108
117
  const buf = queue[0]
109
118
  while (
@@ -113,7 +122,7 @@ export function writer({ sharedState, sharedBuffer }) {
113
122
  dst.set(buf)
114
123
  return buf.byteLength
115
124
  },
116
- queue[0]
125
+ buf
117
126
  )
118
127
  ) {
119
128
  const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
@@ -129,7 +138,7 @@ export function writer({ sharedState, sharedBuffer }) {
129
138
 
130
139
  function write(...args) {
131
140
  if (!args.length) {
132
- return true
141
+ return
133
142
  }
134
143
 
135
144
  let len
@@ -145,7 +154,7 @@ export function writer({ sharedState, sharedBuffer }) {
145
154
  args = args[0]
146
155
  }
147
156
 
148
- len = 4
157
+ len = 0
149
158
  for (const buf of args) {
150
159
  len += buf.byteLength ?? buf.length * 3
151
160
  }
@@ -171,10 +180,13 @@ export function writer({ sharedState, sharedBuffer }) {
171
180
  }
172
181
 
173
182
  const buf = Buffer.allocUnsafe(len)
174
- queue.push(buf.subarray(0, fn(0, buf)))
183
+ queue.push(buf.subarray(0, fn(buf, opaque)))
175
184
 
176
- return (flushing ??= flush())
185
+ return flush()
177
186
  }
178
187
 
188
+ write.write = write
189
+ write.flush = flush
190
+
179
191
  return write
180
192
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "1.3.0",
3
+ "version": "1.4.2",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {