diff --git a/global.js b/global.js new file mode 100644 index 0000000..aa81e26 --- /dev/null +++ b/global.js @@ -0,0 +1,10 @@ +const timers = require('.') + +global.setTimeout = timers.setTimeout +global.clearTimeout = timers.clearTimeout + +global.setInterval = timers.setInterval +global.clearInterval = timers.clearInterval + +global.setImmediate = timers.setImmediate +global.clearImmediate = timers.clearImmediate diff --git a/index.js b/index.js index e25f57c..9e985bb 100644 --- a/index.js +++ b/index.js @@ -371,23 +371,23 @@ function clearTimer(timer) { garbage++ } -function setTimeout(fn, ms, ...args) { +exports.setTimeout = function setTimeout(fn, ms, ...args) { return queueTimer(Math.floor(ms), false, fn, [...args]) } -function clearTimeout(timer) { +exports.clearTimeout = function clearTimeout(timer) { if (timer && timer._list !== null) clearTimer(timer) } -function setInterval(fn, ms, ...args) { +exports.setInterval = function setInterval(fn, ms, ...args) { return queueTimer(Math.floor(ms), true, fn, [...args]) } -function clearInterval(timer) { +exports.clearInterval = function clearInterval(timer) { if (timer && timer._list !== null) clearTimer(timer) } -function setImmediate(fn, ...args) { +exports.setImmediate = function setImmediate(fn, ...args) { if (typeof fn !== 'function') throw typeError('Callback must be a function', 'ERR_INVALID_CALLBACK') @@ -396,7 +396,7 @@ function setImmediate(fn, ...args) { return immediates.queue(false, Date.now(), fn, args) } -function clearImmediate(timer) { +exports.clearImmediate = function clearImmediate(timer) { if (timer && timer._list !== null) clearTimer(timer) } @@ -418,28 +418,3 @@ function typeError(message, code) { error.code = code return error } - -function* iterator() { - if (immediates.tail !== null) { - yield immediates.tail - for (let t = immediates.tail._next; t !== immediates.tail; t = t._next) - yield t - } - for (const list of timers.values()) { - if (list.tail === null) continue - yield list.tail - for (let t = list.tail._next; t !== list.tail; t = t._next) yield t - } -} - -module.exports = { - setTimeout, - clearTimeout, - setInterval, - clearInterval, - setImmediate, - clearImmediate, - [Symbol.iterator]() { - return iterator() - } -} diff --git a/package.json b/package.json index dfef04d..37dc210 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,14 @@ "name": "bare-timers", "version": "2.1.0", "description": "Native timers for Javascript", - "main": "index.js", + "exports": { + ".": "./index.js", + "./package": "./package.json", + "./global": "./global.js" + }, "files": [ "index.js", + "global.js", "binding.c", "binding.js", "CMakeLists.txt",