From 8ecca50268688885d84fbdf6bb7f754ac05e269b Mon Sep 17 00:00:00 2001 From: cgeek Date: Fri, 8 May 2020 11:15:09 +0200 Subject: [PATCH] [enh] don't repeatedly alert about a failed state --- src/lib/types/state.ts | 9 +++++++++ src/lib/watcherLoop.ts | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/types/state.ts b/src/lib/types/state.ts index 6446de5..dc6ccce 100644 --- a/src/lib/types/state.ts +++ b/src/lib/types/state.ts @@ -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 } diff --git a/src/lib/watcherLoop.ts b/src/lib/watcherLoop.ts index 4e1c83a..b38a4e9 100644 --- a/src/lib/watcherLoop.ts +++ b/src/lib/watcherLoop.ts @@ -47,7 +47,10 @@ export function watcherLoop( } // Wait before reconnecting const waitingDelay = reconnectionDelays[Math.min(reconnectionDelays.length - 1, i)] - await onDisconnection(waitingDelay, watcher.error) + if (watcher.stateChanged) { + // Notify only if state changed since + await onDisconnection(waitingDelay, watcher.error) + } await new Promise(resolve => setTimeout(resolve, waitingDelay)) i++ }