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