From 7f74daf62c8aa730765de395953131e2d0a1d929 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 29 Jun 2017 18:18:06 -0700 Subject: [PATCH] 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 --- lib/NetworkManager.js | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/NetworkManager.js b/lib/NetworkManager.js index 4391c2ed..dce1a662 100644 --- a/lib/NetworkManager.js +++ b/lib/NetworkManager.js @@ -26,8 +26,6 @@ class NetworkManager extends EventEmitter { this._requestInterceptor = null; /* @type {!Map} */ this._idToRequest = new Map(); - /* @type {!Map} */ - 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; } }