braid-text 0.0.31 → 0.1.1

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/editor.html CHANGED
@@ -5,8 +5,8 @@
5
5
  ></textarea>
6
6
  </body>
7
7
  <script src="https://braid.org/code/myers-diff1.js"></script>
8
- <script src="https://unpkg.com/braid-http@~0.3/braid-http-client.js"></script>
9
- <script src="https://unpkg.com/braid-text/simpleton-client.js"></script>
8
+ <script src="https://unpkg.com/braid-http@~1.0/braid-http-client.js"></script>
9
+ <script src="/simpleton-client.js"></script>
10
10
  <script>
11
11
  let simpleton = simpleton_client(location.pathname, {
12
12
  apply_remote_update: ({ state, patches }) => {
@@ -19,6 +19,11 @@
19
19
  if (patches.length === 0) return null;
20
20
  return { patches, new_state: texty.value };
21
21
  },
22
+ on_error: (e) => {
23
+ texty.disabled = true
24
+ texty.style.background = '#fee'
25
+ texty.style.border = '4px solid red'
26
+ }
22
27
  });
23
28
 
24
29
  texty.value = "";
package/index.js CHANGED
@@ -159,6 +159,8 @@ braid_text.serve = async (req, res, options = {}) => {
159
159
  }
160
160
 
161
161
  await braid_text.put(resource, { peer, version: req.version, parents: req.parents, patches, body, merge_type })
162
+
163
+ res.setHeader("Version", resource.doc.getRemoteVersion().map((x) => x.join("-")).sort())
162
164
 
163
165
  options.put_cb(options.key, resource.val)
164
166
  } catch (e) {
@@ -12,11 +12,11 @@ dom.BODY = -> DIV(WIKI())
12
12
  window.statebus_fetch = window.fetch
13
13
  window.fetch = window.og_fetch
14
14
  </script>
15
- <script src="https://unpkg.com/braid-http@~0.3/braid-http-client.js"></script>
15
+ <script src="https://unpkg.com/braid-http@~1.0/braid-http-client.js"></script>
16
16
  <script>
17
17
  window.fetch = window.statebus_fetch
18
18
  </script>
19
- <script src="https://unpkg.com/braid-text/simpleton-client.js"></script>
19
+ <script src="/simpleton-client.js"></script>
20
20
 
21
21
  <script>
22
22
 
@@ -47,6 +47,11 @@ var simpleton = simpleton_client(location.pathname, {
47
47
  patches: patches,
48
48
  new_state: t().value
49
49
  };
50
+ },
51
+ on_error: (e) => {
52
+ t().disabled = true
53
+ t().style.background = '#fee'
54
+ t().style.border = '4px solid red'
50
55
  }
51
56
  });
52
57
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.0.31",
3
+ "version": "0.1.1",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidjs",
package/server-demo.js CHANGED
@@ -28,6 +28,12 @@ var server = require("http").createServer(async (req, res) => {
28
28
  return
29
29
  }
30
30
 
31
+ if (req.url == '/simpleton-client.js') {
32
+ res.writeHead(200, { "Content-Type": "text/javascript", "Cache-Control": "no-cache" })
33
+ require("fs").createReadStream("./simpleton-client.js").pipe(res)
34
+ return
35
+ }
36
+
31
37
  // TODO: uncomment out the code below to add /pages endpoint,
32
38
  // which displays all the currently used keys
33
39
  //
@@ -1,4 +1,4 @@
1
- // requires braid-http@0.3.14
1
+ // requires braid-http@~1.0/braid-http-client.js
2
2
  //
3
3
  // url: resource endpoint
4
4
  //
@@ -17,7 +17,7 @@
17
17
  // this is for outgoing changes;
18
18
  // diff_function = () => ({patches, new_version}).
19
19
  //
20
- function simpleton_client(url, { apply_remote_update, generate_local_diff_update, content_type }) {
20
+ function simpleton_client(url, { apply_remote_update, generate_local_diff_update, content_type, on_error }) {
21
21
  var peer = Math.random().toString(36).substr(2)
22
22
  var current_version = []
23
23
  var prev_state = ""
@@ -26,7 +26,7 @@ function simpleton_client(url, { apply_remote_update, generate_local_diff_update
26
26
  var max_outstanding_changes = 10
27
27
  var ac = new AbortController()
28
28
 
29
- braid_fetch_wrapper(url, {
29
+ braid_fetch(url, {
30
30
  headers: { "Merge-Type": "simpleton",
31
31
  ...(content_type ? {Accept: content_type} : {}) },
32
32
  subscribe: true,
@@ -67,8 +67,8 @@ function simpleton_client(url, { apply_remote_update, generate_local_diff_update
67
67
 
68
68
  prev_state = apply_remote_update(update)
69
69
  }
70
- })
71
- )
70
+ }, on_error)
71
+ ).catch(on_error)
72
72
 
73
73
  return {
74
74
  stop: async () => {
@@ -111,14 +111,19 @@ function simpleton_client(url, { apply_remote_update, generate_local_diff_update
111
111
  prev_state = new_state
112
112
 
113
113
  outstanding_changes++
114
- await braid_fetch_wrapper(url, {
115
- headers: { "Merge-Type": "simpleton",
116
- ...(content_type ? {"Content-Type": content_type} : {}) },
117
- method: "PUT",
118
- retry: true,
119
- version, parents, patches,
120
- peer
121
- })
114
+ try {
115
+ await braid_fetch(url, {
116
+ headers: { "Merge-Type": "simpleton",
117
+ ...(content_type ? {"Content-Type": content_type} : {}) },
118
+ method: "PUT",
119
+ retry: true,
120
+ version, parents, patches,
121
+ peer
122
+ })
123
+ } catch (e) {
124
+ on_error(e)
125
+ throw e
126
+ }
122
127
  outstanding_changes--
123
128
  }
124
129
  }
@@ -138,42 +143,3 @@ function count_code_points(str) {
138
143
  }
139
144
  return code_points
140
145
  }
141
-
142
- async function braid_fetch_wrapper(url, params) {
143
- if (!params.retry) throw "wtf"
144
- var waitTime = 10
145
- if (params.subscribe) {
146
- var subscribe_handler = null
147
- connect()
148
- async function connect() {
149
- if (params.signal?.aborted) return
150
- try {
151
- var c = await braid_fetch(url, { ...params, parents: params.parents?.() })
152
- c.subscribe((...args) => subscribe_handler?.(...args), on_error)
153
- waitTime = 10
154
- } catch (e) {
155
- on_error(e)
156
- }
157
- }
158
- function on_error(e) {
159
- console.log('eee = ' + e.stack)
160
- setTimeout(connect, waitTime)
161
- waitTime = Math.min(waitTime * 2, 3000)
162
- }
163
- return {subscribe: handler => { subscribe_handler = handler }}
164
- } else {
165
- return new Promise((done) => {
166
- send()
167
- async function send() {
168
- try {
169
- var res = await braid_fetch(url, params)
170
- if (res.status !== 200) throw "status not 200: " + res.status
171
- done(res)
172
- } catch (e) {
173
- setTimeout(send, waitTime)
174
- waitTime = Math.min(waitTime * 2, 3000)
175
- }
176
- }
177
- })
178
- }
179
- }