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