braid-http 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -63,62 +63,59 @@ fetch('https://braid.org/chat', {subscribe: true}).then(
63
63
  )
64
64
  ```
65
65
 
66
- If you want automatic reconnections, add two error handlers like this:
66
+ If you want automatic reconnections, this library add a `{retry: true}` option to `fetch()`.
67
67
 
68
68
  ```javascript
69
- function connect() {
70
- fetch('https://braid.org/chat', {subscribe: true}).then(
71
- res => res.subscribe(
72
- (update) => {
73
- console.log('We got a new update!', update)
74
- // Do something with the update
75
- },
76
- e => setTimeout(connect, 1000)
77
- )
78
- ).catch(e => setTimeout(connect, 1000))
79
- }
80
- connect()
69
+ fetch('https://braid.org/chat', {subscribe: true, retry: true}).then(
70
+ res => res.subscribe(
71
+ (update) => {
72
+ console.log('We got a new update!', update)
73
+ // Do something with the update
74
+ }
75
+ )
76
+ )
77
+ ```
78
+
79
+ For use in conjunction with `{retry: true}`, it's possible to make the `parents` param equal to a function, which will be called to get the current parents each time the fetch establishes a new connection.
80
+
81
+ ```javascript
82
+ fetch('https://braid.org/chat', {subscribe: true, retry: true, parents: () => {
83
+ return current_parents
84
+ }}).then(
85
+ res => res.subscribe(
86
+ (update) => {
87
+ console.log('We got a new update!', update)
88
+ // Do something with the update
89
+ }
90
+ )
91
+ )
81
92
  ```
82
93
 
83
94
  ### Example Subscription with Async/Await
84
95
 
85
96
  ```javascript
86
- async function connect () {
87
- try {
88
- (await fetch('/chat', {subscribe: true})).subscribe(
89
- (update) => {
90
- // We got a new update!
91
- },
92
- () => setTimeout(connect, 1000)
93
- )
94
- } catch (e) {
95
- setTimeout(connect, 1000)
96
- }
97
- }
97
+ (await fetch('/chat', {subscribe: true, retry: true})).subscribe(
98
+ (update) => {
99
+ // We got a new update!
100
+ })
98
101
  ```
99
102
 
100
103
  ### Example Subscription with `for await`
101
104
 
102
105
  ```javascript
103
- async function connect () {
104
- try {
105
- var subscription_iterator = fetch('/chat', {subscribe: true}).subscription
106
- for await (var update of subscription_iterator) {
107
- // Updates might come in the form of patches:
108
- if (update.patches)
109
- chat = apply_patches(update.patches, chat)
110
-
111
- // Or complete snapshots:
112
- else
113
- // Beware the server doesn't send these yet.
114
- chat = JSON.parse(update.body)
115
-
116
- render_stuff()
117
- }
118
- } catch (e) {
119
- console.log('Reconnecting...')
120
- setTimeout(connect, 4000)
121
- }
106
+ var subscription_iterator = (await fetch('/chat',
107
+ {subscribe: true, retry: true})).subscription
108
+ for await (var update of subscription_iterator) {
109
+ // Updates might come in the form of patches:
110
+ if (update.patches)
111
+ chat = apply_patches(update.patches, chat)
112
+
113
+ // Or complete snapshots:
114
+ else
115
+ // Beware the server doesn't send these yet.
116
+ chat = JSON.parse(update.body)
117
+
118
+ render_stuff()
122
119
  }
123
120
  ```
124
121
 
@@ -350,7 +350,11 @@ async function braid_fetch (url, params = {}) {
350
350
  case 504: // Gateway Timeout
351
351
  give_up = false;
352
352
  }
353
- if (give_up) return fail(new Error(`giving up because of http status: ${res.status}${(res.status === 401 || res.status === 403) ? ` (access denied)` : ''}`))
353
+ if (give_up) {
354
+ let e = new Error(`giving up because of http status: ${res.status}${(res.status === 401 || res.status === 403) ? ` (access denied)` : ''}`)
355
+ subscription_error?.(e)
356
+ return fail(e)
357
+ }
354
358
  if (!res.ok) throw new Error(`status not ok: ${res.status}`)
355
359
  }
356
360
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"