-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathDestinationConfiguration.cs
More file actions
93 lines (82 loc) · 4.56 KB
/
Copy pathDestinationConfiguration.cs
File metadata and controls
93 lines (82 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
namespace Stateless
{
partial class StateMachine<TState, TTrigger>
{
/// <summary>
/// This class containd the required trigger information for a transition.
/// </summary>
public class DestinationConfiguration
{
private readonly TransitionConfiguration _transitionConfiguration;
private readonly TriggerBehaviour _triggerBehaviour;
private readonly StateRepresentation _representation;
internal DestinationConfiguration(TransitionConfiguration transitionConfiguration, TriggerBehaviour triggerBehaviour, StateRepresentation representation)
{
_transitionConfiguration = transitionConfiguration;
_triggerBehaviour = triggerBehaviour;
_representation = representation;
}
/// <summary>
/// Adds a guard function to the trigger. This guard function will determine if the transition will occur or not.
/// </summary>
/// <param name="guard">This method is run when the state machine fires the trigger.</param>
/// <param name="description">Optional description of the guard</param>
public DestinationConfiguration If(Func<object[], bool> guard, string description = null)
{
_triggerBehaviour.SetGuard(new TransitionGuard(guard, description));
return this;
}
/// <summary>
/// Adds a guard function to the trigger. This guard function will determine if the transition will occur or not.
/// </summary>
/// <typeparam name="TArg">The parameter to the guard function </typeparam>
/// <param name="guard">This method is run when the state machine fires the trigger.</param>
/// <param name="description">Optional description of the guard</param>
public DestinationConfiguration If<TArg>(Func<TArg, bool> guard, string description = null)
{
_triggerBehaviour.SetGuard(new TransitionGuard(TransitionGuard.ToPackedGuard(guard), description));
return this;
}
/// <summary>
/// Creates a new transition. Use To(), Self(), Internal() or Dynamic() to set up the destination.
/// </summary>
/// <param name="trigger">The event trigger that will trigger this transition.</param>
public TransitionConfiguration Transition(TTrigger trigger)
{
return new TransitionConfiguration(_transitionConfiguration.StateConfiguration, _representation, trigger);
}
/// <summary>
/// Adds an action to a transition. The action will be executed before the Exit action(s) (if any) are executed.
/// </summary>
/// <param name="someAction">The action run when the trigger event is handled.</param>
public StateConfiguration Do(Action someAction)
{
if (someAction == null) throw new ArgumentNullException(nameof(someAction));
_triggerBehaviour.AddAction((t, args) => someAction());
return _transitionConfiguration.StateConfiguration;
}
/// <summary>
/// Adds an action to a transition. The action will be executed before the Exit action(s) (if any) are executed.
/// </summary>
/// <param name="someAction">The action run when the trigger event is handled.</param>
public StateConfiguration Do(Action<Transition> someAction)
{
if (someAction == null) throw new ArgumentNullException(nameof(someAction));
_triggerBehaviour.AddAction(someAction);
return _transitionConfiguration.StateConfiguration;
}
/// <summary>
/// Adds an action to a transition. The action will be executed before the Exit action(s) (if any) are executed.
/// </summary>
/// <typeparam name="TArg">The paramter used by the action.</typeparam>
/// <param name="someAction">The action run when the trigger event is handled.</param>
public StateConfiguration Do<TArg>(Action<TArg, Transition> someAction)
{
if (someAction == null) throw new ArgumentNullException(nameof(someAction));
_triggerBehaviour.AddAction((t, args) => someAction(ParameterConversion.Unpack<TArg>(args, 0), t));
return _transitionConfiguration.StateConfiguration;
}
}
}
}