diff --git a/index.js b/index.js index 57d8261..d50336f 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) decRef() + if (this._refed === true) this.unref() 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) decRef() + if (this._refed === true) this.unref() this._list = null maybeUpdateTimer() diff --git a/test/unref.js b/test/unref.js new file mode 100644 index 0000000..b389b8a --- /dev/null +++ b/test/unref.js @@ -0,0 +1,15 @@ +const test = require('brittle') +const timers = require('..') + +test('unref and a timer stays alive', async function (t) { + t.plan(1) + + const unreffed = timers.setTimeout(run, 10) + + function run () { + unreffed.unref() + timers.setTimeout(function () { + t.pass('timer triggered') + }, 50) + } +})