bulkloader.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Loads a list of URIs in bulk. All requests must be a success
  16. * in order for the load to be considered a success.
  17. *
  18. */
  19. goog.provide('goog.net.BulkLoader');
  20. goog.require('goog.events.EventHandler');
  21. goog.require('goog.events.EventTarget');
  22. goog.require('goog.log');
  23. goog.require('goog.net.BulkLoaderHelper');
  24. goog.require('goog.net.EventType');
  25. goog.require('goog.net.XhrIo');
  26. /**
  27. * Class used to load multiple URIs.
  28. * @param {Array<string|goog.Uri>} uris The URIs to load.
  29. * @constructor
  30. * @extends {goog.events.EventTarget}
  31. * @final
  32. */
  33. goog.net.BulkLoader = function(uris) {
  34. goog.events.EventTarget.call(this);
  35. /**
  36. * The bulk loader helper.
  37. * @type {goog.net.BulkLoaderHelper}
  38. * @private
  39. */
  40. this.helper_ = new goog.net.BulkLoaderHelper(uris);
  41. /**
  42. * The handler for managing events.
  43. * @type {goog.events.EventHandler<!goog.net.BulkLoader>}
  44. * @private
  45. */
  46. this.eventHandler_ = new goog.events.EventHandler(this);
  47. };
  48. goog.inherits(goog.net.BulkLoader, goog.events.EventTarget);
  49. /**
  50. * A logger.
  51. * @type {goog.log.Logger}
  52. * @private
  53. */
  54. goog.net.BulkLoader.prototype.logger_ =
  55. goog.log.getLogger('goog.net.BulkLoader');
  56. /**
  57. * Gets the response texts, in order.
  58. * @return {Array<string>} The response texts.
  59. */
  60. goog.net.BulkLoader.prototype.getResponseTexts = function() {
  61. return this.helper_.getResponseTexts();
  62. };
  63. /**
  64. * Gets the request Uris.
  65. * @return {Array<string>} The request URIs, in order.
  66. */
  67. goog.net.BulkLoader.prototype.getRequestUris = function() {
  68. return this.helper_.getUris();
  69. };
  70. /**
  71. * Starts the process of loading the URIs.
  72. */
  73. goog.net.BulkLoader.prototype.load = function() {
  74. var eventHandler = this.eventHandler_;
  75. var uris = this.helper_.getUris();
  76. goog.log.info(
  77. this.logger_, 'Starting load of code with ' + uris.length + ' uris.');
  78. for (var i = 0; i < uris.length; i++) {
  79. var xhrIo = new goog.net.XhrIo();
  80. eventHandler.listen(
  81. xhrIo, goog.net.EventType.COMPLETE,
  82. goog.bind(this.handleEvent_, this, i));
  83. xhrIo.send(uris[i]);
  84. }
  85. };
  86. /**
  87. * Handles all events fired by the XhrManager.
  88. * @param {number} id The id of the request.
  89. * @param {goog.events.Event} e The event.
  90. * @private
  91. */
  92. goog.net.BulkLoader.prototype.handleEvent_ = function(id, e) {
  93. goog.log.info(
  94. this.logger_, 'Received event "' + e.type + '" for id ' + id +
  95. ' with uri ' + this.helper_.getUri(id));
  96. var xhrIo = /** @type {goog.net.XhrIo} */ (e.target);
  97. if (xhrIo.isSuccess()) {
  98. this.handleSuccess_(id, xhrIo);
  99. } else {
  100. this.handleError_(id, xhrIo);
  101. }
  102. };
  103. /**
  104. * Handles when a request is successful (i.e., completed and response received).
  105. * Stores thhe responseText and checks if loading is complete.
  106. * @param {number} id The id of the request.
  107. * @param {goog.net.XhrIo} xhrIo The XhrIo objects that was used.
  108. * @private
  109. */
  110. goog.net.BulkLoader.prototype.handleSuccess_ = function(id, xhrIo) {
  111. // Save the response text.
  112. this.helper_.setResponseText(id, xhrIo.getResponseText());
  113. // Check if all response texts have been received.
  114. if (this.helper_.isLoadComplete()) {
  115. this.finishLoad_();
  116. }
  117. xhrIo.dispose();
  118. };
  119. /**
  120. * Handles when a request has ended in error (i.e., all retries completed and
  121. * none were successful). Cancels loading of the URI's.
  122. * @param {number|string} id The id of the request.
  123. * @param {goog.net.XhrIo} xhrIo The XhrIo objects that was used.
  124. * @private
  125. */
  126. goog.net.BulkLoader.prototype.handleError_ = function(id, xhrIo) {
  127. // TODO(user): Abort all pending requests.
  128. // Dispatch the ERROR event.
  129. this.dispatchEvent(goog.net.EventType.ERROR);
  130. xhrIo.dispose();
  131. };
  132. /**
  133. * Finishes the load of the URI's. Dispatches the SUCCESS event.
  134. * @private
  135. */
  136. goog.net.BulkLoader.prototype.finishLoad_ = function() {
  137. goog.log.info(this.logger_, 'All uris loaded.');
  138. // Dispatch the SUCCESS event.
  139. this.dispatchEvent(goog.net.EventType.SUCCESS);
  140. };
  141. /** @override */
  142. goog.net.BulkLoader.prototype.disposeInternal = function() {
  143. goog.net.BulkLoader.superClass_.disposeInternal.call(this);
  144. this.eventHandler_.dispose();
  145. this.eventHandler_ = null;
  146. this.helper_.dispose();
  147. this.helper_ = null;
  148. };