Skip to content

Commit 3ddaa3e

Browse files
committed
fix: don't depend on private node api for Timeout wrapper
NODE-2460
1 parent 582d3e2 commit 3ddaa3e

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
@@ -211,10 +211,18 @@ function Interval(fn, time) {
211211

212212
function Timeout(fn, time) {
213213
var timer = false;
214+
var func = () => {
215+
if (timer) {
216+
clearTimeout(timer);
217+
timer = false;
218+
219+
fn();
220+
}
221+
};
214222

215223
this.start = function() {
216224
if (!this.isRunning()) {
217-
timer = setTimeout(fn, time);
225+
timer = setTimeout(func, time);
218226
}
219227
return this;
220228
};
@@ -226,7 +234,6 @@ function Timeout(fn, time) {
226234
};
227235

228236
this.isRunning = function() {
229-
if (timer && timer._called) return false;
230237
return timer !== false;
231238
};
232239
}

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)