Skip to content

Commit

Permalink
fix(start-checking): Ensure that state accepted by check()
Browse files Browse the repository at this point in the history
Change start-checking.js to ensure the minimum requirements on the state
object are met before passing through to check().
  • Loading branch information
minrwhite authored and gr2m committed Nov 15, 2016
1 parent f3ceb0c commit 3961c76
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/start-checking.js
Expand Up @@ -6,12 +6,16 @@ internals.getOk = require('./get-ok')

function startChecking (state, options) {
options = parse(options)
handleInterval(state, options)
state !== undefined && state.method !== undefined && state.url !== undefined && handleInterval(state, options)
}

function handleInterval (state, options) {
var timeout
var ok = internals.getOk(state)
var timeoutHandler = function () {
var checkAgain = handleInterval.bind(null, state, options)
internals.check(state, options).then(checkAgain, checkAgain)
}

if (options.checkTimeout) {
timeout = options.checkTimeout
Expand All @@ -31,18 +35,12 @@ function handleInterval (state, options) {
// we use setTimeout on purpose, we don't want to send requests each
// x seconds, but rather set a timeout for x seconds after each response
// but we use `checkTimeout` as variable as the effect is the same
state.checkTimeout = setTimeout(function () {
var checkAgain = handleInterval.bind(null, state, options)
internals.check(state, options).then(checkAgain, checkAgain)
}, timeout)
state.checkTimeout = setTimeout(timeoutHandler, timeout)
return
}

options.interval = 30000
state.checkTimeout = setTimeout(function () {
var checkAgain = handleInterval.bind(null, state, options)
internals.check(state, options).then(checkAgain, checkAgain)
}, 0)
state.checkTimeout = setTimeout(timeoutHandler, 0)
}

function parse (options) {
Expand Down

0 comments on commit 3961c76

Please sign in to comment.