Skip to content

Commit

Permalink
Add bare-timers/global
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Nov 23, 2024
1 parent d30cd87 commit 53b1a97
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
10 changes: 10 additions & 0 deletions global.js
Original file line number Diff line number Diff line change
@@ -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
37 changes: 6 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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)
}

Expand All @@ -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()
}
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 53b1a97

Please sign in to comment.