-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete rewrite - abstracted, reduced duplication, and modularized b…
…ase for futher work
- Loading branch information
1 parent
04b6582
commit ebdf2aa
Showing
35 changed files
with
825 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Collections.Generic; | ||
using LiquidState.Core; | ||
using LiquidState.Synchronous.Core; | ||
|
||
namespace LiquidState.Awaitable.Core | ||
{ | ||
public abstract class AbstractStateMachine<TState, TTrigger> : AbstractStateMachineCore<TState, TTrigger> | ||
{ | ||
protected internal StateRepresentation<TState, TTrigger> CurrentStateRepresentation; | ||
protected internal Dictionary<TState, StateRepresentation<TState, TTrigger>> Representations; | ||
|
||
public abstract void Fire<TArgument>(ParameterizedTrigger<TTrigger, TArgument> parameterizedTrigger, | ||
TArgument argument); | ||
|
||
public abstract void Fire(TTrigger trigger); | ||
public abstract void MoveToState(TState state, StateTransitionOption option = StateTransitionOption.Default); | ||
|
||
public override TState CurrentState | ||
{ | ||
get { return CurrentStateRepresentation.State; } | ||
} | ||
|
||
public override IEnumerable<TTrigger> CurrentPermittedTriggers | ||
{ | ||
get | ||
{ | ||
foreach (var triggerRepresentation in CurrentStateRepresentation.Triggers) | ||
{ | ||
yield return triggerRepresentation.Trigger; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using LiquidState.Common; | ||
|
||
namespace LiquidState.Core | ||
{ | ||
public abstract class AbstractStateMachineCore<TState, TTrigger> : IStateMachineCore<TState, TTrigger> | ||
{ | ||
public event Action<TriggerStateEventArgs<TState, TTrigger>> UnhandledTrigger; | ||
public event Action<TransitionEventArgs<TState, TTrigger>> InvalidState; | ||
public event Action<TransitionEventArgs<TState, TTrigger>> TransitionStarted; | ||
public event Action<TransitionExecutedEventArgs<TState, TTrigger>> TransitionExecuted; | ||
|
||
public void Pause() | ||
{ | ||
Interlocked.Exchange(ref isEnabled, 0); | ||
} | ||
|
||
public void Resume() | ||
{ | ||
Interlocked.Exchange(ref isEnabled, 1); | ||
} | ||
|
||
private int isEnabled = 1; | ||
|
||
public abstract IEnumerable<TTrigger> CurrentPermittedTriggers { get; } | ||
public abstract TState CurrentState { get; } | ||
|
||
public bool IsEnabled | ||
{ | ||
get { return Interlocked.CompareExchange(ref isEnabled, -1, -1) == 1; } | ||
} | ||
|
||
public void RaiseInvalidTrigger(TTrigger trigger) | ||
{ | ||
var handler = UnhandledTrigger; | ||
if (handler != null) handler.Invoke(new TriggerStateEventArgs<TState, TTrigger>(CurrentState, trigger)); | ||
} | ||
|
||
public void RaiseInvalidState(TState targetState) | ||
{ | ||
var handler = InvalidState; | ||
if (handler != null) | ||
handler.Invoke(new TransitionEventArgs<TState, TTrigger>(CurrentState, targetState)); | ||
} | ||
|
||
public void RaiseInvalidState(TState targetState, TTrigger trigger) | ||
{ | ||
var handler = InvalidState; | ||
if (handler != null) | ||
handler.Invoke(new TransitionEventArgs<TState, TTrigger>(CurrentState, targetState, trigger)); | ||
} | ||
|
||
public void RaiseTransitionStarted(TState targetState) | ||
{ | ||
var handler = TransitionStarted; | ||
if (handler != null) | ||
handler.Invoke(new TransitionEventArgs<TState, TTrigger>(CurrentState, targetState)); | ||
} | ||
|
||
|
||
public void RaiseTransitionStarted(TState targetState, TTrigger trigger) | ||
{ | ||
var handler = TransitionStarted; | ||
if (handler != null) | ||
handler.Invoke(new TransitionEventArgs<TState, TTrigger>(CurrentState, targetState, trigger)); | ||
} | ||
|
||
public void RaiseTransitionExecuted(TState pastState) | ||
{ | ||
var handler = TransitionExecuted; | ||
if (handler != null) | ||
handler.Invoke(new TransitionExecutedEventArgs<TState, TTrigger>(CurrentState, pastState)); | ||
} | ||
|
||
|
||
public void RaiseTransitionExecuted(TState pastState, TTrigger trigger) | ||
{ | ||
var handler = TransitionExecuted; | ||
if (handler != null) | ||
handler.Invoke(new TransitionExecutedEventArgs<TState, TTrigger>(CurrentState, pastState, trigger)); | ||
} | ||
} | ||
} |
Oops, something went wrong.