-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
312 lines (268 loc) · 11.6 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
(function () {
if (!React) {
throw new Error("React is not loaded");
}
if (!SockJS) {
throw new Error("SockJS is not loaded");
}
function sockjs_client(prefix, url) {
var bus = this
var recent_saves = []
var sock
var attempts = 0
var outbox = []
var fetched_keys = new bus.Set()
var heartbeat
url = url.replace(/^state:\/\//, 'https://')
url = url.replace(/^statei:\/\//, 'http://')
if (url[url.length-1]=='/') url = url.substr(0,url.length-1)
function send (o, pushpop) {
pushpop = pushpop || 'push'
bus.log('sockjs.send:', JSON.stringify(o))
outbox[pushpop](JSON.stringify(o))
flush_outbox()
}
function flush_outbox() {
if (sock.readyState === 1)
while (outbox.length > 0)
sock.send(outbox.shift())
else
setTimeout(flush_outbox, 400)
}
bus(prefix).to_save = function (obj) { send({method: 'save', obj: obj})
bus.save.fire(obj)
if (window.ignore_flashbacks)
recent_saves.push(JSON.stringify(obj))
if (recent_saves.length > 100) {
var extra = recent_saves.length - 100
recent_saves.splice(0, extra)
}
}
bus(prefix).to_fetch = function (key) { send({method: 'fetch', key: key}),
fetched_keys.add(key) }
bus(prefix).to_forget = function (key) { send({method: 'forget', key: key}),
fetched_keys.delete(key) }
bus(prefix).to_delete = function (key) { send({method: 'delete', key: key}) }
function connect () {
console.log('%c[ ] trying to open ' + url, 'color: blue')
sock = sock = new SockJS(url + '/statebus')
sock.onopen = function() {
console.log('%c[*] opened ' + url, 'color: blue')
var me = fetch('ls/me')
bus.log('connect: me is', me)
if (!me.client) {
me.client = (Math.random().toString(36).substring(2)
+ Math.random().toString(36).substring(2)
+ Math.random().toString(36).substring(2))
save(me)
}
send({method: 'save', obj: {key: '/current_user', client: me.client}},
'unshift')
if (attempts > 0) {
// Then we need to refetch everything, cause it
// might have changed
recent_saves = []
var keys = fetched_keys.values()
for (var i=0; i<keys.length; i++)
send({method: 'fetch', key: keys[i]})
}
attempts = 0
//heartbeat = setInterval(function () {send({method: 'ping'})}, 5000)
}
sock.onclose = function() {
console.log('%c[*] closed ' + url, 'color: blue')
heartbeat && clearInterval(heartbeat); heartbeat = null
setTimeout(connect, attempts++ < 3 ? 1500 : 5000)
// Remove all fetches and forgets from queue
var new_outbox = []
var bad = {'fetch':1, 'forget':1}
for (var i=0; i<outbox.length; i++)
if (!bad[JSON.parse(outbox[i]).method])
new_outbox.push(outbox[i])
outbox = new_outbox
}
sock.onmessage = function(event) {
// Todo: Perhaps optimize processing of many messages
// in batch by putting new messages into a queue, and
// waiting a little bit for more messages to show up
// before we try to re-render. That way we don't
// re-render 100 times for a function that depends on
// 100 items from server while they come in. This
// probably won't make things render any sooner, but
// will probably save energy.
//console.log('[.] message')
try {
var message = JSON.parse(event.data)
var method = message.method.toLowerCase()
// Convert v3 pubs to v4 saves for compatibility
if (method == 'pub') method = 'save'
// We only take saves from the server for now
if (method !== 'save' && method !== 'pong') throw 'barf'
bus.log('sockjs_client received', message.obj)
var is_recent_save = false
if (window.ignore_flashbacks) {
var s = JSON.stringify(message.obj)
for (var i=0; i<recent_saves.length; i++)
if (s === recent_saves[i]) {
is_recent_save = true
recent_saves.splice(i, 1)
}
// bus.log('Msg', message.obj.key,
// is_recent_save?'is':'is NOT', 'a flashback')
}
if (!is_recent_save)
bus.save.fire(message.obj)
//setTimeout(function () {bus.announce(message.obj)}, 1000)
} catch (err) {
console.error('Received bad sockjs message from '
+url+': ', event.data, err)
return
}
}
}
connect()
}
function localstorage_client (prefix) {
// This doesn't yet trigger updates across multiple browser windows.
// We can do that by adding a list of dirty keys and
var bus = this
bus.log(this)
// Fetch returns the value immediately in a save
// Saves are queued up, to store values with a delay, in batch
var saves_are_pending = false
var pending_saves = {}
function save_the_pending_saves() {
bus.log('localstore: saving', pending_saves)
for (var k in pending_saves)
localStorage.setItem(k, JSON.stringify(pending_saves[k]))
saves_are_pending = false
}
bus(prefix).to_fetch = function (key) {
var result = localStorage.getItem(key)
return result ? JSON.parse(result) : {key: key}
}
bus(prefix).to_save = function (obj) {
// Do I need to make this recurse into the object?
bus.log('localStore: on_save:', obj.key)
pending_saves[obj.key] = obj
if (!saves_are_pending) {
setTimeout(save_the_pending_saves, 50)
saves_are_pending = true
}
bus.save.fire(obj)
return obj
}
bus(prefix).to_delete = function (key) { localStorage.removeItem(key) }
// Hm... this update stuff doesn't seem to work on file:/// urls in chrome
function update (event) {
bus.log('Got a localstorage update', event)
//this.get(event.key.substr('statebus '.length))
}
if (window.addEventListener) window.addEventListener("storage", update, false)
else window.attachEvent("onstorage", update)
}
window.bus = window.statebus();
const cache = localStorage.getItem('*statebus_cache_invalid_key*');
if (cache) {
bus.paused = true;
bus.cache = JSON.parse(cache);
} else {
bus.paused = false;
}
bus.pause = () => {
if (!bus.paused) {
bus.paused = true;
localStorage.setItem('*statebus_cache_invalid_key*', JSON.stringify(bus.cache));
}
}
bus.resume = () => {
if (bus.paused) {
bus.paused = false;
localStorage.removeItem('*statebus_cache_invalid_key*');
}
}
sockjs_client.bind(bus)('/*', window.statebus_server);
localstorage_client.bind(bus)('ls/*');
/*
* Base class for statebus components who wish to bind statebus state
* to component state
*
* XXX Currently assumes there's a statebus named "bus" in global scope
*/
class StatebusComponent extends React.Component {
constructor(props) {
super(props);
this.updateable = false;
this.callbacks = new Map();
this.statebus = new Proxy({},
{
get: (target, key, receiver) => {
console.assert(!key.includes('*'));
this.fetches.add(key);
if (!this.callbacks.has(key)) {
this.callbacks.set(key, (obj) => {
// render will fetch from bus.cache
this.forceUpdate();
});
if (this.updateable) {
// Ensure this doesn't run within render
setTimeout(() => {
bus.fetch(key, this.callbacks.get(key));
}, 0);
}
}
return (bus.cache[key] || {key: key}).value;
},
set: (target, key, value, receiver) => {
console.assert(!key.includes('*'));
if (!bus.paused) {
console.log("SAVING");
bus.save({
key: key,
value: value,
});
}
return true;
}
});
this.previous_fetches = new Set();
this.fetches = new Set();
this._decorate('render', (original_render) => {
this.previous_fetches = this.fetches;
this.fetches = new Set();
const render_value = original_render();
// Unsubscribe from any keys that weren't fetched again
for (let key of this.previous_fetches) {
if (!this.fetches.has(key)) {
bus.forget(key, this.callbacks.get(key));
this.callbacks.delete(key);
}
}
return render_value;
});
this._decorate('componentWillMount', (original_cwm) => {
this.updateable = true;
// fetch keys that we previously couldn't
for (let [key, callback] of this.callbacks) {
bus.fetch(key, callback);
}
original_cwm();
});
this._decorate('componentWillUnmount', (original_cwu) => {
original_cwu();
for (let [key, callback] of this.callbacks) {
bus.forget(key, callback);
}
this.callbacks.clear();
})
}
_decorate(property, decorator) {
const old_func = this[property] || function(){};
this[property] = () => {
return decorator(old_func.bind(this));
}
}
}
// TODO: export
window.StatebusComponent = StatebusComponent;
})()