Skip to content

Commit

Permalink
fix: don't depend on private node api for Timeout wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 26, 2020
1 parent 582d3e2 commit 3ddaa3e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/core/topologies/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,18 @@ function Interval(fn, time) {

function Timeout(fn, time) {
var timer = false;
var func = () => {
if (timer) {
clearTimeout(timer);
timer = false;

fn();
}
};

this.start = function() {
if (!this.isRunning()) {
timer = setTimeout(fn, time);
timer = setTimeout(func, time);
}
return this;
};
Expand All @@ -226,7 +234,6 @@ function Timeout(fn, time) {
};

this.isRunning = function() {
if (timer && timer._called) return false;
return timer !== false;
};
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/core/replset/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const Timeout = require('../../../../lib/core/topologies/shared').Timeout;
const expect = require('chai').expect;

describe('', function() {
it('should detect when a timer is finished running', function(done) {
let timeout;
function timeoutHandler() {
expect(timeout.isRunning()).to.be.false;
done();
}

timeout = new Timeout(timeoutHandler, 100);
timeout.start();
});
});

0 comments on commit 3ddaa3e

Please sign in to comment.