| 1 |
// Copyright 2015 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 adaptor of XhrStreamReader to the NodeReadableStream interface. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.net.streams.XhrNodeReadableStream'); |
| 20 |
|
| 21 |
goog.require('goog.array'); |
| 22 |
goog.require('goog.log'); |
| 23 |
goog.require('goog.net.streams.NodeReadableStream'); |
| 24 |
goog.require('goog.net.streams.XhrStreamReader'); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* The XhrNodeReadableStream class. |
| 30 |
* |
| 31 |
* @param {!goog.net.streams.XhrStreamReader} xhrReader The XhrStreamReader |
| 32 |
* object that handles the events of the underlying Xhr. |
| 33 |
* @constructor |
| 34 |
* @implements {goog.net.streams.NodeReadableStream} |
| 35 |
* @struct |
| 36 |
* @final |
| 37 |
* @package |
| 38 |
*/ |
| 39 |
goog.net.streams.XhrNodeReadableStream = function(xhrReader) { |
| 40 |
/** |
| 41 |
* @const |
| 42 |
* @private {?goog.log.Logger} the logger. |
| 43 |
*/ |
| 44 |
this.logger_ = goog.log.getLogger('goog.net.streams.XhrNodeReadableStream'); |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* The xhr reader. |
| 49 |
* |
| 50 |
* @private {!goog.net.streams.XhrStreamReader} the xhr reader. |
| 51 |
*/ |
| 52 |
this.xhrReader_ = xhrReader; |
| 53 |
|
| 54 |
this.xhrReader_.setDataHandler(goog.bind(this.onData_, this)); |
| 55 |
this.xhrReader_.setStatusHandler(goog.bind(this.onStatusChange_, this)); |
| 56 |
|
| 57 |
/** |
| 58 |
* The callback map, keyed by eventTypes. |
| 59 |
* |
| 60 |
* @private {!Object<Array<function(!Object=)>>} |
| 61 |
*/ |
| 62 |
this.callbackMap_ = {}; |
| 63 |
|
| 64 |
/** |
| 65 |
* The callback-once map, keyed by eventTypes. |
| 66 |
* |
| 67 |
* @private {!Object<Array<function(!Object=)>>} |
| 68 |
*/ |
| 69 |
this.callbackOnceMap_ = {}; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* @override |
| 75 |
*/ |
| 76 |
goog.net.streams.XhrNodeReadableStream.prototype.on = function( |
| 77 |
eventType, callback) { |
| 78 |
var callbacks = this.callbackMap_[eventType]; |
| 79 |
if (!callbacks) { |
| 80 |
callbacks = []; |
| 81 |
this.callbackMap_[eventType] = callbacks; |
| 82 |
} |
| 83 |
|
| 84 |
callbacks.push(callback); |
| 85 |
return this; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* @override |
| 91 |
*/ |
| 92 |
goog.net.streams.XhrNodeReadableStream.prototype.addListener = function( |
| 93 |
eventType, callback) { |
| 94 |
this.on(eventType, callback); |
| 95 |
return this; |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* @override |
| 101 |
*/ |
| 102 |
goog.net.streams.XhrNodeReadableStream.prototype.removeListener = function( |
| 103 |
eventType, callback) { |
| 104 |
var callbacks = this.callbackMap_[eventType]; |
| 105 |
if (callbacks) { |
| 106 |
goog.array.remove(callbacks, callback); // keep the empty array |
| 107 |
} |
| 108 |
|
| 109 |
var onceCallbacks = this.callbackOnceMap_[eventType]; |
| 110 |
if (onceCallbacks) { |
| 111 |
goog.array.remove(onceCallbacks, callback); |
| 112 |
} |
| 113 |
|
| 114 |
return this; |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* @override |
| 120 |
*/ |
| 121 |
goog.net.streams.XhrNodeReadableStream.prototype.once = function( |
| 122 |
eventType, callback) { |
| 123 |
var callbacks = this.callbackOnceMap_[eventType]; |
| 124 |
if (!callbacks) { |
| 125 |
callbacks = []; |
| 126 |
this.callbackOnceMap_[eventType] = callbacks; |
| 127 |
} |
| 128 |
|
| 129 |
callbacks.push(callback); |
| 130 |
return this; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Handles any new data from XHR. |
| 136 |
* |
| 137 |
* @param {!Array<!Object>} messages New messages, to be delivered in order |
| 138 |
* and atomically. |
| 139 |
* @private |
| 140 |
*/ |
| 141 |
goog.net.streams.XhrNodeReadableStream.prototype.onData_ = function(messages) { |
| 142 |
var callbacks = |
| 143 |
this.callbackMap_[goog.net.streams.NodeReadableStream.EventType.DATA]; |
| 144 |
if (callbacks) { |
| 145 |
this.doMessages_(messages, callbacks); |
| 146 |
} |
| 147 |
|
| 148 |
var onceCallbacks = |
| 149 |
this.callbackOnceMap_[goog.net.streams.NodeReadableStream.EventType.DATA]; |
| 150 |
if (onceCallbacks) { |
| 151 |
this.doMessages_(messages, onceCallbacks); |
| 152 |
} |
| 153 |
this.callbackOnceMap_[goog.net.streams.NodeReadableStream.EventType.DATA] = |
| 154 |
[]; |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Deliver messages to registered callbacks. |
| 160 |
* |
| 161 |
* Exceptions are caught and logged (debug), and ignored otherwise. |
| 162 |
* |
| 163 |
* @param {!Array<!Object>} messages The messages to be delivered |
| 164 |
* @param {!Array<function(!Object=)>} callbacks The callbacks. |
| 165 |
* @private |
| 166 |
*/ |
| 167 |
goog.net.streams.XhrNodeReadableStream.prototype.doMessages_ = function( |
| 168 |
messages, callbacks) { |
| 169 |
var self = this; |
| 170 |
for (var i = 0; i < messages.length; i++) { |
| 171 |
var message = messages[i]; |
| 172 |
|
| 173 |
goog.array.forEach(callbacks, function(callback) { |
| 174 |
try { |
| 175 |
callback(message); |
| 176 |
} catch (ex) { |
| 177 |
self.handleError_('message-callback exception (ignored) ' + ex); |
| 178 |
} |
| 179 |
}); |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Handles any state changes from XHR. |
| 186 |
* |
| 187 |
* @private |
| 188 |
*/ |
| 189 |
goog.net.streams.XhrNodeReadableStream.prototype.onStatusChange_ = function() { |
| 190 |
var currentStatus = this.xhrReader_.getStatus(); |
| 191 |
var Status = goog.net.streams.XhrStreamReader.Status; |
| 192 |
var EventType = goog.net.streams.NodeReadableStream.EventType; |
| 193 |
|
| 194 |
switch (currentStatus) { |
| 195 |
case Status.ACTIVE: |
| 196 |
this.doStatus_(EventType.READABLE); |
| 197 |
break; |
| 198 |
|
| 199 |
case Status.BAD_DATA: |
| 200 |
case Status.HANDLER_EXCEPTION: |
| 201 |
case Status.NO_DATA: |
| 202 |
case Status.TIMEOUT: |
| 203 |
case Status.XHR_ERROR: |
| 204 |
this.doStatus_(EventType.ERROR); |
| 205 |
break; |
| 206 |
|
| 207 |
case Status.CANCELLED: |
| 208 |
this.doStatus_(EventType.CLOSE); |
| 209 |
break; |
| 210 |
|
| 211 |
case Status.SUCCESS: |
| 212 |
this.doStatus_(EventType.END); |
| 213 |
break; |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Run status change callbacks. |
| 220 |
* |
| 221 |
* @param {string} eventType The event type |
| 222 |
* @private |
| 223 |
*/ |
| 224 |
goog.net.streams.XhrNodeReadableStream.prototype.doStatus_ = function( |
| 225 |
eventType) { |
| 226 |
var callbacks = this.callbackMap_[eventType]; |
| 227 |
var self = this; |
| 228 |
if (callbacks) { |
| 229 |
goog.array.forEach(callbacks, function(callback) { |
| 230 |
try { |
| 231 |
callback(); |
| 232 |
} catch (ex) { |
| 233 |
self.handleError_('status-callback exception (ignored) ' + ex); |
| 234 |
} |
| 235 |
}); |
| 236 |
} |
| 237 |
|
| 238 |
var onceCallbacks = this.callbackOnceMap_[eventType]; |
| 239 |
if (onceCallbacks) { |
| 240 |
goog.array.forEach(onceCallbacks, function(callback) { callback(); }); |
| 241 |
} |
| 242 |
|
| 243 |
this.callbackOnceMap_[eventType] = []; |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* Log an error |
| 249 |
* |
| 250 |
* @param {string} message The error message |
| 251 |
* @private |
| 252 |
*/ |
| 253 |
goog.net.streams.XhrNodeReadableStream.prototype.handleError_ = function( |
| 254 |
message) { |
| 255 |
goog.log.error(this.logger_, message); |
| 256 |
}; |