From 16e0c239bba5ee8426cbb22d7363fc74f2243f0b Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 5 Dec 2016 17:48:42 +0100 Subject: [PATCH] Removed most closures. --- lib/internal/http2.js | 104 ++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 55 deletions(-) diff --git a/lib/internal/http2.js b/lib/internal/http2.js index 4dcb360e88..fe11d96a42 100644 --- a/lib/internal/http2.js +++ b/lib/internal/http2.js @@ -312,9 +312,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.setLocalWindowSize(size); } else { - this.once('handle', () => { - this._handle.setLocalWindowSize(size); - }); + this.once('handle', this.setLocalWindowSize.bind(this, size)); } } @@ -322,9 +320,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.changeStreamPriority(parentId, priority, exclusive); } else { - this.once('handle', () => { - this._handle.changeStreamPriority(parentId, priority, exclusive); - }); + this.once('handle', this.changeStreamPriority.bind(this, parentId, priority, exclusive)); } } @@ -332,9 +328,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.respond(); } else { - this.once('handle', () => { - this._handle.respond(); - }); + this.once('handle', onHandleRespond); } } @@ -342,9 +336,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.resume(); } else { - this.once('handle', () => { - this._handle.resume(); - }); + this.once('handle', onHandleResume); } } @@ -352,9 +344,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.sendContinue(); } else { - this.once('handle', () => { - this._handle.sendContinue(); - }); + this.once('handle', this.sendContinue.bind(this)); } } @@ -362,9 +352,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.sendPriority(parentId, priority, exclusive); } else { - this.once('handle', () => { - this._handle.sendPriority(parentId, priority, exclusive); - }); + this.once('handle', this.sendPriority.bind(this, parentId, priority, exclusive)); } } @@ -372,9 +360,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.sendRstStream(code); } else { - this.once('handle', () => { - this._handle.sendRstStream(code); - }); + this.once('handle', this.sendRstStream.bind(this, code)); } } @@ -382,9 +368,7 @@ class Http2Stream extends Duplex { if (this._handle) { return this._handle.sendPushPromise(mapToHeaders(headers)); } else { - this.once('handle', () => { - this._handle.sendPushPromise(mapToHeaders(headers)); - }); + this.once('handle', this.sendPushPromise.bind(this, headers)); } } @@ -392,9 +376,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.addHeader(name, value, noindex); } else { - this.once('handle', () => { - this._handle.addHeader(name, value, noindex); - }); + this.once('handle', this.addHeader.bind(this, name, value, noindex)); } } @@ -402,9 +384,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.addTrailer(name, value, noindex); } else { - this.once('handle', () => { - this._handle.addTrailer(name, value, noindex); - }); + this.once('handle', this.addTrailer.bind(this, name, value, noindex)); } } @@ -464,9 +444,7 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.finishedWriting(); } else { - this.on('handle', () => { - this._handle.finishedWriting(); - }); + this.on('handle', onHandleFinishedWriting); } } @@ -474,19 +452,33 @@ class Http2Stream extends Duplex { if (this._handle) { this._handle.readStart(); } else { - this.once('handle', () => { - this._handle.readStart(); - }); + this.once('handle', onHandleReadStart); } } } +function onHandleReadStart() { + this._handle.readStart(); +} + +function onHandleFinishedWriting() { + this._handle.finishedWriting(); +} + function onHandleWrite(data, encoding, cb) { return function onWriteFinished() { this._write(data, encoding, cb); }; } +function onHandleRespond() { + this._handle.respond(); +} + +function onHandleResume() { + this._handle.resume(); +} + class Http2Session extends EventEmitter { constructor(type, options, socket) { super(); @@ -495,6 +487,7 @@ class Http2Session extends EventEmitter { this[kHandle] = sessions.alloc(); this[kHandle][kOwner] = this; this[kHandle].reinitialize(type, options, socket._handle._externalStream); + this[kSocket] = socket; } reset() { @@ -1103,28 +1096,29 @@ function sessionOnStreamClose(stream, code) { response[kStream] = undefined; stream[kRequest] = undefined; stream[kResponse] = undefined; - setImmediate(() => maybeDestroyStream(stream)); + setImmediate(maybeDestroyStream, stream); } -function sessionOnError(server, socket) { - function fn(error) { - if (server.listenerCount('sessionError') > 0) { - server.emit('sessionError', error); - return; - } - socket.destroy(error); +function sessionOnError() { + const session = this; + const server = session[kServer]; + const socket = session[kSocket]; + + if (server.listenerCount('sessionError') > 0) { + server.emit('sessionError', error); + return; } - return fn; + socket.destroy(error); } -function socketOnTimeout(server, session) { - function fn() { - if (!server.emit('timeout', this)) { - // Session timed out, attempt a graceful exit - session.gracefulTerminate(() => this.destroy()); - } +function socketOnTimeout() { + const socket = this; + const server = socket[kServer]; + + if (!server.emit('timeout', this)) { + // Session timed out, attempt a graceful exit + session.gracefulTerminate(this.destroy.bind(this)); } - return fn; } function socketOnceError(error) { @@ -1248,7 +1242,7 @@ function connectionListener(socket) { session[kServer] = this; socket[kServer] = this; - session.on('error', sessionOnError(this, socket)); + session.on('error', sessionOnError); // Disable TLS Negotiation on this socket. The HTTP/2 allows renegotiation to // happen up until the initial HTTP/2 session bootstrap. After that, it is @@ -1259,7 +1253,7 @@ function connectionListener(socket) { // Set up the timeout listener if (this.timeout) socket.setTimeout(this.timeout); - socket.on('timeout', socketOnTimeout(this, session)); + socket.on('timeout', socketOnTimeout); // Destroy the session if the socket is destroyed const destroySocket = socket.destroy; @@ -1442,7 +1436,7 @@ function clientSessionOnStreamClose(stream, code) { response[kStream] = undefined; stream[kRequest] = undefined; stream[kResponse] = undefined; - setImmediate(() => maybeDestroyStream(stream)); + setImmediate(maybeDestroyStream, stream); } function initializeClientOptions(options) {