This commit is contained in:
CortexCore
2025-07-11 11:45:45 +08:00
parent fc189b98cc
commit ecae0f809c
76 changed files with 237471 additions and 33136 deletions

View File

@@ -98,58 +98,63 @@ namespace BITKit.StateMachine
}
if (Equals(nextState, CurrentState)) return;
var tempState = CurrentState;
CurrentState = nextState;
_transitionCts?.Cancel();
_transitionCts = new CancellationTokenSource();
var ct = _transitionCts.Token;
await IsBusy;
using var _ = IsBusy.GetHandle();
if(ct.IsCancellationRequested||_cancellationTokenSource.IsCancellationRequested)return;
OnStateChanging?.Invoke(tempState,nextState);
if (tempState is not null)
try
{
if (_entryCompletedState == tempState)
CurrentState = nextState;
_transitionCts?.Cancel();
_transitionCts = new CancellationTokenSource();
var ct = _transitionCts.Token;
await IsBusy;
using var _ = IsBusy.GetHandle();
if(ct.IsCancellationRequested||_cancellationTokenSource.IsCancellationRequested)return;
OnStateChanging?.Invoke(tempState,nextState);
if (tempState is not null)
{
_entryCompletedState = null;
}
if (_entryCompletedState == tempState)
{
_entryCompletedState = null;
}
tempState.Enabled = false;
await tempState.OnStateExitAsync(tempState, nextState);
tempState.OnStateExit(tempState,nextState);
if(_cancellationTokenSource.IsCancellationRequested)return;
}
tempState.Enabled = false;
await tempState.OnStateExitAsync(tempState, nextState);
tempState.OnStateExit(tempState,nextState);
if(_cancellationTokenSource.IsCancellationRequested)return;
}
if(ct.IsCancellationRequested)return;
if(ct.IsCancellationRequested)return;
await OnStateChangeAsync.UniTaskFunc(CurrentState,nextState);
await OnStateChangeAsync.UniTaskFunc(CurrentState,nextState);
if(ct.IsCancellationRequested)return;
if(ct.IsCancellationRequested)return;
if (nextState is not null)
{
if (_isRegistered.Add(nextState.Identifier))
if (nextState is not null)
{
await RegisterAsync(nextState);
if(ct.IsCancellationRequested || _cancellationTokenSource.IsCancellationRequested)return;
}
if (_isRegistered.Add(nextState.Identifier))
{
await RegisterAsync(nextState);
if(ct.IsCancellationRequested || _cancellationTokenSource.IsCancellationRequested)return;
}
nextState.Enabled = true;
await nextState.OnStateEntryAsync(CurrentState);
nextState.OnStateEntry(CurrentState);
if(ct.IsCancellationRequested || _cancellationTokenSource.IsCancellationRequested)return;
nextState.Enabled = true;
await nextState.OnStateEntryAsync(CurrentState);
nextState.OnStateEntry(CurrentState);
if(ct.IsCancellationRequested || _cancellationTokenSource.IsCancellationRequested)return;
_entryCompletedState = nextState;
_entryCompletedState = nextState;
}
}
finally
{
OnStateChanged?.Invoke(tempState, nextState);
}
OnStateChanged?.Invoke(tempState, nextState);
}
public T TransitionState(T nextState)
@@ -163,7 +168,6 @@ namespace BITKit.StateMachine
StateDictionary.TryAdd(newState.GetType(),newState);
_dictionary.TryAdd(newState.Identifier, newState);
newState.Initialize();
await newState.InitializeAsync();
@@ -186,12 +190,19 @@ namespace BITKit.StateMachine
if (Equals(CurrentState, newState))
{
await CurrentState.OnStateExitAsync(CurrentState, null);
CurrentState.OnStateExit(CurrentState, null);
var tempState = CurrentState;
CurrentState = null;
OnStateChanging?.Invoke(tempState, null);
await tempState.OnStateExitAsync(tempState, null);
tempState.OnStateExit(tempState, null);
OnStateChanged?.Invoke(tempState,null);
}
// ReSharper disable once SuspiciousTypeConversion.Global
if (newState is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();