Created
February 27, 2018 23:05
-
-
Save rodrigomf24/e89785f8cf735b7348bfa7e0fe99f4fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getTimeoutFor = function(timeoutSettings) { | |
return timeoutSettings.divider * timeoutSettings.times; | |
}; | |
const timedCondition = function (client, done, context) { | |
const timeoutFor = getTimeoutFor(context.timeoutSettings); | |
setTimeout(function() { | |
context.condition(client).then(function(result) { | |
if (result.state !== 'success' && context.timeoutSettings.times > 0) { | |
context.timeoutSettings.times -= 1; | |
if (context.timeoutSettings.times <= 0) { | |
done(result); | |
} else { | |
timedCondition(client, done, context); | |
} | |
} else { | |
done(result); | |
} | |
}); | |
}, timeoutFor); | |
}; | |
const getTimeoutSettings = function(milliseconds) { | |
let times = 0; | |
if (milliseconds > 0 && milliseconds < 1000) { | |
times = milliseconds / 100; | |
} else { | |
times = milliseconds / 1000; | |
} | |
return { | |
times: Math.ceil(times), | |
divider: 100, | |
}; | |
}; | |
exports.command = function waitForCondition(condition, milliseconds) { | |
const timeoutSettings = getTimeoutSettings(milliseconds); | |
const contextToBind = { | |
context: {condition, timeoutSettings}, | |
helpers: {timedCondition, getTimeoutFor}, | |
}; | |
return this.perform(function(client, done) { | |
timedCondition(client, done, contextToBind.context); | |
// afterExecuteAsync.bind({callback, milliseconds}) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment