nio.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2011 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile Core.
  6. *
  7. * SingleFile Core is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile Core is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile Core. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. (function() {
  21. singlefile.nio = {};
  22. singlefile.nio.RequestManager = function() {
  23. var cache = {}, keys = [], pendingResponseHandlers = {}, XHR_TIMEOUT = 45000;
  24. function sendResponses(key) {
  25. if (pendingResponseHandlers[key]) {
  26. pendingResponseHandlers[key].forEach(function(callback) {
  27. callback(cache[key]);
  28. });
  29. delete pendingResponseHandlers[key];
  30. }
  31. }
  32. function throwAwayHighOrderBytes(str) {
  33. var i, ret = [];
  34. ret.length = str.length;
  35. for (i = 0; i < str.length; i++)
  36. ret[i] = String.fromCharCode(str.charCodeAt(i) & 0xff);
  37. return ret.join("");
  38. }
  39. this.reset = function() {
  40. cache = {};
  41. keys = [];
  42. };
  43. this.send = function(url, responseHandler, characterSet, mediaTypeParam) {
  44. var xhr, timeout, key = JSON.stringify({
  45. url : url,
  46. characterSet : characterSet,
  47. mediaTypeParam : mediaTypeParam
  48. });
  49. if (cache[key])
  50. setTimeout(function() {
  51. responseHandler(cache[key]);
  52. }, 1);
  53. else if (pendingResponseHandlers[key])
  54. pendingResponseHandlers[key].push(responseHandler);
  55. else {
  56. pendingResponseHandlers[key] = [ responseHandler ];
  57. xhr = new XMLHttpRequest();
  58. xhr.onreadystatechange = function() {
  59. if (xhr.readyState == 4) {
  60. clearTimeout(timeout);
  61. cache[key] = {
  62. url : url,
  63. status : xhr.status,
  64. mediaType : xhr.getResponseHeader("Content-Type"),
  65. content : mediaTypeParam == "base64" ? btoa(throwAwayHighOrderBytes(xhr.responseText)) : xhr.responseText,
  66. mediaTypeParam : mediaTypeParam
  67. };
  68. keys.push(key);
  69. sendResponses(key);
  70. }
  71. };
  72. xhr.onerror = function() {
  73. sendResponses(key);
  74. };
  75. xhr.open("GET", url, true);
  76. if (characterSet)
  77. xhr.overrideMimeType('text/plain; charset=' + characterSet);
  78. timeout = setTimeout(function() {
  79. xhr.abort();
  80. sendResponses(key);
  81. }, XHR_TIMEOUT);
  82. try {
  83. xhr.send(null);
  84. } catch (e) {
  85. sendResponses(key);
  86. }
  87. }
  88. };
  89. };
  90. })();