Skip to content

Commit eaf0186

Browse files
authoredJun 25, 2023
refactor to es-class (#712)
1 parent e1e62d9 commit eaf0186

File tree

5 files changed

+914
-901
lines changed

5 files changed

+914
-901
lines changed
 

‎lib/channel.js

+352-345
Large diffs are not rendered by default.

‎lib/channel_model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Channel.prototype.prefetch = Channel.prototype.qos
264264
class ConfirmChannel extends Channel {
265265
publish(exchange, routingKey, content, options, cb) {
266266
this.pushConfirmCallback(cb);
267-
return Channel.prototype.publish.call(this, exchange, routingKey, content, options);
267+
return super.publish(exchange, routingKey, content, options);
268268
}
269269

270270
sendToQueue(queue, content, options, cb) {

‎lib/connection.js

+530-526
Large diffs are not rendered by default.

‎lib/format.js

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
var defs = require('./defs');
1010
var format = require('util').format;
11-
var inherits = require('util').inherits;
1211
var HEARTBEAT = require('./frame').HEARTBEAT;
1312

1413
module.exports.closeMessage = function(close) {

‎lib/heartbeat.js

+31-28
Original file line numberDiff line numberDiff line change
@@ -47,43 +47,46 @@
4747

4848
'use strict';
4949

50-
var inherits = require('util').inherits;
5150
var EventEmitter = require('events').EventEmitter;
5251

5352
// Exported so that we can mess with it in tests
5453
module.exports.UNITS_TO_MS = 1000;
5554

56-
function Heart(interval, checkSend, checkRecv) {
57-
EventEmitter.call(this);
58-
this.interval = interval;
55+
class Heart extends EventEmitter {
56+
constructor (interval, checkSend, checkRecv) {
57+
super();
5958

60-
var intervalMs = interval * module.exports.UNITS_TO_MS;
61-
// Function#bind is my new best friend
62-
var beat = this.emit.bind(this, 'beat');
63-
var timeout = this.emit.bind(this, 'timeout');
59+
this.interval = interval;
6460

65-
this.sendTimer = setInterval(
66-
this.runHeartbeat.bind(this, checkSend, beat), intervalMs / 2);
61+
var intervalMs = interval * module.exports.UNITS_TO_MS;
62+
// Function#bind is my new best friend
63+
var beat = this.emit.bind(this, 'beat');
64+
var timeout = this.emit.bind(this, 'timeout');
6765

68-
// A timeout occurs if I see nothing for *two consecutive* intervals
69-
var recvMissed = 0;
70-
function missedTwo() {
71-
if (!checkRecv()) return (++recvMissed < 2);
72-
else { recvMissed = 0; return true; }
66+
this.sendTimer = setInterval(
67+
this.runHeartbeat.bind(this, checkSend, beat), intervalMs / 2);
68+
69+
// A timeout occurs if I see nothing for *two consecutive* intervals
70+
var recvMissed = 0;
71+
function missedTwo () {
72+
if (!checkRecv())
73+
return (++recvMissed < 2);
74+
else { recvMissed = 0; return true; }
75+
}
76+
this.recvTimer = setInterval(
77+
this.runHeartbeat.bind(this, missedTwo, timeout), intervalMs);
7378
}
74-
this.recvTimer = setInterval(
75-
this.runHeartbeat.bind(this, missedTwo, timeout), intervalMs);
76-
}
77-
inherits(Heart, EventEmitter);
7879

79-
module.exports.Heart = Heart;
80+
clear () {
81+
clearInterval(this.sendTimer);
82+
clearInterval(this.recvTimer);
83+
}
8084

81-
Heart.prototype.clear = function() {
82-
clearInterval(this.sendTimer);
83-
clearInterval(this.recvTimer);
84-
};
85+
runHeartbeat (check, fail) {
86+
// Have we seen activity?
87+
if (!check())
88+
fail();
89+
}
90+
}
8591

86-
Heart.prototype.runHeartbeat = function(check, fail) {
87-
// Have we seen activity?
88-
if (!check()) fail();
89-
};
92+
module.exports.Heart = Heart;

0 commit comments

Comments
 (0)
Please sign in to comment.