braid-http 1.0.2 → 1.0.3
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 +41 -44
- package/package.json +1 -1
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
|
|
66
|
+
If you want automatic reconnections, this library add a `{retry: true}` option to `fetch()`.
|
|
67
67
|
|
|
68
68
|
```javascript
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
(update)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
|