Skip to content

Commit f44ad1d

Browse files
Dereference timers in node.js so that the process may exit when finished (#139)
1 parent 6b18b6f commit f44ad1d

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ class Backend {
3838
this.options = { ...getDefaults(), ...(this.options || {}), ...options }
3939
this.allOptions = allOptions
4040
if (this.services && this.options.reloadInterval) {
41-
setInterval(() => this.reload(), this.options.reloadInterval)
41+
const timer = setInterval(() => this.reload(), this.options.reloadInterval)
42+
if (typeof timer === 'object') {
43+
timer.unref()
44+
}
4245
}
4346
}
4447

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
"compile": "npm run compile:esm && npm run compile:cjs",
7878
"browser": "browserify --ignore cross-fetch --standalone i18nextHttpBackend cjs/index.js -o i18nextHttpBackend.js && uglifyjs i18nextHttpBackend.js --compress --mangle -o i18nextHttpBackend.min.js",
7979
"build": "npm run compile && npm run browser",
80-
"test:xmlhttpreq": "mocha test -R spec --require test/fixtures/xmlHttpRequest.cjs --exit --experimental-modules",
81-
"test:fetch": "mocha test -R spec --exit --experimental-modules",
80+
"test:xmlhttpreq": "mocha test -R spec --require test/fixtures/xmlHttpRequest.cjs --experimental-modules",
81+
"test:fetch": "mocha test -R spec --experimental-modules",
8282
"test": "npm run lint && npm run build && npm run test:fetch && npm run test:xmlhttpreq && npm run test:typescript",
8383
"test:typescript": "tslint --project tsconfig.json && tsd",
8484
"test:deno": "deno test --allow-net test/deno/*.js",

test/fixtures/server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const server = (done) => {
66
if (js) return done(null, js)
77

88
js = jsonServer.create()
9+
js.use((req, res, next)=> {
10+
// disable keep alive so the test server can close quickly.
11+
res.setHeader('Connection', 'close')
12+
next()
13+
})
914
js.use(jsonServer.bodyParser)
1015

1116
js.get('/locales/en/testqs', (req, res) => {
@@ -54,7 +59,7 @@ const server = (done) => {
5459
js.listen(5001, () => {
5560
console.log('JSON Server is running')
5661
done(null, js)
57-
})
62+
}).unref()
5863
}
5964

6065
export default server

test/http.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
4141
errO = err
4242
dataO = data
4343
resolve(true)
44-
setTimeout(() => reject(new Error('timeout')), 1500)
4544
})
45+
setTimeout(() => reject(new Error('timeout')), 1500).unref()
4646
})
4747
// evaluate outside callback to get actuall error when something is wrong
4848
expect(errO).to.be(null)
@@ -72,8 +72,8 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
7272
errO = err
7373
dataO = data
7474
resolve(true)
75-
setTimeout(() => reject(new Error('timeout')), 1500)
7675
})
76+
setTimeout(() => reject(new Error('timeout')), 1500).unref()
7777
})
7878
// evaluate outside callback to get actuall error when something is wrong
7979
expect(errO).to.be(null)

0 commit comments

Comments
 (0)