Skip to content

Commit c46611c

Browse files
refactor: remove "self" references
1 parent dcb85e9 commit c46611c

File tree

5 files changed

+127
-168
lines changed

5 files changed

+127
-168
lines changed

lib/socket.js

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ class Socket extends Emitter {
172172
transport = "websocket";
173173
} else if (0 === this.transports.length) {
174174
// Emit error on next tick so it can be listened to
175-
const self = this;
176-
setTimeout(function() {
177-
self.emit("error", "No transports available");
175+
setTimeout(() => {
176+
this.emit("error", "No transports available");
178177
}, 0);
179178
return;
180179
} else {
@@ -203,7 +202,6 @@ class Socket extends Emitter {
203202
*/
204203
setTransport(transport) {
205204
debug("setting transport %s", transport.name);
206-
const self = this;
207205

208206
if (this.transport) {
209207
debug("clearing existing transport %s", this.transport.name);
@@ -215,17 +213,11 @@ class Socket extends Emitter {
215213

216214
// set up transport listeners
217215
transport
218-
.on("drain", function() {
219-
self.onDrain();
220-
})
221-
.on("packet", function(packet) {
222-
self.onPacket(packet);
223-
})
224-
.on("error", function(e) {
225-
self.onError(e);
226-
})
227-
.on("close", function() {
228-
self.onClose("transport close");
216+
.on("drain", this.onDrain.bind(this))
217+
.on("packet", this.onPacket.bind(this))
218+
.on("error", this.onError.bind(this))
219+
.on("close", () => {
220+
this.onClose("transport close");
229221
});
230222
}
231223

@@ -239,52 +231,46 @@ class Socket extends Emitter {
239231
debug('probing transport "%s"', name);
240232
let transport = this.createTransport(name, { probe: 1 });
241233
let failed = false;
242-
const self = this;
243234

244235
Socket.priorWebsocketSuccess = false;
245236

246-
function onTransportOpen() {
247-
if (self.onlyBinaryUpgrades) {
248-
const upgradeLosesBinary =
249-
!this.supportsBinary && self.transport.supportsBinary;
250-
failed = failed || upgradeLosesBinary;
251-
}
237+
const onTransportOpen = () => {
252238
if (failed) return;
253239

254240
debug('probe transport "%s" opened', name);
255241
transport.send([{ type: "ping", data: "probe" }]);
256-
transport.once("packet", function(msg) {
242+
transport.once("packet", msg => {
257243
if (failed) return;
258244
if ("pong" === msg.type && "probe" === msg.data) {
259245
debug('probe transport "%s" pong', name);
260-
self.upgrading = true;
261-
self.emit("upgrading", transport);
246+
this.upgrading = true;
247+
this.emit("upgrading", transport);
262248
if (!transport) return;
263249
Socket.priorWebsocketSuccess = "websocket" === transport.name;
264250

265-
debug('pausing current transport "%s"', self.transport.name);
266-
self.transport.pause(function() {
251+
debug('pausing current transport "%s"', this.transport.name);
252+
this.transport.pause(() => {
267253
if (failed) return;
268-
if ("closed" === self.readyState) return;
254+
if ("closed" === this.readyState) return;
269255
debug("changing transport and sending upgrade packet");
270256

271257
cleanup();
272258

273-
self.setTransport(transport);
259+
this.setTransport(transport);
274260
transport.send([{ type: "upgrade" }]);
275-
self.emit("upgrade", transport);
261+
this.emit("upgrade", transport);
276262
transport = null;
277-
self.upgrading = false;
278-
self.flush();
263+
this.upgrading = false;
264+
this.flush();
279265
});
280266
} else {
281267
debug('probe transport "%s" failed', name);
282268
const err = new Error("probe error");
283269
err.transport = transport.name;
284-
self.emit("upgradeError", err);
270+
this.emit("upgradeError", err);
285271
}
286272
});
287-
}
273+
};
288274

289275
function freezeTransport() {
290276
if (failed) return;
@@ -299,16 +285,16 @@ class Socket extends Emitter {
299285
}
300286

301287
// Handle any error that happens while probing
302-
function onerror(err) {
288+
const onerror = err => {
303289
const error = new Error("probe error: " + err);
304290
error.transport = transport.name;
305291

306292
freezeTransport();
307293

308294
debug('probe transport "%s" failed because of error: %s', name, err);
309295

310-
self.emit("upgradeError", error);
311-
}
296+
this.emit("upgradeError", error);
297+
};
312298

313299
function onTransportClose() {
314300
onerror("transport closed");
@@ -328,13 +314,13 @@ class Socket extends Emitter {
328314
}
329315

330316
// Remove all listeners on the transport and on self
331-
function cleanup() {
317+
const cleanup = () => {
332318
transport.removeListener("open", onTransportOpen);
333319
transport.removeListener("error", onerror);
334320
transport.removeListener("close", onTransportClose);
335-
self.removeListener("close", onclose);
336-
self.removeListener("upgrading", onupgrade);
337-
}
321+
this.removeListener("close", onclose);
322+
this.removeListener("upgrading", onupgrade);
323+
};
338324

339325
transport.once("open", onTransportOpen);
340326
transport.once("error", onerror);
@@ -557,13 +543,29 @@ class Socket extends Emitter {
557543
* @api private
558544
*/
559545
close() {
560-
const self = this;
546+
const close = () => {
547+
this.onClose("forced close");
548+
debug("socket closing - telling transport to close");
549+
this.transport.close();
550+
};
551+
552+
const cleanupAndClose = () => {
553+
this.removeListener("upgrade", cleanupAndClose);
554+
this.removeListener("upgradeError", cleanupAndClose);
555+
close();
556+
};
557+
558+
const waitForUpgrade = () => {
559+
// wait for upgrade to finish since we can't send packets while pausing a transport
560+
this.once("upgrade", cleanupAndClose);
561+
this.once("upgradeError", cleanupAndClose);
562+
};
561563

562564
if ("opening" === this.readyState || "open" === this.readyState) {
563565
this.readyState = "closing";
564566

565567
if (this.writeBuffer.length) {
566-
this.once("drain", function() {
568+
this.once("drain", () => {
567569
if (this.upgrading) {
568570
waitForUpgrade();
569571
} else {
@@ -577,24 +579,6 @@ class Socket extends Emitter {
577579
}
578580
}
579581

580-
function close() {
581-
self.onClose("forced close");
582-
debug("socket closing - telling transport to close");
583-
self.transport.close();
584-
}
585-
586-
function cleanupAndClose() {
587-
self.removeListener("upgrade", cleanupAndClose);
588-
self.removeListener("upgradeError", cleanupAndClose);
589-
close();
590-
}
591-
592-
function waitForUpgrade() {
593-
// wait for upgrade to finish since we can't send packets while pausing a transport
594-
self.once("upgrade", cleanupAndClose);
595-
self.once("upgradeError", cleanupAndClose);
596-
}
597-
598582
return this;
599583
}
600584

@@ -622,7 +606,6 @@ class Socket extends Emitter {
622606
"closing" === this.readyState
623607
) {
624608
debug('socket close with reason: "%s"', reason);
625-
const self = this;
626609

627610
// clear timers
628611
clearTimeout(this.pingIntervalTimer);
@@ -652,8 +635,8 @@ class Socket extends Emitter {
652635

653636
// clean buffers after, so users can still
654637
// grab the buffers on `close` event
655-
self.writeBuffer = [];
656-
self.prevBufferLen = 0;
638+
this.writeBuffer = [];
639+
this.prevBufferLen = 0;
657640
}
658641
}
659642

lib/transports/polling-jsonp.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ class JSONPPolling extends Polling {
3333
this.index = callbacks.length;
3434

3535
// add callback to jsonp global
36-
const self = this;
37-
callbacks.push(function(msg) {
38-
self.onData(msg);
39-
});
36+
callbacks.push(this.onData.bind(this));
4037

4138
// append to query string
4239
this.query.j = this.index;
@@ -77,7 +74,6 @@ class JSONPPolling extends Polling {
7774
* @api private
7875
*/
7976
doPoll() {
80-
const self = this;
8177
const script = document.createElement("script");
8278

8379
if (this.script) {
@@ -87,8 +83,8 @@ class JSONPPolling extends Polling {
8783

8884
script.async = true;
8985
script.src = this.uri();
90-
script.onerror = function(e) {
91-
self.onError("jsonp poll error", e);
86+
script.onerror = e => {
87+
this.onError("jsonp poll error", e);
9288
};
9389

9490
const insertAt = document.getElementsByTagName("script")[0];
@@ -119,7 +115,6 @@ class JSONPPolling extends Polling {
119115
* @api private
120116
*/
121117
doWrite(data, fn) {
122-
const self = this;
123118
let iframe;
124119

125120
if (!this.form) {
@@ -149,30 +144,30 @@ class JSONPPolling extends Polling {
149144
fn();
150145
}
151146

152-
function initIframe() {
153-
if (self.iframe) {
147+
const initIframe = () => {
148+
if (this.iframe) {
154149
try {
155-
self.form.removeChild(self.iframe);
150+
this.form.removeChild(this.iframe);
156151
} catch (e) {
157-
self.onError("jsonp polling iframe removal error", e);
152+
this.onError("jsonp polling iframe removal error", e);
158153
}
159154
}
160155

161156
try {
162157
// ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
163-
const html = '<iframe src="javascript:0" name="' + self.iframeId + '">';
158+
const html = '<iframe src="javascript:0" name="' + this.iframeId + '">';
164159
iframe = document.createElement(html);
165160
} catch (e) {
166161
iframe = document.createElement("iframe");
167-
iframe.name = self.iframeId;
162+
iframe.name = this.iframeId;
168163
iframe.src = "javascript:0";
169164
}
170165

171-
iframe.id = self.iframeId;
166+
iframe.id = this.iframeId;
172167

173-
self.form.appendChild(iframe);
174-
self.iframe = iframe;
175-
}
168+
this.form.appendChild(iframe);
169+
this.iframe = iframe;
170+
};
176171

177172
initIframe();
178173

@@ -187,7 +182,7 @@ class JSONPPolling extends Polling {
187182

188183
if (this.iframe.attachEvent) {
189184
this.iframe.onreadystatechange = function() {
190-
if (self.iframe.readyState === "complete") {
185+
if (this.iframe.readyState === "complete") {
191186
complete();
192187
}
193188
};

lib/transports/polling-xhr.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ class XHR extends Polling {
7474
method: "POST",
7575
data: data
7676
});
77-
const self = this;
7877
req.on("success", fn);
79-
req.on("error", function(err) {
80-
self.onError("xhr post error", err);
78+
req.on("error", err => {
79+
this.onError("xhr post error", err);
8180
});
8281
}
8382

@@ -89,12 +88,9 @@ class XHR extends Polling {
8988
doPoll() {
9089
debug("xhr poll");
9190
const req = this.request();
92-
const self = this;
93-
req.on("data", function(data) {
94-
self.onData(data);
95-
});
96-
req.on("error", function(err) {
97-
self.onError("xhr poll error", err);
91+
req.on("data", this.onData.bind(this));
92+
req.on("error", err => {
93+
this.onError("xhr poll error", err);
9894
});
9995
this.pollXhr = req;
10096
}
@@ -142,7 +138,6 @@ class Request extends Emitter {
142138
opts.xscheme = !!this.opts.xs;
143139

144140
const xhr = (this.xhr = new XMLHttpRequest(opts));
145-
const self = this;
146141

147142
try {
148143
debug("xhr open %s: %s", this.method, this.uri);
@@ -178,22 +173,22 @@ class Request extends Emitter {
178173
}
179174

180175
if (this.hasXDR()) {
181-
xhr.onload = function() {
182-
self.onLoad();
176+
xhr.onload = () => {
177+
this.onLoad();
183178
};
184-
xhr.onerror = function() {
185-
self.onError(xhr.responseText);
179+
xhr.onerror = () => {
180+
this.onError(xhr.responseText);
186181
};
187182
} else {
188-
xhr.onreadystatechange = function() {
183+
xhr.onreadystatechange = () => {
189184
if (4 !== xhr.readyState) return;
190185
if (200 === xhr.status || 1223 === xhr.status) {
191-
self.onLoad();
186+
this.onLoad();
192187
} else {
193188
// make sure the `error` event handler that's user-set
194189
// does not throw in the same tick and gets caught here
195-
setTimeout(function() {
196-
self.onError(typeof xhr.status === "number" ? xhr.status : 0);
190+
setTimeout(() => {
191+
this.onError(typeof xhr.status === "number" ? xhr.status : 0);
197192
}, 0);
198193
}
199194
};
@@ -205,8 +200,8 @@ class Request extends Emitter {
205200
// Need to defer since .create() is called directly from the constructor
206201
// and thus the 'error' event can only be only bound *after* this exception
207202
// occurs. Therefore, also, we cannot throw here at all.
208-
setTimeout(function() {
209-
self.onError(e);
203+
setTimeout(() => {
204+
this.onError(e);
210205
}, 0);
211206
return;
212207
}

0 commit comments

Comments
 (0)