[enh] don't repeatedly alert about a failed state
This commit is contained in:
@@ -1,20 +1,25 @@
|
||||
export class Watcher {
|
||||
private _state: 'INIT'|'OK'|'FAILURE'|'RECOVERED'
|
||||
private _stateChanged: boolean
|
||||
private _error?: any
|
||||
|
||||
constructor(public readonly name: string) {
|
||||
this._stateChanged = false
|
||||
this._state = "INIT"
|
||||
}
|
||||
|
||||
public stateOK() {
|
||||
this._stateChanged = this._state !== "OK"
|
||||
this._state = "OK"
|
||||
}
|
||||
|
||||
public stateRecovered() {
|
||||
this._stateChanged = this._state !== "RECOVERED"
|
||||
this._state = "RECOVERED"
|
||||
}
|
||||
|
||||
public stateFailure(error: any) {
|
||||
this._stateChanged = this._state !== "FAILURE"
|
||||
this._state = "FAILURE"
|
||||
this._error = error
|
||||
}
|
||||
@@ -23,6 +28,10 @@ export class Watcher {
|
||||
return this._state
|
||||
}
|
||||
|
||||
public get stateChanged() {
|
||||
return this._stateChanged
|
||||
}
|
||||
|
||||
public get error() {
|
||||
return this._error
|
||||
}
|
||||
|
||||
@@ -47,7 +47,10 @@ export function watcherLoop(
|
||||
}
|
||||
// Wait before reconnecting
|
||||
const waitingDelay = reconnectionDelays[Math.min(reconnectionDelays.length - 1, i)]
|
||||
if (watcher.stateChanged) {
|
||||
// Notify only if state changed since
|
||||
await onDisconnection(waitingDelay, watcher.error)
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, waitingDelay))
|
||||
i++
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user