bulkloaderhelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Helper class to load a list of URIs in bulk. All URIs
  16. * must be a successfully loaded in order for the entire load to be considered
  17. * a success.
  18. *
  19. */
  20. goog.provide('goog.net.BulkLoaderHelper');
  21. goog.require('goog.Disposable');
  22. /**
  23. * Helper class used to load multiple URIs.
  24. * @param {Array<string|goog.Uri>} uris The URIs to load.
  25. * @constructor
  26. * @extends {goog.Disposable}
  27. * @final
  28. */
  29. goog.net.BulkLoaderHelper = function(uris) {
  30. goog.Disposable.call(this);
  31. /**
  32. * The URIs to load.
  33. * @type {Array<string|goog.Uri>}
  34. * @private
  35. */
  36. this.uris_ = uris;
  37. /**
  38. * The response from the XHR's.
  39. * @type {Array<string>}
  40. * @private
  41. */
  42. this.responseTexts_ = [];
  43. };
  44. goog.inherits(goog.net.BulkLoaderHelper, goog.Disposable);
  45. /**
  46. * Gets the URI by id.
  47. * @param {number} id The id.
  48. * @return {string|goog.Uri} The URI specified by the id.
  49. */
  50. goog.net.BulkLoaderHelper.prototype.getUri = function(id) {
  51. return this.uris_[id];
  52. };
  53. /**
  54. * Gets the URIs.
  55. * @return {Array<string|goog.Uri>} The URIs.
  56. */
  57. goog.net.BulkLoaderHelper.prototype.getUris = function() {
  58. return this.uris_;
  59. };
  60. /**
  61. * Gets the response texts.
  62. * @return {Array<string>} The response texts.
  63. */
  64. goog.net.BulkLoaderHelper.prototype.getResponseTexts = function() {
  65. return this.responseTexts_;
  66. };
  67. /**
  68. * Sets the response text by id.
  69. * @param {number} id The id.
  70. * @param {string} responseText The response texts.
  71. */
  72. goog.net.BulkLoaderHelper.prototype.setResponseText = function(
  73. id, responseText) {
  74. this.responseTexts_[id] = responseText;
  75. };
  76. /**
  77. * Determines if the load of the URIs is complete.
  78. * @return {boolean} TRUE iff the load is complete.
  79. */
  80. goog.net.BulkLoaderHelper.prototype.isLoadComplete = function() {
  81. var responseTexts = this.responseTexts_;
  82. if (responseTexts.length == this.uris_.length) {
  83. for (var i = 0; i < responseTexts.length; i++) {
  84. if (!goog.isDefAndNotNull(responseTexts[i])) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. return false;
  91. };
  92. /** @override */
  93. goog.net.BulkLoaderHelper.prototype.disposeInternal = function() {
  94. goog.net.BulkLoaderHelper.superClass_.disposeInternal.call(this);
  95. this.uris_ = null;
  96. this.responseTexts_ = null;
  97. };