adamc@692
|
1 // Lists
|
adamc@692
|
2
|
adamc@580
|
3 function cons(v, ls) {
|
adamc@690
|
4 return { next : ls, data : v };
|
adamc@690
|
5 }
|
adamc@690
|
6 function concat(ls1, ls2) {
|
adamc@690
|
7 return (ls1 ? cons(ls1.data, concat(ls1.next, ls2)) : ls2);
|
adamc@690
|
8 }
|
adamc@690
|
9 function member(x, ls) {
|
adamc@690
|
10 for (; ls; ls = ls.next)
|
adamc@690
|
11 if (ls.data == x)
|
adamc@690
|
12 return true;
|
adamc@690
|
13 return false;
|
adamc@690
|
14 }
|
adamc@690
|
15 function remove(x, ls) {
|
adamc@690
|
16 return (ls ? (ls.data == x ? ls.next : cons(ls.data, remove(x, ls.next))) : null);
|
adamc@690
|
17 }
|
adamc@690
|
18 function union(ls1, ls2) {
|
adamc@690
|
19 return (ls1 ? (member(ls1.data, ls2) ? union(ls1.next, ls2) : cons(ls1.data, union(ls1.next, ls2))) : ls2);
|
adamc@580
|
20 }
|
adamc@703
|
21 function length(ls) {
|
adamc@703
|
22 return (ls ? 1 + length(ls.next) : 0);
|
adamc@703
|
23 }
|
adamc@670
|
24
|
adamc@690
|
25
|
adamc@729
|
26 // Error handling
|
adamc@729
|
27
|
adamc@729
|
28 function whine(msg) {
|
adamc@729
|
29 alert(msg);
|
adamc@729
|
30 throw msg;
|
adamc@729
|
31 }
|
adamc@729
|
32
|
adamc@810
|
33 function pf(loc) {
|
adamc@835
|
34 throw ("Pattern match failure (" + loc + ")");
|
adamc@729
|
35 }
|
adamc@729
|
36
|
adamc@798
|
37 function runHandlers(kind, ls, arg) {
|
adamc@798
|
38 if (ls == null)
|
adamc@798
|
39 alert(kind + ": " + arg);
|
adamc@729
|
40 for (; ls; ls = ls.next)
|
adamc@729
|
41 try {
|
adamc@729
|
42 ls.data(arg)(null);
|
adamc@729
|
43 } catch (v) { }
|
adamc@729
|
44 }
|
adamc@729
|
45
|
adamc@729
|
46 var errorHandlers = null;
|
adamc@729
|
47
|
adamc@729
|
48 function onError(f) {
|
adamc@729
|
49 errorHandlers = cons(f, errorHandlers);
|
adamc@729
|
50 }
|
adamc@729
|
51
|
adamc@729
|
52 function er(s) {
|
adamc@798
|
53 runHandlers("Error", errorHandlers, s);
|
adamc@729
|
54 throw {uw_error: s};
|
adamc@729
|
55 }
|
adamc@729
|
56
|
adamc@729
|
57 var failHandlers = null;
|
adamc@729
|
58
|
adamc@729
|
59 function onFail(f) {
|
adamc@729
|
60 failHandlers = cons(f, failHandlers);
|
adamc@729
|
61 }
|
adamc@729
|
62
|
adamc@729
|
63 function doExn(v) {
|
adamc@729
|
64 if (v == null || v.uw_error == null) {
|
adamc@729
|
65 var s = (v == null ? "null" : v.toString());
|
adamc@798
|
66 runHandlers("Fail", failHandlers, s);
|
adamc@729
|
67 }
|
adamc@729
|
68 }
|
adamc@729
|
69
|
adamc@729
|
70 var disconnectHandlers = null;
|
adamc@729
|
71
|
adamc@729
|
72 function onDisconnect(f) {
|
adamc@729
|
73 disconnectHandlers = cons(function (_){return f}, disconnectHandlers);
|
adamc@729
|
74 }
|
adamc@729
|
75
|
adamc@729
|
76 function discon() {
|
adamc@798
|
77 runHandlers("Disconnect", disconnectHandlers, null);
|
adamc@729
|
78 }
|
adamc@729
|
79
|
adamc@729
|
80 var connectHandlers = null;
|
adamc@729
|
81
|
adamc@729
|
82 function onConnectFail(f) {
|
adamc@729
|
83 connectHandlers = cons(function (_){return f}, connectHandlers);
|
adamc@729
|
84 }
|
adamc@729
|
85
|
adamc@729
|
86 function conn() {
|
adamc@798
|
87 runHandlers("Connect", connectHandlers, null);
|
adamc@729
|
88 }
|
adamc@729
|
89
|
adamc@729
|
90 var serverHandlers = null;
|
adamc@729
|
91
|
adamc@729
|
92 function onServerError(f) {
|
adamc@729
|
93 serverHandlers = cons(f, serverHandlers);
|
adamc@729
|
94 }
|
adamc@729
|
95
|
adamc@729
|
96 function servErr(s) {
|
adamc@824
|
97 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
|
adamc@729
|
98 }
|
adamc@729
|
99
|
adamc@729
|
100
|
adamc@692
|
101 // Embedding closures in XML strings
|
adamc@692
|
102
|
adamc@693
|
103 function cs(f) {
|
adamc@693
|
104 return {closure: f};
|
adamc@693
|
105 }
|
adamc@693
|
106
|
adamc@693
|
107 function isWeird(v) {
|
adamc@693
|
108 return v.closure != null || v.cat1 != null;
|
adamc@693
|
109 }
|
adamc@693
|
110
|
adamc@692
|
111 function cat(s1, s2) {
|
adamc@693
|
112 if (isWeird(s1) || isWeird(s2))
|
adamc@693
|
113 return {cat1: s1, cat2: s2};
|
adamc@693
|
114 else
|
adamc@692
|
115 return s1 + s2;
|
adamc@692
|
116 }
|
adamc@692
|
117
|
adamc@692
|
118 var closures = [];
|
adamc@703
|
119 var freeClosures = null;
|
adamc@692
|
120
|
adamc@692
|
121 function newClosure(f) {
|
adamc@703
|
122 var n;
|
adamc@703
|
123 if (freeClosures == null) {
|
adamc@703
|
124 n = closures.length;
|
adamc@703
|
125 } else {
|
adamc@703
|
126 n = freeClosures.data;
|
adamc@703
|
127 freeClosures = freeClosures.next;
|
adamc@703
|
128 }
|
adamc@692
|
129 closures[n] = f;
|
adamc@692
|
130 return n;
|
adamc@692
|
131 }
|
adamc@692
|
132
|
adamc@703
|
133 function freeClosure(n) {
|
adamc@703
|
134 closures[n] = null;
|
adamc@703
|
135 freeClosures = cons(n, freeClosures);
|
adamc@703
|
136 }
|
adamc@703
|
137
|
adamc@692
|
138 function cr(n) {
|
adamc@692
|
139 return closures[n]();
|
adamc@692
|
140 }
|
adamc@692
|
141
|
adamc@703
|
142 function flatten(cls, tr) {
|
adamc@693
|
143 if (tr.cat1 != null)
|
adamc@703
|
144 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
|
adamc@703
|
145 else if (tr.closure != null) {
|
adamc@703
|
146 var cl = newClosure(tr.closure);
|
adamc@703
|
147 cls.v = cons(cl, cls.v);
|
adamc@703
|
148 return "cr(" + cl + ")";
|
adamc@703
|
149 } else
|
adamc@692
|
150 return tr;
|
adamc@692
|
151 }
|
adamc@692
|
152
|
adamc@728
|
153 function flattenLocal(s) {
|
adamc@728
|
154 var cls = {v : null};
|
adamc@728
|
155 var r = flatten(cls, s);
|
adamc@728
|
156 for (cl = cls.v; cl != null; cl = cl.next)
|
adamc@728
|
157 freeClosure(cl.data);
|
adamc@728
|
158 return r;
|
adamc@728
|
159 }
|
adamc@728
|
160
|
adamc@692
|
161
|
adamc@692
|
162
|
adamc@692
|
163 // Dynamic tree management
|
adamc@692
|
164
|
adamc@690
|
165 function populate(node) {
|
adamc@690
|
166 var s = node.signal;
|
adamc@690
|
167 var oldSources = node.sources;
|
adamc@729
|
168 try {
|
adamc@729
|
169 var sr = s();
|
adamc@729
|
170 var newSources = sr.sources;
|
adamc@690
|
171
|
adamc@729
|
172 for (var sp = oldSources; sp; sp = sp.next)
|
adamc@729
|
173 if (!member(sp.data, newSources))
|
adamc@729
|
174 sp.data.dyns = remove(node, sp.data.dyns);
|
adamc@690
|
175
|
adamc@729
|
176 for (var sp = newSources; sp; sp = sp.next)
|
adamc@729
|
177 if (!member(sp.data, oldSources))
|
adamc@729
|
178 sp.data.dyns = cons(node, sp.data.dyns);
|
adamc@690
|
179
|
adamc@729
|
180 node.sources = newSources;
|
adamc@729
|
181 node.recreate(sr.data);
|
adamc@729
|
182 } catch (v) {
|
adamc@729
|
183 doExn(v);
|
adamc@729
|
184 }
|
adamc@579
|
185 }
|
adamc@574
|
186
|
adamc@580
|
187 function sc(v) {
|
adamc@690
|
188 return {data : v, dyns : null};
|
adamc@580
|
189 }
|
adamc@580
|
190 function sv(s, v) {
|
adamc@690
|
191 s.data = v;
|
adamc@690
|
192 for (var ls = s.dyns; ls; ls = ls.next)
|
adamc@690
|
193 if (!ls.dead)
|
adamc@690
|
194 populate(ls.data);
|
adamc@580
|
195 }
|
adamc@601
|
196 function sg(s) {
|
adamc@690
|
197 return s.data;
|
adamc@601
|
198 }
|
adamc@579
|
199
|
adamc@580
|
200 function ss(s) {
|
adamc@690
|
201 return function() { return {sources : cons(s, null), data : s.data } };
|
adamc@580
|
202 }
|
adamc@580
|
203 function sr(v) {
|
adamc@690
|
204 return function() { return {sources : null, data : v } };
|
adamc@580
|
205 }
|
adamc@580
|
206 function sb(x,y) {
|
adamc@690
|
207 return function() {
|
adamc@690
|
208 var xr = x();
|
adamc@690
|
209 var yr = y(xr.data)();
|
adamc@690
|
210 return {sources : union(xr.sources, yr.sources), data : yr.data};
|
adamc@690
|
211 };
|
adamc@580
|
212 }
|
adamc@571
|
213
|
adamc@604
|
214 function lastParent() {
|
adamc@604
|
215 var pos = document;
|
adamc@604
|
216
|
adamc@600
|
217 while (pos.lastChild && pos.lastChild.nodeType == 1)
|
adamc@600
|
218 pos = pos.lastChild;
|
adamc@600
|
219
|
adamc@600
|
220 return pos.parentNode;
|
adamc@600
|
221 }
|
adamc@600
|
222
|
adamc@604
|
223 function addNode(node) {
|
adamc@604
|
224 if (thisScript) {
|
adamc@604
|
225 thisScript.parentNode.appendChild(node);
|
adamc@604
|
226 thisScript.parentNode.removeChild(thisScript);
|
adamc@604
|
227 } else
|
adamc@604
|
228 lastParent().appendChild(node);
|
adamc@603
|
229 }
|
adamc@603
|
230
|
adamc@812
|
231 function setHTML(html) {
|
adamc@812
|
232 var x = document.createElement("span");
|
adamc@812
|
233 x.dead = false;
|
adamc@812
|
234 x.signal = null;
|
adamc@812
|
235 x.sources = null;
|
adamc@812
|
236 x.closures = null;
|
adamc@812
|
237 x.innerHTML = html;
|
adamc@812
|
238 addNode(x);
|
adamc@812
|
239 runScripts(x);
|
adamc@812
|
240 }
|
adamc@812
|
241
|
adamc@690
|
242 var thisScript = null;
|
adamc@690
|
243
|
adamc@604
|
244 function runScripts(node) {
|
adamc@604
|
245 var savedScript = thisScript;
|
adamc@603
|
246
|
adamc@692
|
247 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
|
adamc@604
|
248 var len = scripts.length;
|
adamc@646
|
249 for (var i = 0; i < len; ++i)
|
adamc@646
|
250 scriptsCopy[i] = scripts[i];
|
adamc@604
|
251 for (var i = 0; i < len; ++i) {
|
adamc@646
|
252 thisScript = scriptsCopy[i];
|
adamc@729
|
253 try {
|
adamc@729
|
254 eval(thisScript.textContent);
|
adamc@729
|
255 } catch (v) {
|
adamc@729
|
256 doExn(v);
|
adamc@729
|
257 }
|
adamc@604
|
258 }
|
adamc@604
|
259
|
adamc@604
|
260 thisScript = savedScript;
|
adamc@603
|
261 }
|
adamc@603
|
262
|
adamc@603
|
263
|
adamc@692
|
264 // Dynamic tree entry points
|
adamc@692
|
265
|
adamc@692
|
266 var dynDepth = 0;
|
adamc@692
|
267
|
adamc@571
|
268 function dyn(s) {
|
adamc@571
|
269 var x = document.createElement("span");
|
adamc@690
|
270 x.dead = false;
|
adamc@690
|
271 x.signal = s;
|
adamc@690
|
272 x.sources = null;
|
adamc@703
|
273 x.closures = null;
|
adamc@690
|
274 x.recreate = function(v) {
|
adamc@703
|
275 for (var ls = x.closures; ls; ls = ls.next)
|
adamc@703
|
276 freeClosure(ls.data);
|
adamc@692
|
277
|
adamc@690
|
278 var spans = x.getElementsByTagName("span");
|
adamc@690
|
279 for (var i = 0; i < spans.length; ++i) {
|
adamc@690
|
280 var span = spans[i];
|
adamc@690
|
281 span.dead = true;
|
adamc@690
|
282 for (var ls = span.sources; ls; ls = ls.next)
|
adamc@690
|
283 ls.data.dyns = remove(span, ls.data.dyns);
|
adamc@703
|
284 for (var ls = span.closures; ls; ls = ls.next)
|
adamc@703
|
285 freeClosure(ls.data);
|
adamc@690
|
286 }
|
adamc@690
|
287
|
adamc@703
|
288 var cls = {v : null};
|
adamc@703
|
289 x.innerHTML = flatten(cls, v);
|
adamc@703
|
290 x.closures = cls.v;
|
adamc@690
|
291 runScripts(x);
|
adamc@690
|
292 };
|
adamc@703
|
293 addNode(x);
|
adamc@690
|
294 populate(x);
|
adamc@571
|
295 }
|
adamc@582
|
296
|
adamc@817
|
297 function input(t, s, recreate) {
|
adamc@598
|
298 var x = document.createElement(t);
|
adamc@690
|
299 x.dead = false;
|
adamc@690
|
300 x.signal = ss(s);
|
adamc@690
|
301 x.sources = null;
|
adamc@817
|
302 x.recreate = recreate(x);
|
adamc@690
|
303 populate(x);
|
adamc@604
|
304 addNode(x);
|
adamc@816
|
305
|
adamc@816
|
306 return x;
|
adamc@816
|
307 }
|
adamc@816
|
308
|
adamc@816
|
309 function inp(s) {
|
adamc@817
|
310 var x = input("input", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
|
adamc@816
|
311 x.value = s.data;
|
adamc@816
|
312 x.onkeyup = function() { sv(s, x.value) };
|
adamc@816
|
313
|
adamc@816
|
314 return x;
|
adamc@816
|
315 }
|
adamc@816
|
316
|
adamc@816
|
317 function sel(s, content) {
|
adamc@817
|
318 var x = input("select", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
|
adamc@816
|
319 x.innerHTML = content;
|
adamc@816
|
320 x.value = s.data;
|
adamc@835
|
321 if (x.value != s.data)
|
adamc@835
|
322 sv(s, x.value);
|
adamc@816
|
323 x.onchange = function() { sv(s, x.value) };
|
adamc@797
|
324
|
adamc@835
|
325
|
adamc@606
|
326 return x;
|
adamc@598
|
327 }
|
adamc@598
|
328
|
adamc@817
|
329 function chk(s) {
|
adamc@817
|
330 var x = input("input", s, function(x) { return function(v) { if (x.checked != v) x.checked = v; }; });
|
adamc@817
|
331 x.type = "checkbox";
|
adamc@817
|
332 x.checked = s.data;
|
adamc@817
|
333 x.onchange = function() { sv(s, x.checked) };
|
adamc@817
|
334
|
adamc@817
|
335 return x;
|
adamc@817
|
336 }
|
adamc@817
|
337
|
adamc@800
|
338 function addOnChange(x, f) {
|
adamc@800
|
339 var old = x.onchange;
|
adamc@800
|
340 x.onchange = function() { old(); f (); };
|
adamc@800
|
341 }
|
adamc@800
|
342
|
adamc@692
|
343
|
adamc@692
|
344 // Basic string operations
|
adamc@692
|
345
|
adamc@597
|
346 function eh(x) {
|
adamc@800
|
347 if (x == null)
|
adamc@800
|
348 return "NULL";
|
adamc@800
|
349 else
|
adamc@800
|
350 return x.split("&").join("&").split("<").join("<").split(">").join(">");
|
adamc@597
|
351 }
|
adamc@597
|
352
|
adamc@582
|
353 function ts(x) { return x.toString() }
|
adamc@586
|
354 function bs(b) { return (b ? "True" : "False") }
|
adamc@586
|
355
|
adamc@828
|
356 function sub(s, i) { return s[i]; }
|
adamc@828
|
357 function suf(s, i) { return s.substring(i); }
|
adamc@828
|
358 function slen(s) { return s.length; }
|
adamc@829
|
359 function sidx(s, ch) {
|
adamc@829
|
360 var r = s.indexOf(ch);
|
adamc@829
|
361 if (r == -1)
|
adamc@829
|
362 return null;
|
adamc@829
|
363 else
|
adamc@829
|
364 return r;
|
adamc@829
|
365 }
|
adamc@831
|
366 function sspn(s, chs) {
|
adamc@831
|
367 for (var i = 0; i < s.length; ++i)
|
adamc@831
|
368 if (chs.indexOf(s[i]) != -1)
|
adamc@831
|
369 return i;
|
adamc@831
|
370
|
adamc@831
|
371 return null;
|
adamc@831
|
372 }
|
adamc@829
|
373 function schr(s, ch) {
|
adamc@829
|
374 var r = s.indexOf(ch);
|
adamc@829
|
375 if (r == -1)
|
adamc@829
|
376 return null;
|
adamc@829
|
377 else
|
adamc@829
|
378 return s.substring(r);
|
adamc@829
|
379 }
|
adamc@829
|
380 function ssub(s, start, len) {
|
adamc@829
|
381 return s.substring(start, start+len);
|
adamc@829
|
382 }
|
adamc@821
|
383
|
adamc@649
|
384 function pi(s) {
|
adamc@649
|
385 var r = parseInt(s);
|
adamc@649
|
386 if (r.toString() == s)
|
adamc@649
|
387 return r;
|
adamc@649
|
388 else
|
adamc@729
|
389 er("Can't parse int: " + s);
|
adamc@649
|
390 }
|
adamc@649
|
391
|
adamc@649
|
392 function pfl(s) {
|
adamc@649
|
393 var r = parseFloat(s);
|
adamc@649
|
394 if (r.toString() == s)
|
adamc@649
|
395 return r;
|
adamc@649
|
396 else
|
adamc@729
|
397 er("Can't parse float: " + s);
|
adamc@649
|
398 }
|
adamc@649
|
399
|
adamc@838
|
400 function pio(s) {
|
adamc@838
|
401 var r = parseInt(s);
|
adamc@838
|
402 if (r.toString() == s)
|
adamc@838
|
403 return r;
|
adamc@838
|
404 else
|
adamc@838
|
405 return null;
|
adamc@838
|
406 }
|
adamc@838
|
407
|
adamc@838
|
408 function pflo(s) {
|
adamc@838
|
409 var r = parseFloat(s);
|
adamc@838
|
410 if (r.toString() == s)
|
adamc@838
|
411 return r;
|
adamc@838
|
412 else
|
adamc@838
|
413 return null;
|
adamc@838
|
414 }
|
adamc@838
|
415
|
adamc@692
|
416 function uf(s) {
|
adamc@692
|
417 return escape(s).replace(new RegExp ("/", "g"), "%2F");
|
adamc@691
|
418 }
|
adamc@691
|
419
|
adamc@692
|
420 function uu(s) {
|
adamc@692
|
421 return unescape(s).replace(new RegExp ("\\+", "g"), " ");
|
adamc@692
|
422 }
|
adamc@692
|
423
|
adamc@692
|
424
|
adamc@603
|
425
|
adamc@692
|
426 // Remote calls
|
adamc@609
|
427
|
adamc@703
|
428 var client_id = null;
|
adamc@668
|
429 var client_pass = 0;
|
adamc@668
|
430 var url_prefix = "/";
|
adamc@673
|
431 var timeout = 60;
|
adamc@668
|
432
|
adamc@668
|
433 function getXHR(uri)
|
adamc@609
|
434 {
|
adamc@609
|
435 try {
|
adamc@609
|
436 return new XMLHttpRequest();
|
adamc@609
|
437 } catch (e) {
|
adamc@609
|
438 try {
|
adamc@609
|
439 return new ActiveXObject("Msxml2.XMLHTTP");
|
adamc@609
|
440 } catch (e) {
|
adamc@609
|
441 try {
|
adamc@609
|
442 return new ActiveXObject("Microsoft.XMLHTTP");
|
adamc@609
|
443 } catch (e) {
|
adamc@609
|
444 throw "Your browser doesn't seem to support AJAX.";
|
adamc@609
|
445 }
|
adamc@609
|
446 }
|
adamc@609
|
447 }
|
adamc@609
|
448 }
|
adamc@609
|
449
|
adamc@736
|
450 var sig = null;
|
adamc@736
|
451
|
adamc@736
|
452 function requestUri(xhr, uri, needsSig) {
|
adamc@668
|
453 xhr.open("GET", uri, true);
|
adamc@668
|
454
|
adamc@703
|
455 if (client_id != null) {
|
adamc@668
|
456 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
|
adamc@668
|
457 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
|
adamc@668
|
458 }
|
adamc@668
|
459
|
adamc@736
|
460 if (needsSig) {
|
adamc@736
|
461 if (sig == null)
|
adamc@736
|
462 whine("Missing cookie signature!");
|
adamc@736
|
463
|
adamc@736
|
464 xhr.setRequestHeader("UrWeb-Sig", sig);
|
adamc@736
|
465 }
|
adamc@736
|
466
|
adamc@668
|
467 xhr.send(null);
|
adamc@668
|
468 }
|
adamc@668
|
469
|
adamc@736
|
470 function rc(uri, parse, k, needsSig) {
|
adamc@728
|
471 uri = flattenLocal(uri);
|
adamc@609
|
472 var xhr = getXHR();
|
adamc@609
|
473
|
adamc@609
|
474 xhr.onreadystatechange = function() {
|
adamc@612
|
475 if (xhr.readyState == 4) {
|
adamc@612
|
476 var isok = false;
|
adamc@612
|
477
|
adamc@612
|
478 try {
|
adamc@612
|
479 if (xhr.status == 200)
|
adamc@612
|
480 isok = true;
|
adamc@612
|
481 } catch (e) { }
|
adamc@612
|
482
|
adamc@729
|
483 if (isok) {
|
adamc@729
|
484 try {
|
adamc@729
|
485 k(parse(xhr.responseText));
|
adamc@729
|
486 } catch (v) {
|
adamc@729
|
487 doExn(v);
|
adamc@729
|
488 }
|
adamc@729
|
489 } else {
|
adamc@729
|
490 conn();
|
adamc@649
|
491 }
|
adamc@612
|
492 }
|
adamc@609
|
493 };
|
adamc@609
|
494
|
adamc@736
|
495 requestUri(xhr, uri, needsSig);
|
adamc@609
|
496 }
|
adamc@667
|
497
|
adamc@667
|
498 function path_join(s1, s2) {
|
adamc@667
|
499 if (s1.length > 0 && s1[s1.length-1] == '/')
|
adamc@667
|
500 return s1 + s2;
|
adamc@667
|
501 else
|
adamc@667
|
502 return s1 + "/" + s2;
|
adamc@667
|
503 }
|
adamc@667
|
504
|
adamc@670
|
505 var channels = [];
|
adamc@670
|
506
|
adamc@670
|
507 function newQueue() {
|
adamc@670
|
508 return { front : null, back : null };
|
adamc@670
|
509 }
|
adamc@670
|
510 function enqueue(q, v) {
|
adamc@670
|
511 if (q.front == null) {
|
adamc@670
|
512 q.front = cons(v, null);
|
adamc@670
|
513 q.back = q.front;
|
adamc@670
|
514 } else {
|
adamc@670
|
515 var node = cons(v, null);
|
adamc@690
|
516 q.back.next = node;
|
adamc@670
|
517 q.back = node;
|
adamc@670
|
518 }
|
adamc@670
|
519 }
|
adamc@670
|
520 function dequeue(q) {
|
adamc@670
|
521 if (q.front == null)
|
adamc@670
|
522 return null;
|
adamc@670
|
523 else {
|
adamc@690
|
524 var r = q.front.data;
|
adamc@690
|
525 q.front = q.front.next;
|
adamc@670
|
526 if (q.front == null)
|
adamc@670
|
527 q.back = null;
|
adamc@670
|
528 return r;
|
adamc@670
|
529 }
|
adamc@670
|
530 }
|
adamc@670
|
531
|
adamc@670
|
532 function newChannel() {
|
adamc@670
|
533 return { msgs : newQueue(), listeners : newQueue() };
|
adamc@670
|
534 }
|
adamc@670
|
535
|
adamc@667
|
536 function listener() {
|
adamc@668
|
537 var uri = path_join(url_prefix, ".msgs");
|
adamc@667
|
538 var xhr = getXHR();
|
adamc@673
|
539 var tid, orsc, onTimeout;
|
adamc@673
|
540
|
adamc@673
|
541 var connect = function () {
|
adamc@673
|
542 xhr.onreadystatechange = orsc;
|
adamc@673
|
543 tid = window.setTimeout(onTimeout, timeout * 500);
|
adamc@736
|
544 requestUri(xhr, uri, false);
|
adamc@673
|
545 }
|
adamc@673
|
546
|
adamc@673
|
547 orsc = function() {
|
adamc@667
|
548 if (xhr.readyState == 4) {
|
adamc@673
|
549 window.clearTimeout(tid);
|
adamc@673
|
550
|
adamc@667
|
551 var isok = false;
|
adamc@667
|
552
|
adamc@667
|
553 try {
|
adamc@667
|
554 if (xhr.status == 200)
|
adamc@667
|
555 isok = true;
|
adamc@667
|
556 } catch (e) { }
|
adamc@667
|
557
|
adamc@668
|
558 if (isok) {
|
adamc@669
|
559 var lines = xhr.responseText.split("\n");
|
adamc@729
|
560 if (lines.length < 2) {
|
adamc@729
|
561 discon();
|
adamc@729
|
562 return;
|
adamc@729
|
563 }
|
adamc@669
|
564
|
adamc@669
|
565 for (var i = 0; i+1 < lines.length; i += 2) {
|
adamc@670
|
566 var chn = lines[i];
|
adamc@670
|
567 var msg = lines[i+1];
|
adamc@670
|
568
|
adamc@670
|
569 if (chn < 0)
|
adamc@670
|
570 whine("Out-of-bounds channel in message from remote server");
|
adamc@670
|
571
|
adamc@670
|
572 var ch;
|
adamc@670
|
573
|
adamc@670
|
574 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
575 ch = newChannel();
|
adamc@670
|
576 channels[chn] = ch;
|
adamc@670
|
577 } else
|
adamc@670
|
578 ch = channels[chn];
|
adamc@670
|
579
|
adamc@670
|
580 var listener = dequeue(ch.listeners);
|
adamc@670
|
581 if (listener == null) {
|
adamc@670
|
582 enqueue(ch.msgs, msg);
|
adamc@670
|
583 } else {
|
adamc@728
|
584 try {
|
adamc@728
|
585 listener(msg);
|
adamc@728
|
586 } catch (v) {
|
adamc@728
|
587 doExn(v);
|
adamc@728
|
588 }
|
adamc@670
|
589 }
|
adamc@669
|
590 }
|
adamc@669
|
591
|
adamc@673
|
592 connect();
|
adamc@668
|
593 }
|
adamc@667
|
594 else {
|
adamc@729
|
595 try {
|
adamc@824
|
596 if (xhr.status != 0)
|
adamc@824
|
597 servErr("Error querying remote server for messages: " + xhr.status);
|
adamc@736
|
598 } catch (e) { servErr("Error querying remote server for messages"); }
|
adamc@667
|
599 }
|
adamc@667
|
600 }
|
adamc@667
|
601 };
|
adamc@667
|
602
|
adamc@673
|
603 onTimeout = function() {
|
adamc@673
|
604 xhr.abort();
|
adamc@673
|
605 connect();
|
adamc@673
|
606 };
|
adamc@673
|
607
|
adamc@673
|
608 connect();
|
adamc@667
|
609 }
|
adamc@670
|
610
|
adamc@670
|
611 function rv(chn, parse, k) {
|
adamc@682
|
612 if (chn == null)
|
adamc@682
|
613 return;
|
adamc@682
|
614
|
adamc@670
|
615 if (chn < 0)
|
adamc@670
|
616 whine("Out-of-bounds channel receive");
|
adamc@670
|
617
|
adamc@670
|
618 var ch;
|
adamc@670
|
619
|
adamc@670
|
620 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
621 ch = newChannel();
|
adamc@670
|
622 channels[chn] = ch;
|
adamc@670
|
623 } else
|
adamc@670
|
624 ch = channels[chn];
|
adamc@670
|
625
|
adamc@670
|
626 var msg = dequeue(ch.msgs);
|
adamc@670
|
627 if (msg == null) {
|
adamc@670
|
628 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
|
adamc@670
|
629 } else {
|
adamc@728
|
630 try {
|
adamc@728
|
631 k(parse(msg))(null);
|
adamc@728
|
632 } catch (v) {
|
adamc@728
|
633 doExn(v);
|
adamc@728
|
634 }
|
adamc@670
|
635 }
|
adamc@670
|
636 }
|
adamc@693
|
637
|
adamc@693
|
638
|
adamc@693
|
639 // App-specific code
|
adamc@693
|
640
|