braid-text 0.2.79 → 0.2.81

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 CHANGED
@@ -197,6 +197,7 @@ function create_braid_text() {
197
197
  disconnect()
198
198
  connect()
199
199
  }
200
+ options.on_res?.(remote_result)
200
201
  // on_error will call handle_error when connection drops
201
202
  } catch (e) {
202
203
  handle_error(e)
@@ -996,7 +997,7 @@ function create_braid_text() {
996
997
 
997
998
  await resource.db_delta(resource.doc.getPatchSince(v_before))
998
999
 
999
- for (var [client, x] of post_commit_updates) client.my_subscribe(x)
1000
+ await Promise.all(post_commit_updates.map(([client, x]) => client.my_subscribe(x)))
1000
1001
 
1001
1002
  return { change_count }
1002
1003
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.79",
3
+ "version": "0.2.81",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
package/test/tests.js CHANGED
@@ -1144,6 +1144,50 @@ runTest(
1144
1144
  'x'
1145
1145
  )
1146
1146
 
1147
+ runTest(
1148
+ "test put awaits subscriber callbacks",
1149
+ async () => {
1150
+ var key = 'test-' + Math.random().toString(36).slice(2)
1151
+
1152
+ var r = await braid_fetch(`/eval`, {
1153
+ method: 'PUT',
1154
+ body: `void (async () => {
1155
+ var order = []
1156
+
1157
+ // Subscribe with an async callback that takes some time
1158
+ braid_text.get('/${key}', {
1159
+ subscribe: async (update) => {
1160
+ if (update.version?.[0]?.startsWith('test-v')) {
1161
+ order.push('subscriber-start')
1162
+ await new Promise(done => setTimeout(done, 50))
1163
+ order.push('subscriber-end')
1164
+ }
1165
+ }
1166
+ })
1167
+
1168
+ // Wait for subscription to be established
1169
+ await new Promise(done => setTimeout(done, 50))
1170
+
1171
+ // Put should await the subscriber callback
1172
+ await braid_text.put('/${key}', {
1173
+ version: ['test-v-0'],
1174
+ parents: [],
1175
+ body: 'hello'
1176
+ })
1177
+ order.push('put-done')
1178
+
1179
+ // If put properly awaited, order should be: subscriber-start, subscriber-end, put-done
1180
+ // If put didn't await, order would be: subscriber-start, put-done, subscriber-end
1181
+ res.end(order.join(','))
1182
+ })()`
1183
+ })
1184
+ if (!r.ok) return 'eval failed: ' + r.status
1185
+
1186
+ return await r.text()
1187
+ },
1188
+ 'subscriber-start,subscriber-end,put-done'
1189
+ )
1190
+
1147
1191
  runTest(
1148
1192
  "test out-of-order PUTs",
1149
1193
  async () => {
@@ -1981,6 +2025,47 @@ runTest(
1981
2025
  'created locally'
1982
2026
  )
1983
2027
 
2028
+ runTest(
2029
+ "test braid_text.sync on_res callback",
2030
+ async () => {
2031
+ var local_key = 'test-local-' + Math.random().toString(36).slice(2)
2032
+ var remote_key = 'test-remote-' + Math.random().toString(36).slice(2)
2033
+
2034
+ // Create the remote resource first
2035
+ var r = await braid_fetch(`/${remote_key}`, {
2036
+ method: 'PUT',
2037
+ body: 'remote content'
2038
+ })
2039
+ if (!r.ok) return 'put failed: ' + r.status
2040
+
2041
+ // Start sync with on_res callback and verify it gets called
2042
+ var r = await braid_fetch(`/eval`, {
2043
+ method: 'PUT',
2044
+ body: `void (async () => {
2045
+ var ac = new AbortController()
2046
+ var got_res = false
2047
+
2048
+ braid_text.sync('/${local_key}', new URL('http://localhost:8889/${remote_key}'), {
2049
+ signal: ac.signal,
2050
+ on_res: (response) => {
2051
+ got_res = response && typeof response.headers !== 'undefined'
2052
+ }
2053
+ })
2054
+
2055
+ // Wait for sync to establish and on_res to be called
2056
+ await new Promise(done => setTimeout(done, 200))
2057
+
2058
+ ac.abort()
2059
+ res.end(got_res ? 'on_res called' : 'on_res not called')
2060
+ })()`
2061
+ })
2062
+ if (!r.ok) return 'eval failed: ' + r.status
2063
+
2064
+ return await r.text()
2065
+ },
2066
+ 'on_res called'
2067
+ )
2068
+
1984
2069
  runTest(
1985
2070
  "test getting a binary update from a subscription",
1986
2071
  async () => {