Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const closeOrError = (errorText?: string) => {
if (errorText) {
console.log(`WebSocket Connection to Home Assistant closed with an error: ${errorText}`);
}
if (invalidAuth) {
promReject(ha.ERR_INVALID_AUTH);
return;
}
// Reject if we no longer have to retry
if (triesLeft === 0) {
// We never were connected and will not retry
promReject(ha.ERR_CANNOT_CONNECT);
return;
}
const newTries = triesLeft === -1 ? -1 : triesLeft - 1;
// Try again in a second
setTimeout(
() =>
connect(
newTries,
const onClose = () => {
// If we are in error handler make sure close handler doesn't also fire.
socket.removeEventListener('close', onClose);
if (invalidAuth) {
promReject(homeassistant.ERR_INVALID_AUTH);
return;
}
// Try again in a second
setTimeout(() => connect(promResolve, promReject), 5000);
};
const onClose = () => {
socket.removeListener('close', onClose);
if (invalidAuth) {
reject(HomeAssistant.ERR_INVALID_AUTH);
return;
}
if (triesLeft === 0) {
reject(HomeAssistant.ERR_CANNOT_CONNECT);
return;
}
const newTries = triesLeft === -1 ? -1 : triesLeft - 1;
// try again in a second
setTimeout(() => connect(newTries), 1000);
};
const onOpen = async () => {
const closeMessage = () => {
// If we are in error handler make sure close handler doesn't also fire.
socket.removeEventListener("close", closeMessage);
if (invalidAuth) {
promReject(ha.ERR_INVALID_AUTH);
return;
}
// Reject if we no longer have to retry
if (triesLeft === 0) {
// We never were connected and will not retry
promReject(ha.ERR_CANNOT_CONNECT);
return;
}
const newTries = triesLeft === -1 ? -1 : triesLeft - 1;
// Try again in a second
setTimeout(
() =>
connect(
newTries,