diff --git a/index.js b/index.js index d50336f..7136eda 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,7 @@ class Timer { this._expiry = now + this._list.ms this._list.push(this) } else { - if (this._refed === true) this.unref() + if (this._refed === true) decRef() this._list = null } // apply at the bottom to avoid re-entries... @@ -36,7 +36,7 @@ class Timer { _clear () { if (this._list === null) return this._list.clear(this) - if (this._refed === true) this.unref() + if (this._refed === true) decRef() this._list = null maybeUpdateTimer() @@ -57,14 +57,14 @@ class Timer { } unref () { - if (this._refed === false) return this + if (this._refed === false || this._list === null) return this this._refed = false decRef() return this } ref () { - if (this._refed === true) return this + if (this._refed === true || this._list === null) return this this._refed = true incRef() return this diff --git a/test/all.js b/test/all.js index 669bce3..964e53f 100644 --- a/test/all.js +++ b/test/all.js @@ -1,3 +1,4 @@ require('./immediate') require('./interval') require('./timeout') +require('./ref') // has to be last one diff --git a/test/unref.js b/test/ref.js similarity index 50% rename from test/unref.js rename to test/ref.js index b389b8a..93b5e59 100644 --- a/test/unref.js +++ b/test/ref.js @@ -13,3 +13,17 @@ test('unref and a timer stays alive', async function (t) { }, 50) } }) + +// must be last test! +test('ref in callbacks are noops', async function (t) { + t.plan(1) + + const timer = timers.setTimeout(run, 10) + + function run () { + timer.ref() + const other = timers.setTimeout(() => t.fail('should not run'), 10_000) + other.unref() + t.pass('timer triggered') + } +})