1
- < script src ="braidify -client.js "> </ script >
1
+ < script src ="braid-http -client.js "> </ script >
2
2
< script type ="module ">
3
3
// Imports
4
4
import { h , Component , render } from 'https://unpkg.com/preact?module'
@@ -32,8 +32,6 @@ <h1>It's a chat!</h1>
32
32
}
33
33
}
34
34
35
- var path = '/chat'
36
-
37
35
// State
38
36
var chat = [ ]
39
37
var curr_version = { }
@@ -43,77 +41,82 @@ <h1>It's a chat!</h1>
43
41
render_root ( )
44
42
45
43
var send_message = async ( ) => {
44
+ // Update the text input
46
45
var input = document . getElementById ( 'new_stuff' ) ,
47
46
post = { text : input . value }
48
47
input . value = ''
49
48
49
+ // Update local state
50
+ chat . push ( post )
51
+ curr_version [ '/chat' ] = [ ( parseInt ( curr_version [ '/chat' ] [ 0 ] ) + 1 ) + '' ]
52
+
53
+ // Re-render UI
54
+ render_root ( )
55
+
56
+ // Send patch over the network
50
57
var patches = [ { unit : 'json' , range : '[-0:-0]' , content : JSON . stringify ( post ) } ]
51
- var url = new URL ( path , window . location . href )
52
- var res = await fetch ( url , { method : 'put' , patches} )
58
+ var res = await braid_fetch ( url , { method : 'put' , patches, peer} )
53
59
if ( res . status === 200 )
54
60
console . debug ( 'put complete' )
55
61
else
56
62
console . debug ( 'put failed with' , res . status )
57
-
58
- chat . push ( post )
59
- curr_version [ '/chat' ] ++
60
- render_root ( )
61
63
}
62
64
63
- // Updates
64
- function connect ( ) {
65
- fetch (
66
- new URL ( path , window . location . href ) ,
67
- { subscribe : true } ,
68
- ) . andThen ( response => {
69
- console . log ( 'Connected!' , response )
70
-
71
- curr_version [ path ] = response . version
65
+ // Networking
66
+ var path = '/chat' ,
67
+ url = new URL ( path , window . location . href ) ,
68
+ peer = Math . random ( ) . toString ( 36 ) . substr ( 2 )
72
69
73
- // When we receive updates, they might come in the form of patches:
74
- if ( response . patches )
75
- chat = apply_patches ( response . patches , chat )
76
-
77
- // Or a complete version:
78
- else
79
- // Beware the server doesn't send these yet.
80
- chat = JSON . parse ( response . body )
81
-
82
- render_root ( )
83
- } ) . catch ( e => {
84
- console . log ( 'Reconnecting...' )
85
- setTimeout ( connect , 4000 )
86
- } )
87
- }
88
- connect2 ( )
89
-
90
-
91
- async function connect2 ( ) {
92
- try {
93
- for await ( var response of fetch ( new URL ( path , window . location . href ) ,
94
- { subscribe : true } ) ) {
95
-
96
- console . log ( 'Connected!' , response )
70
+ function connect ( ) {
71
+ braid_fetch ( url , { subscribe : true , peer} ) . then (
72
+ response => response . subscribe (
73
+ update => {
74
+ console . log ( 'Got update!' , update )
97
75
98
- curr_version [ path ] = response . version
76
+ curr_version [ path ] = update . version
99
77
100
78
// When we receive updates, they might come in the form of patches:
101
- if ( response . patches )
102
- chat = apply_patches ( response . patches , chat )
79
+ if ( update . patches )
80
+ chat = apply_patches ( update . patches , chat )
103
81
104
82
// Or a complete version:
105
83
else
106
84
// Beware the server doesn't send these yet.
107
- chat = JSON . parse ( response . body )
85
+ chat = JSON . parse ( update . body )
108
86
109
87
render_root ( )
110
- }
111
- } catch ( e ) {
112
- console . log ( 'Reconnecting...' )
113
- setTimeout ( connect2 , 4000 )
114
- }
88
+ } ,
89
+ e => setTimeout ( connect , 2000 )
90
+ )
91
+ ) . catch ( e => setTimeout ( connect , 2000 ) )
115
92
}
116
93
94
+ connect ( )
95
+
96
+ // // The for await version is not currently used
97
+ // async function connect2 () {
98
+ // try {
99
+ // for await (var update of
100
+ // braid_fetch(url, {subscribe: true}, peer).subscription) {
101
+ // curr_version[path] = update.version
102
+
103
+ // // When we receive updates, they might come in the form of patches:
104
+ // if (update.patches)
105
+ // chat = apply_patches(update.patches, chat)
106
+
107
+ // // Or a complete version:
108
+ // else
109
+ // // Beware the server doesn't send these yet.
110
+ // chat = JSON.parse(update.body)
111
+
112
+ // render_root()
113
+ // }
114
+ // } catch (e) {
115
+ // console.log('Reconnecting...')
116
+ // setTimeout(connect2, 2000)
117
+ // }
118
+ // }
119
+
117
120
function apply_patches ( patches , object ) {
118
121
for ( var patch of patches )
119
122
// There are only two types of patches we could receive
0 commit comments