braid-text 0.2.87 → 0.2.88

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 +77 -15
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -160,24 +160,86 @@ function create_braid_text() {
160
160
  }
161
161
  }
162
162
 
163
- // local -> remote
163
+ // local -> remote (with in_flight queue for concurrency control)
164
+ var q = []
165
+ var in_flight = new Map()
166
+ var max_in_flight = 10
167
+ var send_pump_lock = 0
168
+ var temp_acs = new Set()
169
+ ac.signal.addEventListener('abort', () => {
170
+ for (var t of temp_acs) t.abort()
171
+ })
172
+
173
+ async function send_out(update) {
174
+ update.signal = ac.signal
175
+ update.dont_retry = true
176
+ var x = await braid_text.put(b, update)
177
+ if (x.ok) {
178
+ local_first_put()
179
+ extend_fork_point(update)
180
+ } else if (x.status === 401 || x.status === 403) {
181
+ options.on_unauthorized?.()
182
+ } else throw new Error('failed to PUT: ' + x.status)
183
+ }
184
+
185
+ async function send_pump() {
186
+ send_pump_lock++
187
+ if (send_pump_lock > 1) return
188
+ try {
189
+ if (closed) return
190
+ if (in_flight.size >= max_in_flight) return
191
+ if (!q.length) {
192
+ // Extend frontier based on in-flight updates
193
+ var frontier = resource.meta.fork_point || []
194
+ for (var u of in_flight.values())
195
+ frontier = extend_frontier(frontier, u.version, u.parents)
196
+
197
+ var temp_ac = new AbortController()
198
+ temp_acs.add(temp_ac)
199
+ var temp_ops = {
200
+ signal: temp_ac.signal,
201
+ parents: frontier,
202
+ merge_type: 'dt',
203
+ subscribe: u => u.version?.length && q.push(u)
204
+ }
205
+ await braid_text.get(a, temp_ops)
206
+ temp_ac.abort()
207
+ temp_acs.delete(temp_ac)
208
+ }
209
+ while (q.length && in_flight.size < max_in_flight) {
210
+ let u = q.shift()
211
+ if (!u.version?.length) continue
212
+ in_flight.set(u.version[0], u);
213
+ (async () => {
214
+ try {
215
+ if (closed) return
216
+ await send_out(u)
217
+ if (closed) return
218
+ in_flight.delete(u.version[0])
219
+ setTimeout(send_pump, 0)
220
+ } catch (e) {
221
+ if (e.name === 'AbortError') {
222
+ // ignore
223
+ } else handle_error(e)
224
+ }
225
+ })()
226
+ }
227
+ } finally {
228
+ var retry = send_pump_lock > 1
229
+ send_pump_lock = 0
230
+ if (retry) setTimeout(send_pump, 0)
231
+ }
232
+ }
233
+
164
234
  var a_ops = {
165
235
  signal: ac.signal,
236
+ merge_type: 'dt',
166
237
  subscribe: update => {
167
- update.signal = ac.signal
168
- update.dont_retry = true
169
- braid_text.put(b, update).then((x) => {
170
- if (x.ok) {
171
- local_first_put()
172
- extend_fork_point(update)
173
- } else if (x.status === 401 || x.status === 403) {
174
- options.on_unauthorized?.()
175
- } else throw new Error('failed to PUT: ' + x.status)
176
- }).catch(e => {
177
- if (e.name === 'AbortError') {
178
- // ignore
179
- } else handle_error(e)
180
- })
238
+ if (closed) return
239
+ if (update.version?.length) {
240
+ q.push(update)
241
+ send_pump()
242
+ }
181
243
  }
182
244
  }
183
245
  if (resource.meta.fork_point)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.87",
3
+ "version": "0.2.88",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",