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