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