Skip to content
This repository was archived by the owner on Jul 6, 2018. It is now read-only.

Commit 16e0c23

Browse files
committed
Removed most closures.
1 parent 99a32a9 commit 16e0c23

1 file changed

Lines changed: 49 additions & 55 deletions

File tree

lib/internal/http2.js

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -312,99 +312,79 @@ class Http2Stream extends Duplex {
312312
if (this._handle) {
313313
this._handle.setLocalWindowSize(size);
314314
} else {
315-
this.once('handle', () => {
316-
this._handle.setLocalWindowSize(size);
317-
});
315+
this.once('handle', this.setLocalWindowSize.bind(this, size));
318316
}
319317
}
320318

321319
changeStreamPriority(parentId, priority, exclusive) {
322320
if (this._handle) {
323321
this._handle.changeStreamPriority(parentId, priority, exclusive);
324322
} else {
325-
this.once('handle', () => {
326-
this._handle.changeStreamPriority(parentId, priority, exclusive);
327-
});
323+
this.once('handle', this.changeStreamPriority.bind(this, parentId, priority, exclusive));
328324
}
329325
}
330326

331327
respond() {
332328
if (this._handle) {
333329
this._handle.respond();
334330
} else {
335-
this.once('handle', () => {
336-
this._handle.respond();
337-
});
331+
this.once('handle', onHandleRespond);
338332
}
339333
}
340334

341335
resume() {
342336
if (this._handle) {
343337
this._handle.resume();
344338
} else {
345-
this.once('handle', () => {
346-
this._handle.resume();
347-
});
339+
this.once('handle', onHandleResume);
348340
}
349341
}
350342

351343
sendContinue() {
352344
if (this._handle) {
353345
this._handle.sendContinue();
354346
} else {
355-
this.once('handle', () => {
356-
this._handle.sendContinue();
357-
});
347+
this.once('handle', this.sendContinue.bind(this));
358348
}
359349
}
360350

361351
sendPriority(parentId, priority, exclusive) {
362352
if (this._handle) {
363353
this._handle.sendPriority(parentId, priority, exclusive);
364354
} else {
365-
this.once('handle', () => {
366-
this._handle.sendPriority(parentId, priority, exclusive);
367-
});
355+
this.once('handle', this.sendPriority.bind(this, parentId, priority, exclusive));
368356
}
369357
}
370358

371359
sendRstStream(code) {
372360
if (this._handle) {
373361
this._handle.sendRstStream(code);
374362
} else {
375-
this.once('handle', () => {
376-
this._handle.sendRstStream(code);
377-
});
363+
this.once('handle', this.sendRstStream.bind(this, code));
378364
}
379365
}
380366

381367
sendPushPromise(headers) {
382368
if (this._handle) {
383369
return this._handle.sendPushPromise(mapToHeaders(headers));
384370
} else {
385-
this.once('handle', () => {
386-
this._handle.sendPushPromise(mapToHeaders(headers));
387-
});
371+
this.once('handle', this.sendPushPromise.bind(this, headers));
388372
}
389373
}
390374

391375
addHeader(name, value, noindex) {
392376
if (this._handle) {
393377
this._handle.addHeader(name, value, noindex);
394378
} else {
395-
this.once('handle', () => {
396-
this._handle.addHeader(name, value, noindex);
397-
});
379+
this.once('handle', this.addHeader.bind(this, name, value, noindex));
398380
}
399381
}
400382

401383
addTrailer(name, value, noindex) {
402384
if (this._handle) {
403385
this._handle.addTrailer(name, value, noindex);
404386
} else {
405-
this.once('handle', () => {
406-
this._handle.addTrailer(name, value, noindex);
407-
});
387+
this.once('handle', this.addTrailer.bind(this, name, value, noindex));
408388
}
409389
}
410390

@@ -464,29 +444,41 @@ class Http2Stream extends Duplex {
464444
if (this._handle) {
465445
this._handle.finishedWriting();
466446
} else {
467-
this.on('handle', () => {
468-
this._handle.finishedWriting();
469-
});
447+
this.on('handle', onHandleFinishedWriting);
470448
}
471449
}
472450

473451
_read(n) {
474452
if (this._handle) {
475453
this._handle.readStart();
476454
} else {
477-
this.once('handle', () => {
478-
this._handle.readStart();
479-
});
455+
this.once('handle', onHandleReadStart);
480456
}
481457
}
482458
}
483459

460+
function onHandleReadStart() {
461+
this._handle.readStart();
462+
}
463+
464+
function onHandleFinishedWriting() {
465+
this._handle.finishedWriting();
466+
}
467+
484468
function onHandleWrite(data, encoding, cb) {
485469
return function onWriteFinished() {
486470
this._write(data, encoding, cb);
487471
};
488472
}
489473

474+
function onHandleRespond() {
475+
this._handle.respond();
476+
}
477+
478+
function onHandleResume() {
479+
this._handle.resume();
480+
}
481+
490482
class Http2Session extends EventEmitter {
491483
constructor(type, options, socket) {
492484
super();
@@ -495,6 +487,7 @@ class Http2Session extends EventEmitter {
495487
this[kHandle] = sessions.alloc();
496488
this[kHandle][kOwner] = this;
497489
this[kHandle].reinitialize(type, options, socket._handle._externalStream);
490+
this[kSocket] = socket;
498491
}
499492

500493
reset() {
@@ -1103,28 +1096,29 @@ function sessionOnStreamClose(stream, code) {
11031096
response[kStream] = undefined;
11041097
stream[kRequest] = undefined;
11051098
stream[kResponse] = undefined;
1106-
setImmediate(() => maybeDestroyStream(stream));
1099+
setImmediate(maybeDestroyStream, stream);
11071100
}
11081101

1109-
function sessionOnError(server, socket) {
1110-
function fn(error) {
1111-
if (server.listenerCount('sessionError') > 0) {
1112-
server.emit('sessionError', error);
1113-
return;
1114-
}
1115-
socket.destroy(error);
1102+
function sessionOnError() {
1103+
const session = this;
1104+
const server = session[kServer];
1105+
const socket = session[kSocket];
1106+
1107+
if (server.listenerCount('sessionError') > 0) {
1108+
server.emit('sessionError', error);
1109+
return;
11161110
}
1117-
return fn;
1111+
socket.destroy(error);
11181112
}
11191113

1120-
function socketOnTimeout(server, session) {
1121-
function fn() {
1122-
if (!server.emit('timeout', this)) {
1123-
// Session timed out, attempt a graceful exit
1124-
session.gracefulTerminate(() => this.destroy());
1125-
}
1114+
function socketOnTimeout() {
1115+
const socket = this;
1116+
const server = socket[kServer];
1117+
1118+
if (!server.emit('timeout', this)) {
1119+
// Session timed out, attempt a graceful exit
1120+
session.gracefulTerminate(this.destroy.bind(this));
11261121
}
1127-
return fn;
11281122
}
11291123

11301124
function socketOnceError(error) {
@@ -1248,7 +1242,7 @@ function connectionListener(socket) {
12481242
session[kServer] = this;
12491243
socket[kServer] = this;
12501244

1251-
session.on('error', sessionOnError(this, socket));
1245+
session.on('error', sessionOnError);
12521246

12531247
// Disable TLS Negotiation on this socket. The HTTP/2 allows renegotiation to
12541248
// happen up until the initial HTTP/2 session bootstrap. After that, it is
@@ -1259,7 +1253,7 @@ function connectionListener(socket) {
12591253
// Set up the timeout listener
12601254
if (this.timeout)
12611255
socket.setTimeout(this.timeout);
1262-
socket.on('timeout', socketOnTimeout(this, session));
1256+
socket.on('timeout', socketOnTimeout);
12631257

12641258
// Destroy the session if the socket is destroyed
12651259
const destroySocket = socket.destroy;
@@ -1442,7 +1436,7 @@ function clientSessionOnStreamClose(stream, code) {
14421436
response[kStream] = undefined;
14431437
stream[kRequest] = undefined;
14441438
stream[kResponse] = undefined;
1445-
setImmediate(() => maybeDestroyStream(stream));
1439+
setImmediate(maybeDestroyStream, stream);
14461440
}
14471441

14481442
function initializeClientOptions(options) {

0 commit comments

Comments
 (0)