Path: blob/trunk/third_party/closure/goog/net/bulkloader.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Loads a list of URIs in bulk. All requests must be a success16* in order for the load to be considered a success.17*18*/1920goog.provide('goog.net.BulkLoader');2122goog.require('goog.events.EventHandler');23goog.require('goog.events.EventTarget');24goog.require('goog.log');25goog.require('goog.net.BulkLoaderHelper');26goog.require('goog.net.EventType');27goog.require('goog.net.XhrIo');28293031/**32* Class used to load multiple URIs.33* @param {Array<string|goog.Uri>} uris The URIs to load.34* @constructor35* @extends {goog.events.EventTarget}36* @final37*/38goog.net.BulkLoader = function(uris) {39goog.events.EventTarget.call(this);4041/**42* The bulk loader helper.43* @type {goog.net.BulkLoaderHelper}44* @private45*/46this.helper_ = new goog.net.BulkLoaderHelper(uris);4748/**49* The handler for managing events.50* @type {goog.events.EventHandler<!goog.net.BulkLoader>}51* @private52*/53this.eventHandler_ = new goog.events.EventHandler(this);54};55goog.inherits(goog.net.BulkLoader, goog.events.EventTarget);565758/**59* A logger.60* @type {goog.log.Logger}61* @private62*/63goog.net.BulkLoader.prototype.logger_ =64goog.log.getLogger('goog.net.BulkLoader');656667/**68* Gets the response texts, in order.69* @return {Array<string>} The response texts.70*/71goog.net.BulkLoader.prototype.getResponseTexts = function() {72return this.helper_.getResponseTexts();73};747576/**77* Gets the request Uris.78* @return {Array<string>} The request URIs, in order.79*/80goog.net.BulkLoader.prototype.getRequestUris = function() {81return this.helper_.getUris();82};838485/**86* Starts the process of loading the URIs.87*/88goog.net.BulkLoader.prototype.load = function() {89var eventHandler = this.eventHandler_;90var uris = this.helper_.getUris();91goog.log.info(92this.logger_, 'Starting load of code with ' + uris.length + ' uris.');9394for (var i = 0; i < uris.length; i++) {95var xhrIo = new goog.net.XhrIo();96eventHandler.listen(97xhrIo, goog.net.EventType.COMPLETE,98goog.bind(this.handleEvent_, this, i));99100xhrIo.send(uris[i]);101}102};103104105/**106* Handles all events fired by the XhrManager.107* @param {number} id The id of the request.108* @param {goog.events.Event} e The event.109* @private110*/111goog.net.BulkLoader.prototype.handleEvent_ = function(id, e) {112goog.log.info(113this.logger_, 'Received event "' + e.type + '" for id ' + id +114' with uri ' + this.helper_.getUri(id));115var xhrIo = /** @type {goog.net.XhrIo} */ (e.target);116if (xhrIo.isSuccess()) {117this.handleSuccess_(id, xhrIo);118} else {119this.handleError_(id, xhrIo);120}121};122123124/**125* Handles when a request is successful (i.e., completed and response received).126* Stores thhe responseText and checks if loading is complete.127* @param {number} id The id of the request.128* @param {goog.net.XhrIo} xhrIo The XhrIo objects that was used.129* @private130*/131goog.net.BulkLoader.prototype.handleSuccess_ = function(id, xhrIo) {132// Save the response text.133this.helper_.setResponseText(id, xhrIo.getResponseText());134135// Check if all response texts have been received.136if (this.helper_.isLoadComplete()) {137this.finishLoad_();138}139xhrIo.dispose();140};141142143/**144* Handles when a request has ended in error (i.e., all retries completed and145* none were successful). Cancels loading of the URI's.146* @param {number|string} id The id of the request.147* @param {goog.net.XhrIo} xhrIo The XhrIo objects that was used.148* @private149*/150goog.net.BulkLoader.prototype.handleError_ = function(id, xhrIo) {151// TODO(user): Abort all pending requests.152153// Dispatch the ERROR event.154this.dispatchEvent(goog.net.EventType.ERROR);155xhrIo.dispose();156};157158159/**160* Finishes the load of the URI's. Dispatches the SUCCESS event.161* @private162*/163goog.net.BulkLoader.prototype.finishLoad_ = function() {164goog.log.info(this.logger_, 'All uris loaded.');165166// Dispatch the SUCCESS event.167this.dispatchEvent(goog.net.EventType.SUCCESS);168};169170171/** @override */172goog.net.BulkLoader.prototype.disposeInternal = function() {173goog.net.BulkLoader.superClass_.disposeInternal.call(this);174175this.eventHandler_.dispose();176this.eventHandler_ = null;177178this.helper_.dispose();179this.helper_ = null;180};181182183