braid-text 0.3.8 → 0.3.9
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/client/simpleton-sync.js +33 -68
- package/package.json +1 -1
package/client/simpleton-sync.js
CHANGED
|
@@ -45,15 +45,6 @@
|
|
|
45
45
|
// get_state: () => current_state
|
|
46
46
|
// returns the current state (e.g., textarea.value)
|
|
47
47
|
//
|
|
48
|
-
// [DEPRECATED] apply_remote_update: ({patches, state}) => {...}
|
|
49
|
-
// this is for incoming changes;
|
|
50
|
-
// one of these will be non-null,
|
|
51
|
-
// and can be applied to the current state.
|
|
52
|
-
//
|
|
53
|
-
// [DEPRECATED] generate_local_diff_update: (client_state) => {...}
|
|
54
|
-
// this is to generate outgoing changes,
|
|
55
|
-
// and if there are changes, returns { patches, new_state }
|
|
56
|
-
//
|
|
57
48
|
// content_type: used for Accept and Content-Type headers
|
|
58
49
|
//
|
|
59
50
|
// returns { changed, abort }
|
|
@@ -75,18 +66,12 @@
|
|
|
75
66
|
//
|
|
76
67
|
// PUT requests:
|
|
77
68
|
// retry: (res) => res.status !== 550 — retry all errors EXCEPT
|
|
78
|
-
// HTTP 550 (
|
|
69
|
+
// HTTP 550 (Repr-Digest mismatch, meaning client is out of sync).
|
|
70
|
+
// This means:
|
|
79
71
|
// - Connection failure: retried with backoff
|
|
80
|
-
// - HTTP 408, 429, 500, 502, 503, 504, etc.: retried
|
|
81
|
-
// - HTTP 550:
|
|
82
|
-
//
|
|
83
|
-
// - HTTP 401, 403: retried by braid_fetch, but the !r.ok check
|
|
84
|
-
// throws, which calls on_error and exits the async loop
|
|
85
|
-
//
|
|
86
|
-
// NOTE: When a PUT permanently fails (550 or !r.ok), outstanding_changes
|
|
87
|
-
// is incremented but never decremented. If this happens repeatedly, the
|
|
88
|
-
// client will eventually hit max_outstanding_changes and stop sending.
|
|
89
|
-
// This is arguably a bug in the JS implementation too.
|
|
72
|
+
// - HTTP 401, 403, 408, 429, 500, 502, 503, 504, etc.: retried
|
|
73
|
+
// - HTTP 550: out of sync — stop retrying, throw error. The
|
|
74
|
+
// client must be torn down and restarted from scratch.
|
|
90
75
|
//
|
|
91
76
|
// --- Local Edit Absorption ---
|
|
92
77
|
//
|
|
@@ -107,8 +92,6 @@ function simpleton_client(url, {
|
|
|
107
92
|
on_state,
|
|
108
93
|
get_patches,
|
|
109
94
|
get_state,
|
|
110
|
-
apply_remote_update, // DEPRECATED
|
|
111
|
-
generate_local_diff_update, // DEPRECATED
|
|
112
95
|
content_type,
|
|
113
96
|
|
|
114
97
|
on_error,
|
|
@@ -127,10 +110,6 @@ function simpleton_client(url, {
|
|
|
127
110
|
var throttled_update = null
|
|
128
111
|
var ac = new AbortController()
|
|
129
112
|
|
|
130
|
-
// temporary: our old code uses this deprecated api,
|
|
131
|
-
// and our old code wants to send digests..
|
|
132
|
-
if (apply_remote_update) send_digests = true
|
|
133
|
-
|
|
134
113
|
// ── Subscription (GET) ──────────────────────────────────────────────
|
|
135
114
|
//
|
|
136
115
|
// Opens a long-lived GET subscription with retry: () => true, meaning
|
|
@@ -211,28 +190,23 @@ function simpleton_client(url, {
|
|
|
211
190
|
}
|
|
212
191
|
|
|
213
192
|
// ── Apply the update ────────────────────────────────
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
193
|
+
// Convert initial snapshot body to a patch replacing
|
|
194
|
+
// [0,0] — so initial load follows the same code path
|
|
195
|
+
// as incremental patches.
|
|
196
|
+
var patches = update.patches ||
|
|
197
|
+
[{range: [0, 0], content: update.state}]
|
|
198
|
+
if (on_patches) {
|
|
199
|
+
// EXTERNAL MODE: Apply patches to the UI, then
|
|
200
|
+
// read back the full state. Note: this absorbs
|
|
201
|
+
// any un-flushed local edits into client_state.
|
|
202
|
+
on_patches(patches)
|
|
203
|
+
client_state = get_state()
|
|
217
204
|
} else {
|
|
218
|
-
//
|
|
219
|
-
//
|
|
220
|
-
//
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (on_patches) {
|
|
224
|
-
// EXTERNAL MODE: Apply patches to the UI, then
|
|
225
|
-
// read back the full state. Note: this absorbs
|
|
226
|
-
// any un-flushed local edits into client_state.
|
|
227
|
-
on_patches(patches)
|
|
228
|
-
client_state = get_state()
|
|
229
|
-
} else {
|
|
230
|
-
// INTERNAL MODE: Apply patches to our internal
|
|
231
|
-
// state only. Local edits in the UI are NOT
|
|
232
|
-
// absorbed — they will be captured by the next
|
|
233
|
-
// changed() diff.
|
|
234
|
-
client_state = apply_patches(client_state, patches)
|
|
235
|
-
}
|
|
205
|
+
// INTERNAL MODE: Apply patches to our internal
|
|
206
|
+
// state only. Local edits in the UI are NOT
|
|
207
|
+
// absorbed — they will be captured by the next
|
|
208
|
+
// changed() diff.
|
|
209
|
+
client_state = apply_patches(client_state, patches)
|
|
236
210
|
}
|
|
237
211
|
|
|
238
212
|
// ── Digest verification ─────────────────────────────
|
|
@@ -278,18 +252,11 @@ function simpleton_client(url, {
|
|
|
278
252
|
// during a PUT round-trip are eventually sent.
|
|
279
253
|
changed: () => {
|
|
280
254
|
function get_change() {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
} else {
|
|
287
|
-
var new_state = get_state()
|
|
288
|
-
if (new_state === client_state) return null
|
|
289
|
-
var patches = get_patches ? get_patches(client_state) :
|
|
290
|
-
[simple_diff(client_state, new_state)]
|
|
291
|
-
return {patches, new_state}
|
|
292
|
-
}
|
|
255
|
+
var new_state = get_state()
|
|
256
|
+
if (new_state === client_state) return null
|
|
257
|
+
var patches = get_patches ? get_patches(client_state) :
|
|
258
|
+
[simple_diff(client_state, new_state)]
|
|
259
|
+
return {patches, new_state}
|
|
293
260
|
}
|
|
294
261
|
|
|
295
262
|
var change = get_change()
|
|
@@ -357,9 +324,9 @@ function simpleton_client(url, {
|
|
|
357
324
|
// Uses braid_fetch with retry: (res) => res.status !== 550
|
|
358
325
|
// This means:
|
|
359
326
|
// - Network failures: retried with backoff
|
|
360
|
-
// - HTTP 408, 429, 500, 502, 503, 504: retried
|
|
361
|
-
// - HTTP 550 (
|
|
362
|
-
//
|
|
327
|
+
// - HTTP 401, 403, 408, 429, 500, 502, 503, 504: retried
|
|
328
|
+
// - HTTP 550 (Repr-Digest mismatch / out of sync):
|
|
329
|
+
// give up, throw — client must be re-created
|
|
363
330
|
outstanding_changes++
|
|
364
331
|
try {
|
|
365
332
|
var r = await braid_fetch(url, {
|
|
@@ -373,13 +340,11 @@ function simpleton_client(url, {
|
|
|
373
340
|
version, parents, patches,
|
|
374
341
|
peer
|
|
375
342
|
})
|
|
376
|
-
if (!r.ok) throw new Error(`bad http status: ${r.status}
|
|
343
|
+
if (!r.ok) throw new Error(`bad http status: ${r.status}`)
|
|
377
344
|
} catch (e) {
|
|
378
|
-
//
|
|
379
|
-
//
|
|
380
|
-
//
|
|
381
|
-
// eventually hit max_outstanding_changes and stop
|
|
382
|
-
// the client from sending any more edits.
|
|
345
|
+
// A 550 means Repr-Digest check failed — we're out
|
|
346
|
+
// of sync. The client must be torn down and
|
|
347
|
+
// re-created from scratch.
|
|
383
348
|
on_error(e)
|
|
384
349
|
throw e
|
|
385
350
|
}
|