Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dereference timers in node.js so that the process may exit when finished #139

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class Backend {
this.options = { ...getDefaults(), ...(this.options || {}), ...options }
this.allOptions = allOptions
if (this.services && this.options.reloadInterval) {
setInterval(() => this.reload(), this.options.reloadInterval)
const timer = setInterval(() => this.reload(), this.options.reloadInterval)
if (typeof timer === 'object') {
timer.unref()
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"compile": "npm run compile:esm && npm run compile:cjs",
"browser": "browserify --ignore cross-fetch --standalone i18nextHttpBackend cjs/index.js -o i18nextHttpBackend.js && uglifyjs i18nextHttpBackend.js --compress --mangle -o i18nextHttpBackend.min.js",
"build": "npm run compile && npm run browser",
"test:xmlhttpreq": "mocha test -R spec --require test/fixtures/xmlHttpRequest.cjs --exit --experimental-modules",
"test:fetch": "mocha test -R spec --exit --experimental-modules",
"test:xmlhttpreq": "mocha test -R spec --require test/fixtures/xmlHttpRequest.cjs --experimental-modules",
"test:fetch": "mocha test -R spec --experimental-modules",
"test": "npm run lint && npm run build && npm run test:fetch && npm run test:xmlhttpreq && npm run test:typescript",
"test:typescript": "tslint --project tsconfig.json && tsd",
"test:deno": "deno test --allow-net test/deno/*.js",
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const server = (done) => {
if (js) return done(null, js)

js = jsonServer.create()
js.use((req, res, next)=> {
// disable keep alive so the test server can close quickly.
res.setHeader('Connection', 'close')
next()
})
js.use(jsonServer.bodyParser)

js.get('/locales/en/testqs', (req, res) => {
Expand Down Expand Up @@ -54,7 +59,7 @@ const server = (done) => {
js.listen(5001, () => {
console.log('JSON Server is running')
done(null, js)
})
}).unref()
}

export default server
4 changes: 2 additions & 2 deletions test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
errO = err
dataO = data
resolve(true)
setTimeout(() => reject(new Error('timeout')), 1500)
})
setTimeout(() => reject(new Error('timeout')), 1500).unref()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this line so that it behaves as a proper timeout check against the backend.read call.

})
// evaluate outside callback to get actuall error when something is wrong
expect(errO).to.be(null)
Expand Down Expand Up @@ -72,8 +72,8 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
errO = err
dataO = data
resolve(true)
setTimeout(() => reject(new Error('timeout')), 1500)
})
setTimeout(() => reject(new Error('timeout')), 1500).unref()
})
// evaluate outside callback to get actuall error when something is wrong
expect(errO).to.be(null)
Expand Down