annotate lib/js/urweb.js @ 967:1b80b9df0d32

Plugged a JavaScript memory leak
author Adam Chlipala <adamc@hcoop.net>
date Sat, 19 Sep 2009 15:59:22 -0400
parents 8c37699de273
children 001edfbe2561
rev   line source
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@967 192
adamc@690 193 for (var ls = s.dyns; ls; ls = ls.next)
adamc@690 194 if (!ls.dead)
adamc@690 195 populate(ls.data);
adamc@580 196 }
adamc@601 197 function sg(s) {
adamc@690 198 return s.data;
adamc@601 199 }
adamc@579 200
adamc@580 201 function ss(s) {
adamc@690 202 return function() { return {sources : cons(s, null), data : s.data } };
adamc@580 203 }
adamc@580 204 function sr(v) {
adamc@690 205 return function() { return {sources : null, data : v } };
adamc@580 206 }
adamc@580 207 function sb(x,y) {
adamc@690 208 return function() {
adamc@690 209 var xr = x();
adamc@690 210 var yr = y(xr.data)();
adamc@690 211 return {sources : union(xr.sources, yr.sources), data : yr.data};
adamc@690 212 };
adamc@580 213 }
adamc@841 214 function scur(s) {
adamc@841 215 return s().data;
adamc@841 216 }
adamc@571 217
adamc@604 218 function lastParent() {
adamc@604 219 var pos = document;
adamc@604 220
adamc@600 221 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@600 222 pos = pos.lastChild;
adamc@600 223
adamc@600 224 return pos.parentNode;
adamc@600 225 }
adamc@600 226
adamc@898 227 function parent() {
adamc@898 228 return thisScript ? thisScript.parentNode : lastParent();
adamc@898 229 }
adamc@898 230
adamc@604 231 function addNode(node) {
adamc@899 232 if (thisScript)
adamc@899 233 thisScript.parentNode.replaceChild(node, thisScript);
adamc@899 234 else
adamc@604 235 lastParent().appendChild(node);
adamc@603 236 }
adamc@603 237
adamc@690 238 var thisScript = null;
adamc@690 239
adamc@604 240 function runScripts(node) {
adamc@899 241 if (node.getElementsByTagName) {
adamc@899 242 var savedScript = thisScript;
adamc@603 243
adamc@899 244 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@899 245 var len = scripts.length;
adamc@899 246 for (var i = 0; i < len; ++i)
adamc@899 247 scriptsCopy[i] = scripts[i];
adamc@899 248 for (var i = 0; i < len; ++i) {
adamc@899 249 thisScript = scriptsCopy[i];
adamc@899 250 try {
adamc@899 251 eval(thisScript.textContent);
adamc@899 252 } catch (v) {
adamc@899 253 doExn(v);
adamc@899 254 }
adamc@899 255 if (thisScript.parentNode)
adamc@901 256 thisScript.parentNode.removeChild(thisScript);
adamc@729 257 }
adamc@899 258
adamc@899 259 thisScript = savedScript;
adamc@604 260 }
adamc@603 261 }
adamc@603 262
adamc@603 263
adamc@692 264 // Dynamic tree entry points
adamc@692 265
adamc@967 266 function killScript(scr) {
adamc@967 267 scr.dead = true;
adamc@967 268 for (var ls = scr.sources; ls; ls = ls.next)
adamc@967 269 ls.data.dyns = remove(scr, ls.data.dyns);
adamc@967 270 for (var ls = scr.closures; ls; ls = ls.next)
adamc@967 271 freeClosure(ls.data);
adamc@967 272 }
adamc@967 273
adamc@901 274 function dyn(pnode, s) {
adamc@901 275 var x = document.createElement("script");
adamc@690 276 x.dead = false;
adamc@690 277 x.signal = s;
adamc@690 278 x.sources = null;
adamc@703 279 x.closures = null;
adamc@899 280
adamc@902 281 var firstChild = null;
adamc@899 282
adamc@690 283 x.recreate = function(v) {
adamc@703 284 for (var ls = x.closures; ls; ls = ls.next)
adamc@703 285 freeClosure(ls.data);
adamc@692 286
adamc@902 287 var next;
adamc@902 288 for (var child = firstChild; child && child != x; child = next) {
adamc@902 289 next = child.nextSibling;
adamc@967 290 killScript(child);
adamc@901 291 if (child.getElementsByTagName) {
adamc@901 292 var arr = child.getElementsByTagName("script");
adamc@967 293 for (var i = 0; i < arr.length; ++i)
adamc@967 294 killScript(arr[i]);
adamc@847 295 }
adamc@847 296
adamc@901 297 if (child.parentNode)
adamc@901 298 child.parentNode.removeChild(child);
adamc@899 299 }
adamc@690 300
adamc@703 301 var cls = {v : null};
adamc@901 302 var html = flatten(cls, v);
adamc@703 303 x.closures = cls.v;
adamc@899 304
adamc@901 305 if (pnode == "table") {
adamc@901 306 var dummy = document.createElement("body");
adamc@901 307 dummy.innerHTML = "<table>" + html + "</table>";
adamc@901 308 runScripts(dummy);
adamc@901 309 var table = x.parentNode;
adamc@901 310
adamc@946 311 if (table) {
adamc@946 312 var arr = dummy.getElementsByTagName("tbody");
adamc@946 313 firstChild = null;
adamc@946 314 var tbody;
adamc@946 315 if (arr.length > 0 && table != null)
adamc@946 316 tbody = arr[0];
adamc@946 317 else
adamc@946 318 tbody = dummy.firstChild;
adamc@945 319
adamc@946 320 var next;
adamc@946 321 firstChild = document.createElement("script");
adamc@946 322 table.insertBefore(firstChild, x);
adamc@946 323 for (var node = tbody.firstChild; node; node = next) {
adamc@946 324 next = node.nextSibling;
adamc@946 325 table.insertBefore(node, x);
adamc@946 326 }
adamc@901 327 }
adamc@903 328 } else if (pnode == "tr") {
adamc@903 329 var dummy = document.createElement("body");
adamc@903 330 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
adamc@903 331 runScripts(dummy);
adamc@903 332 var table = x.parentNode;
adamc@903 333
adamc@946 334 if (table) {
adamc@946 335 var arr = dummy.getElementsByTagName("tr");
adamc@946 336 firstChild = null;
adamc@946 337 var tbody;
adamc@946 338 if (arr.length > 0 && table != null)
adamc@946 339 tbody = arr[0];
adamc@946 340 else
adamc@946 341 tbody = dummy.firstChild;
adamc@946 342
adamc@946 343 var next;
adamc@903 344 firstChild = document.createElement("script");
adamc@903 345 table.insertBefore(firstChild, x);
adamc@903 346 for (var node = tbody.firstChild; node; node = next) {
adamc@903 347 next = node.nextSibling;
adamc@903 348 table.insertBefore(node, x);
adamc@903 349 }
adamc@903 350 }
adamc@901 351 } else {
adamc@902 352 firstChild = document.createElement("span");
adamc@902 353 firstChild.innerHTML = html;
adamc@902 354 runScripts(firstChild);
adamc@900 355 if (x.parentNode)
adamc@902 356 x.parentNode.insertBefore(firstChild, x);
adamc@899 357 }
adamc@690 358 };
adamc@899 359
adamc@899 360 addNode(x);
adamc@690 361 populate(x);
adamc@571 362 }
adamc@582 363
adamc@817 364 function input(t, s, recreate) {
adamc@598 365 var x = document.createElement(t);
adamc@690 366 x.dead = false;
adamc@690 367 x.signal = ss(s);
adamc@690 368 x.sources = null;
adamc@817 369 x.recreate = recreate(x);
adamc@899 370 addNode(x);
adamc@690 371 populate(x);
adamc@816 372
adamc@816 373 return x;
adamc@816 374 }
adamc@816 375
adamc@816 376 function inp(s) {
adamc@817 377 var x = input("input", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 378 x.value = s.data;
adamc@816 379 x.onkeyup = function() { sv(s, x.value) };
adamc@816 380
adamc@816 381 return x;
adamc@816 382 }
adamc@816 383
adamc@816 384 function sel(s, content) {
adamc@817 385 var x = input("select", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 386 x.innerHTML = content;
adamc@816 387 x.value = s.data;
adamc@835 388 if (x.value != s.data)
adamc@835 389 sv(s, x.value);
adamc@816 390 x.onchange = function() { sv(s, x.value) };
adamc@797 391
adamc@606 392 return x;
adamc@598 393 }
adamc@598 394
adamc@817 395 function chk(s) {
adamc@817 396 var x = input("input", s, function(x) { return function(v) { if (x.checked != v) x.checked = v; }; });
adamc@817 397 x.type = "checkbox";
adamc@817 398 x.checked = s.data;
adamc@817 399 x.onchange = function() { sv(s, x.checked) };
adamc@817 400
adamc@817 401 return x;
adamc@817 402 }
adamc@817 403
adamc@800 404 function addOnChange(x, f) {
adamc@800 405 var old = x.onchange;
adamc@800 406 x.onchange = function() { old(); f (); };
adamc@800 407 }
adamc@800 408
adamc@692 409
adamc@692 410 // Basic string operations
adamc@692 411
adamc@597 412 function eh(x) {
adamc@800 413 if (x == null)
adamc@800 414 return "NULL";
adamc@800 415 else
adamc@800 416 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 417 }
adamc@597 418
adamc@582 419 function ts(x) { return x.toString() }
adamc@586 420 function bs(b) { return (b ? "True" : "False") }
adamc@586 421
adamc@828 422 function sub(s, i) { return s[i]; }
adamc@828 423 function suf(s, i) { return s.substring(i); }
adamc@828 424 function slen(s) { return s.length; }
adamc@829 425 function sidx(s, ch) {
adamc@829 426 var r = s.indexOf(ch);
adamc@829 427 if (r == -1)
adamc@829 428 return null;
adamc@829 429 else
adamc@829 430 return r;
adamc@829 431 }
adamc@831 432 function sspn(s, chs) {
adamc@831 433 for (var i = 0; i < s.length; ++i)
adamc@831 434 if (chs.indexOf(s[i]) != -1)
adamc@831 435 return i;
adamc@831 436
adamc@831 437 return null;
adamc@831 438 }
adamc@829 439 function schr(s, ch) {
adamc@829 440 var r = s.indexOf(ch);
adamc@829 441 if (r == -1)
adamc@829 442 return null;
adamc@829 443 else
adamc@829 444 return s.substring(r);
adamc@829 445 }
adamc@829 446 function ssub(s, start, len) {
adamc@829 447 return s.substring(start, start+len);
adamc@829 448 }
adamc@821 449
adamc@649 450 function pi(s) {
adamc@649 451 var r = parseInt(s);
adamc@649 452 if (r.toString() == s)
adamc@649 453 return r;
adamc@649 454 else
adamc@729 455 er("Can't parse int: " + s);
adamc@649 456 }
adamc@649 457
adamc@649 458 function pfl(s) {
adamc@649 459 var r = parseFloat(s);
adamc@649 460 if (r.toString() == s)
adamc@649 461 return r;
adamc@649 462 else
adamc@729 463 er("Can't parse float: " + s);
adamc@649 464 }
adamc@649 465
adamc@838 466 function pio(s) {
adamc@838 467 var r = parseInt(s);
adamc@838 468 if (r.toString() == s)
adamc@838 469 return r;
adamc@838 470 else
adamc@838 471 return null;
adamc@838 472 }
adamc@838 473
adamc@838 474 function pflo(s) {
adamc@838 475 var r = parseFloat(s);
adamc@838 476 if (r.toString() == s)
adamc@838 477 return r;
adamc@838 478 else
adamc@838 479 return null;
adamc@838 480 }
adamc@838 481
adamc@692 482 function uf(s) {
adamc@925 483 if (s.length == 0)
adamc@925 484 return "_";
adamc@925 485 return (s[0] == '_' ? "_" : "")
adamc@925 486 + escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B");
adamc@691 487 }
adamc@691 488
adamc@692 489 function uu(s) {
adamc@925 490 if (s.length > 0 && s[0] == '_')
adamc@925 491 s = s.substring(1);
adamc@927 492 else if (s.length >= 3 && s[0] == '%' && s[1] == '5' && (s[2] == 'f' || s[2] == 'F'))
adamc@927 493 s = s.substring(3);
adamc@905 494 return unescape(s.replace(new RegExp ("\\+", "g"), " "));
adamc@905 495 }
adamc@905 496
adamc@912 497 function ub(b) {
adamc@912 498 return b ? "1" : "0";
adamc@912 499 }
adamc@912 500
adamc@905 501 function uul(getToken, getData) {
adamc@905 502 var tok = getToken();
adamc@905 503 if (tok == "Nil") {
adamc@905 504 return null;
adamc@905 505 } else if (tok == "Cons") {
adamc@905 506 var d = getData();
adamc@905 507 var l = uul(getToken, getData);
adamc@905 508 return {_1:d, _2:l};
adamc@905 509 } else
adamc@905 510 throw ("Can't unmarshal list (" + tok + ")");
adamc@692 511 }
adamc@692 512
adamc@961 513 function strcmp(str1, str2) {
adamc@961 514 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
adamc@961 515 }
adamc@692 516
adamc@603 517
adamc@692 518 // Remote calls
adamc@609 519
adamc@703 520 var client_id = null;
adamc@668 521 var client_pass = 0;
adamc@668 522 var url_prefix = "/";
adamc@673 523 var timeout = 60;
adamc@668 524
adamc@668 525 function getXHR(uri)
adamc@609 526 {
adamc@609 527 try {
adamc@609 528 return new XMLHttpRequest();
adamc@609 529 } catch (e) {
adamc@609 530 try {
adamc@609 531 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 532 } catch (e) {
adamc@609 533 try {
adamc@609 534 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 535 } catch (e) {
adamc@609 536 throw "Your browser doesn't seem to support AJAX.";
adamc@609 537 }
adamc@609 538 }
adamc@609 539 }
adamc@609 540 }
adamc@609 541
adamc@736 542 var sig = null;
adamc@736 543
adamc@736 544 function requestUri(xhr, uri, needsSig) {
adamc@668 545 xhr.open("GET", uri, true);
adamc@668 546
adamc@703 547 if (client_id != null) {
adamc@668 548 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 549 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 550 }
adamc@668 551
adamc@736 552 if (needsSig) {
adamc@736 553 if (sig == null)
adamc@736 554 whine("Missing cookie signature!");
adamc@736 555
adamc@736 556 xhr.setRequestHeader("UrWeb-Sig", sig);
adamc@736 557 }
adamc@736 558
adamc@668 559 xhr.send(null);
adamc@668 560 }
adamc@668 561
adamc@736 562 function rc(uri, parse, k, needsSig) {
adamc@728 563 uri = flattenLocal(uri);
adamc@609 564 var xhr = getXHR();
adamc@609 565
adamc@609 566 xhr.onreadystatechange = function() {
adamc@612 567 if (xhr.readyState == 4) {
adamc@612 568 var isok = false;
adamc@612 569
adamc@612 570 try {
adamc@612 571 if (xhr.status == 200)
adamc@612 572 isok = true;
adamc@612 573 } catch (e) { }
adamc@612 574
adamc@729 575 if (isok) {
adamc@729 576 try {
adamc@729 577 k(parse(xhr.responseText));
adamc@729 578 } catch (v) {
adamc@729 579 doExn(v);
adamc@729 580 }
adamc@729 581 } else {
adamc@729 582 conn();
adamc@649 583 }
adamc@612 584 }
adamc@609 585 };
adamc@609 586
adamc@736 587 requestUri(xhr, uri, needsSig);
adamc@609 588 }
adamc@667 589
adamc@667 590 function path_join(s1, s2) {
adamc@667 591 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 592 return s1 + s2;
adamc@667 593 else
adamc@667 594 return s1 + "/" + s2;
adamc@667 595 }
adamc@667 596
adamc@670 597 var channels = [];
adamc@670 598
adamc@670 599 function newQueue() {
adamc@670 600 return { front : null, back : null };
adamc@670 601 }
adamc@670 602 function enqueue(q, v) {
adamc@670 603 if (q.front == null) {
adamc@670 604 q.front = cons(v, null);
adamc@670 605 q.back = q.front;
adamc@670 606 } else {
adamc@670 607 var node = cons(v, null);
adamc@690 608 q.back.next = node;
adamc@670 609 q.back = node;
adamc@670 610 }
adamc@670 611 }
adamc@670 612 function dequeue(q) {
adamc@670 613 if (q.front == null)
adamc@670 614 return null;
adamc@670 615 else {
adamc@690 616 var r = q.front.data;
adamc@690 617 q.front = q.front.next;
adamc@670 618 if (q.front == null)
adamc@670 619 q.back = null;
adamc@670 620 return r;
adamc@670 621 }
adamc@670 622 }
adamc@670 623
adamc@670 624 function newChannel() {
adamc@670 625 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 626 }
adamc@670 627
adamc@667 628 function listener() {
adamc@668 629 var uri = path_join(url_prefix, ".msgs");
adamc@667 630 var xhr = getXHR();
adamc@673 631 var tid, orsc, onTimeout;
adamc@673 632
adamc@673 633 var connect = function () {
adamc@673 634 xhr.onreadystatechange = orsc;
adamc@673 635 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@736 636 requestUri(xhr, uri, false);
adamc@673 637 }
adamc@673 638
adamc@673 639 orsc = function() {
adamc@667 640 if (xhr.readyState == 4) {
adamc@673 641 window.clearTimeout(tid);
adamc@673 642
adamc@667 643 var isok = false;
adamc@667 644
adamc@667 645 try {
adamc@667 646 if (xhr.status == 200)
adamc@667 647 isok = true;
adamc@667 648 } catch (e) { }
adamc@667 649
adamc@668 650 if (isok) {
adamc@889 651 var text = xhr.responseText
adamc@889 652 if (text == "")
adamc@889 653 return;
adamc@889 654 var lines = text.split("\n");
adamc@889 655
adamc@729 656 if (lines.length < 2) {
adamc@729 657 discon();
adamc@729 658 return;
adamc@729 659 }
adamc@669 660
adamc@669 661 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@670 662 var chn = lines[i];
adamc@670 663 var msg = lines[i+1];
adamc@670 664
adamc@670 665 if (chn < 0)
adamc@670 666 whine("Out-of-bounds channel in message from remote server");
adamc@670 667
adamc@670 668 var ch;
adamc@670 669
adamc@670 670 if (chn >= channels.length || channels[chn] == null) {
adamc@670 671 ch = newChannel();
adamc@670 672 channels[chn] = ch;
adamc@670 673 } else
adamc@670 674 ch = channels[chn];
adamc@670 675
adamc@670 676 var listener = dequeue(ch.listeners);
adamc@670 677 if (listener == null) {
adamc@670 678 enqueue(ch.msgs, msg);
adamc@670 679 } else {
adamc@728 680 try {
adamc@728 681 listener(msg);
adamc@728 682 } catch (v) {
adamc@728 683 doExn(v);
adamc@728 684 }
adamc@670 685 }
adamc@669 686 }
adamc@669 687
adamc@673 688 connect();
adamc@668 689 }
adamc@667 690 else {
adamc@729 691 try {
adamc@824 692 if (xhr.status != 0)
adamc@824 693 servErr("Error querying remote server for messages: " + xhr.status);
adamc@889 694 } catch (e) { }
adamc@667 695 }
adamc@667 696 }
adamc@667 697 };
adamc@667 698
adamc@673 699 onTimeout = function() {
adamc@673 700 xhr.abort();
adamc@673 701 connect();
adamc@673 702 };
adamc@673 703
adamc@673 704 connect();
adamc@667 705 }
adamc@670 706
adamc@670 707 function rv(chn, parse, k) {
adamc@682 708 if (chn == null)
adamc@682 709 return;
adamc@682 710
adamc@670 711 if (chn < 0)
adamc@670 712 whine("Out-of-bounds channel receive");
adamc@670 713
adamc@670 714 var ch;
adamc@670 715
adamc@670 716 if (chn >= channels.length || channels[chn] == null) {
adamc@670 717 ch = newChannel();
adamc@670 718 channels[chn] = ch;
adamc@670 719 } else
adamc@670 720 ch = channels[chn];
adamc@670 721
adamc@670 722 var msg = dequeue(ch.msgs);
adamc@670 723 if (msg == null) {
adamc@670 724 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
adamc@670 725 } else {
adamc@728 726 try {
adamc@728 727 k(parse(msg))(null);
adamc@728 728 } catch (v) {
adamc@728 729 doExn(v);
adamc@728 730 }
adamc@670 731 }
adamc@670 732 }
adamc@693 733
adamc@693 734
adamc@895 735 // Key events
adamc@895 736
adamc@895 737 function kc(e) {
adamc@895 738 return window.event ? e.keyCode : e.which;
adamc@895 739 }
adamc@895 740
adamc@895 741
adamc@693 742 // App-specific code
adamc@693 743