Skip to content

Commit e6dc1f4

Browse files
committed
fix: don't depend on private node api for Timeout wrapper
NODE-2460
1 parent c4add7b commit e6dc1f4

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/core/topologies/shared.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,18 @@ function Interval(fn, time) {
213213

214214
function Timeout(fn, time) {
215215
var timer = false;
216+
var func = () => {
217+
if (timer) {
218+
clearTimeout(timer);
219+
timer = false;
220+
221+
fn();
222+
}
223+
};
216224

217225
this.start = function() {
218226
if (!this.isRunning()) {
219-
timer = setTimeout(fn, time);
227+
timer = setTimeout(func, time);
220228
}
221229
return this;
222230
};
@@ -228,7 +236,6 @@ function Timeout(fn, time) {
228236
};
229237

230238
this.isRunning = function() {
231-
if (timer && timer._called) return false;
232239
return timer !== false;
233240
};
234241
}

test/unit/core/replset/utils.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const Timeout = require('../../../../lib/core/topologies/shared').Timeout;
3+
const expect = require('chai').expect;
4+
5+
describe('', function() {
6+
it('should detect when a timer is finished running', function(done) {
7+
let timeout;
8+
function timeoutHandler() {
9+
expect(timeout.isRunning()).to.be.false;
10+
done();
11+
}
12+
13+
timeout = new Timeout(timeoutHandler, 100);
14+
timeout.start();
15+
});
16+
});

0 commit comments

Comments
 (0)