You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use `<-!` operator to try transition by `Event` rather than specifying target `State` ([Test Case](https://github.com/ReactKit/SwiftState/blob/6858f8f49087c4b8b30bd980cfc81e8e74205718/SwiftStateTests/StateMachineEventTests.swift#L54-L76)).
65
+
Use `<-!` operator to try transition by `Event` rather than specifying target `State`.
62
66
63
67
```swift
64
-
enumMyEvent: StateEventType{
68
+
enumMyEvent: EventType{
65
69
caseEvent0, Event1
66
-
caseAnyEvent// create case=Any
67
-
68
-
init(nilLiteral: Void) {
69
-
self= AnyEvent
70
-
}
71
70
}
72
71
```
73
72
74
73
```swift
75
74
let machine = StateMachine<MyState, MyEvent>(state: .State0) { machine in
XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any")
97
+
```
98
+
99
+
If there is no `Event`-based transition, use built-in `NoEvent` instead.
100
+
101
+
### State & Event enums with associated values
102
+
103
+
Above examples use _arrow-style routing_ which are easy to understand, but it lacks in ability to handle **state & event enums with associated values**. In such cases, use either of the following functions to apply _closure-style routing_:
This behaves very similar to JavaScript's safe state-container [rackt/Redux](https://github.com/rackt/redux), where `RouteMapping` can be interpretted as `Redux.Reducer`.
State | `StateType` (protocol) | Mostly enum, describing each state e.g. `.State0`.
123
-
Event | `StateEventType` (protocol) | Name for route-group. Transition can be fired via `Event` instead of explicitly targeting next `State`.
124
-
Machine | `StateMachine` | State transition manager which can register `Route` and `Handler` separately for variety of transitions.
125
-
Transition | `StateTransition` | `From-` and `to-` states represented as `.State1 => .State2`. If `nil` is used for either state, it will be represented as `.AnyState`.
Condition | `Transition -> Bool` | Closure for validating transition. If condition returns `false`, transition will fail and associated handlers will not be invoked.
128
-
Handler | `HandlerContext -> Void` | Transition callback invoked after state has been changed.
129
-
Chain | `StateTransitionChain` | Group of continuous routes represented as `.State1 => .State2 => .State3`
State | `StateType` (protocol) | Mostly enum, describing each state e.g. `.State0`.
199
+
Event | `EventType` (protocol) | Name for route-group. Transition can be fired via `Event` instead of explicitly targeting next `State`.
200
+
State Machine | `Machine` | State transition manager which can register `Route`/`RouteMapping` and `Handler` separately for variety of transitions.
201
+
Transition | `Transition` | `From-` and `to-` states represented as `.State1 => .State2`. Also, `.Any` can be used to represent _any state_.
202
+
Route | `Route` | `Transition` + `Condition`.
203
+
Condition | `Context -> Bool` | Closure for validating transition. If condition returns `false`, transition will fail and associated handlers will not be invoked.
204
+
Route Mapping | `(event: E?, fromState: S, userInfo: Any?) -> S?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state & event are enum with associated values. Return value (`S?`) means preferred-`toState`, where passing `nil` means no routes available. See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info.
205
+
State Route Mapping | `(fromState: S, userInfo: Any?) -> [S]?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state is enum with associated values. Return value (`[S]?`) means multiple `toState`s from single `fromState` (synonym for multiple routing e.g. `.State0 => [.State1, .State2]`). See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info.
206
+
Handler | `Context -> Void` | Transition callback invoked when state has been changed successfully.
0 commit comments