nio.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = {};
  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. for (i = 0; i < str.length; i++)
  35. ret[i] = String.fromCharCode(str.charCodeAt(i) & 0xff);
  36. return ret.join("");
  37. }
  38. this.reset = function() {
  39. cache = {};
  40. keys = [];
  41. };
  42. this.send = function(url, responseHandler, characterSet, mediaTypeParam) {
  43. var xhr, key = JSON.stringify({
  44. url : url,
  45. characterSet : characterSet,
  46. mediaTypeParam : mediaTypeParam
  47. }), resource = cache[key];
  48. if (resource)
  49. setTimeout(function() {
  50. responseHandler(resource);
  51. }, 1);
  52. else if (pendingResponseHandlers[key])
  53. pendingResponseHandlers[key].push(responseHandler);
  54. else {
  55. pendingResponseHandlers[key] = [ responseHandler ];
  56. xhr = new XMLHttpRequest();
  57. xhr.onreadystatechange = function() {
  58. if (xhr.readyState == 4) {
  59. cache[key] = {
  60. url : url,
  61. status : xhr.status,
  62. mediaType : xhr.getResponseHeader("Content-Type"),
  63. content : mediaTypeParam == "base64" ? btoa(throwAwayHighOrderBytes(xhr.responseText)) : xhr.responseText,
  64. mediaTypeParam : mediaTypeParam
  65. };
  66. keys.push(key);
  67. sendResponses(key);
  68. }
  69. };
  70. xhr.onerror = function() {
  71. sendResponses(key);
  72. };
  73. xhr.open("GET", url, true);
  74. if (characterSet)
  75. xhr.overrideMimeType('text/plain; charset=' + characterSet);
  76. try {
  77. xhr.send(null);
  78. } catch (e) {
  79. sendResponses(key);
  80. }
  81. }
  82. };
  83. };
  84. })();