Simplify NetworkManager's request/response management

This patch:
- drops Request dependency on NetworkManager
- drops Response dependency on NetworkManager
- drops requestIds from Request and Response objects
This commit is contained in:
Andrey Lushnikov 2017-06-29 18:18:06 -07:00
parent 3dfa688920
commit 7f74daf62c

View File

@ -26,8 +26,6 @@ class NetworkManager extends EventEmitter {
this._requestInterceptor = null;
/* @type {!Map<string, !Request>} */
this._idToRequest = new Map();
/* @type {!Map<string, !Response>} */
this._idToResponse = new Map();
this._httpHeaders = {};
this._userAgent = userAgent;
@ -94,7 +92,7 @@ class NetworkManager extends EventEmitter {
* @param {!Object} event
*/
_onRequestWillBeSent(event) {
let request = new Request(this, event.requestId, event.request);
let request = new Request(event.request);
this._idToRequest.set(event.requestId, request);
this.emit(NetworkManager.Events.Request, request);
}
@ -111,8 +109,9 @@ class NetworkManager extends EventEmitter {
* @param {!Object} event
*/
_onResponseReceived(event) {
let response = new Response(this, event.requestId, event.response);
this._idToResponse.set(event.requestId, response);
let request = this._idToRequest.get(event.requestId) || null;
let response = new Response(request, event.response);
request._response = response;
this.emit(NetworkManager.Events.Response, response);
}
@ -121,6 +120,7 @@ class NetworkManager extends EventEmitter {
*/
_onLoadingFinished(event) {
let request = this._idToRequest.get(event.requestId);
this._idToRequest.delete(event.requestId);
this.emit(NetworkManager.Events.RequestFinished, request);
}
@ -129,6 +129,7 @@ class NetworkManager extends EventEmitter {
*/
_onLoadingFailed(event) {
let request = this._idToRequest.get(event.requestId);
this._idToRequest.delete(event.requestId);
this.emit(NetworkManager.Events.RequestFailed, request);
}
}
@ -220,13 +221,10 @@ class Headers {
class Request {
/**
* @param {!NetworkManager} networkManager
* @param {string} id
* @param {!Object} payload
*/
constructor(networkManager, id, payload) {
this._networkManager = networkManager;
this._id = id;
constructor(payload) {
this._response = null;
this.url = payload.url;
this.method = payload.method;
this.headers = Headers.fromPayload(payload.headers);
@ -237,20 +235,17 @@ class Request {
* @return {?Response}
*/
response() {
return this._networkManager._idToResponse.get(this._id) || null;
return this._response;
}
}
class Response {
/**
* @param {!NetworkManager} networkManager
* @param {string} id
* @param {?Request} request
* @param {!Object} payload
*/
constructor(networkManager, id, payload) {
this._networkManager = networkManager;
this._id = id;
constructor(request, payload) {
this._request = request;
this.headers = Headers.fromPayload(payload.headers);
this.ok = payload.status >= 200 && payload.status <= 299;
this.status = payload.status;
@ -262,7 +257,7 @@ class Response {
* @return {?Response}
*/
request() {
return this._networkManager._idToRequest.get(this._id) || null;
return this._request;
}
}