From b11a6b0c0f2ed328f95df80c371cfe50ea5b6e01 Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Sun, 13 Nov 2016 12:23:06 +0800 Subject: [PATCH] strict check current to equla to target, or throw exception --- src/state-monitor.spec.ts | 10 ++++++++++ src/state-monitor.ts | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/state-monitor.spec.ts b/src/state-monitor.spec.ts index 7ce27930..1c77cadd 100644 --- a/src/state-monitor.spec.ts +++ b/src/state-monitor.spec.ts @@ -63,3 +63,13 @@ test('StateMonitor client & stable/inprocess', t => { t.true(sm.stable(), 'should be stable') t.false(sm.inprocess(), 'should be not inprocess') }) + +test('current() strict check with target', t => { + const CLIENT_NAME = 'StateMonitorTest' + const sm = new StateMonitor<'A', 'B'>(CLIENT_NAME, 'A') + + t.throws(() => sm.current('B'), Error, 'should thorw for unmatch current & target') + + sm.target('B') + t.notThrows(() => sm.current('B'), 'should not throws for matched current & target') +}) diff --git a/src/state-monitor.ts b/src/state-monitor.ts index eb1bb8b4..986a4abc 100644 --- a/src/state-monitor.ts +++ b/src/state-monitor.ts @@ -58,6 +58,20 @@ export class StateMonitor { , this._current, this._stable ) + /** + * strict check current is equal to target + */ + if (this._target !== newState) { + log.warn('StateMonitor', '%s:current(%s,%s) current is different with target. call state.target(%s) first.' + , this._client + , newState, stable + , newState + ) + const e = new Error('current not match target') + log.verbose('StateMonitor', e.stack) + throw e + } + /** * warn for inprocess current state change twice, mostly like a logic bug outside */ -- GitLab