Skip to content

Commit

Permalink
Fix error clearing timeout
Browse files Browse the repository at this point in the history
Closes #60
  • Loading branch information
rhys-vdw committed Mar 8, 2018
1 parent 409eef9 commit 0d195c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ function transformSource (runner, config, source, map, callback) {
),
{ stdio: ['pipe', 'pipe', process.stderr] }
)
if (config.timeoutMs) {
var cancelTimeout = setTimeout(function () {
child.kill()
}, config.timeoutMs)
}
var timeoutId = config.timeoutMs
? setTimeout(function () { child.kill() }, config.timeoutMs)
: -1

var dataBuffers = []
child.stdout.on('data', function (data) {
Expand All @@ -97,7 +95,9 @@ function transformSource (runner, config, source, map, callback) {
var sourceRegex = new RegExp(ioDelimiter + '([\\s\\S]+)' + ioDelimiter)
var matches = dataBuffers.join('').match(sourceRegex)
var transformedSource = matches && matches[1]
if (config.timeoutMs) { cancelTimeout() }
if (timeoutId !== -1) {
clearTimeout(timeoutId)
}
callback(null, transformedSource, map)
} else if (child.killed) {
callback(new Error(
Expand Down
9 changes: 5 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ function compile (config, callback) {
{
test: /\.erb$/,
loader: './index',
options: defaults({
dependenciesRoot: './test/dependencies'
}, config)
options: defaults({}, config, {
dependenciesRoot: './test/dependencies',
timeoutMs: 2000
})
}
]
},
Expand Down Expand Up @@ -109,7 +110,7 @@ test('times out with error (timeoutMs: 1000)', function (done) {
})

test('times out with error (DEPRECATED timeout: 1)', function (done) {
compile2({ file: 'sleep.js.erb', timeout: 1 }, done, function (stats) {
compile2({ file: 'sleep.js.erb', timeout: 1, timeoutMs: null }, done, function (stats) {
expect(stats.compilation.errors[0].message).toMatch(
'rails-erb-loader took longer than the specified 1000ms timeout'
)
Expand Down

0 comments on commit 0d195c5

Please sign in to comment.