Debian Codegraph @

closure-library/closure/goog/net/bulkloader.js

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