nio.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 arrayBufferToBase64(buffer) {
  33. var binary, bytes, len, i;
  34. binary = "";
  35. if (buffer) {
  36. bytes = new Uint8Array(buffer);
  37. len = bytes.byteLength;
  38. for (i = 0; i < len; i++) {
  39. binary += String.fromCharCode(bytes[i]);
  40. }
  41. }
  42. return btoa(binary);
  43. }
  44. this.reset = function() {
  45. cache = {};
  46. keys = [];
  47. };
  48. this.send = function(url, responseHandler, characterSet, mediaTypeParam) {
  49. var xhr, timeout, key = JSON.stringify({
  50. url : url,
  51. characterSet : characterSet,
  52. mediaTypeParam : mediaTypeParam
  53. });
  54. if (cache[key])
  55. setTimeout(function() {
  56. responseHandler(cache[key]);
  57. }, 1);
  58. else if (pendingResponseHandlers[key])
  59. pendingResponseHandlers[key].push(responseHandler);
  60. else {
  61. pendingResponseHandlers[key] = [ responseHandler ];
  62. xhr = new XMLHttpRequest();
  63. xhr.onload = function() {
  64. clearTimeout(timeout);
  65. cache[key] = {
  66. url : url,
  67. status : xhr.status,
  68. mediaType : xhr.getResponseHeader("Content-Type"),
  69. content : mediaTypeParam == "base64" ? arrayBufferToBase64(xhr.response) : xhr.responseText,
  70. mediaTypeParam : mediaTypeParam
  71. };
  72. keys.push(key);
  73. sendResponses(key);
  74. };
  75. xhr.onerror = function() {
  76. cache[key] = {};
  77. keys.push(key);
  78. sendResponses(key);
  79. };
  80. xhr.open("GET", url, true);
  81. if (mediaTypeParam == "base64") {
  82. xhr.responseType = "arraybuffer";
  83. }
  84. timeout = setTimeout(function() {
  85. xhr.abort();
  86. sendResponses(key);
  87. }, XHR_TIMEOUT);
  88. try {
  89. xhr.send(null);
  90. } catch (e) {
  91. sendResponses(key);
  92. }
  93. }
  94. };
  95. };
  96. })();