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
machine.addHandler(.State0=> .State1) { context in
26
+
machine.addHandler(.state0=> .state1) { context in
27
27
print("0 => 1")
28
28
}
29
29
@@ -34,21 +34,21 @@ let machine = StateMachine<MyState, NoEvent>(state: .State0) { machine in
34
34
}
35
35
36
36
// initial
37
-
XCTAssertEqual(machine.state, MyState.State0)
37
+
XCTAssertEqual(machine.state, MyState.state0)
38
38
39
39
// tryState 0 => 1 => 2 => 1 => 0
40
40
41
-
machine <- .State1
42
-
XCTAssertEqual(machine.state, MyState.State1)
41
+
machine <- .state1
42
+
XCTAssertEqual(machine.state, MyState.state1)
43
43
44
-
machine <- (.State2, "Hello")
45
-
XCTAssertEqual(machine.state, MyState.State2)
44
+
machine <- (.state2, "Hello")
45
+
XCTAssertEqual(machine.state, MyState.state2)
46
46
47
-
machine <- (.State1, "Bye")
48
-
XCTAssertEqual(machine.state, MyState.State1)
47
+
machine <- (.state1, "Bye")
48
+
XCTAssertEqual(machine.state, MyState.state1)
49
49
50
-
machine <- .State0// fail: no 1 => 0
51
-
XCTAssertEqual(machine.state, MyState.State1)
50
+
machine <- .state0// fail: no 1 => 0
51
+
XCTAssertEqual(machine.state, MyState.state1)
52
52
```
53
53
54
54
This will print:
@@ -57,7 +57,7 @@ This will print:
57
57
0=>1
58
58
Any=>2, msg=Optional("Hello")
59
59
2=>Any, msg=Optional("Bye")
60
-
[ERROR] State1=>State0
60
+
[ERROR] state1=>state0
61
61
```
62
62
63
63
### Transition by Event
@@ -66,39 +66,39 @@ Use `<-!` operator to try transition by `Event` rather than specifying target `S
66
66
67
67
```swift
68
68
enumMyEvent: EventType {
69
-
caseEvent0, Event1
69
+
caseevent0, event1
70
70
}
71
71
```
72
72
73
73
```swift
74
-
let machine = StateMachine<MyState, MyEvent>(state: .State0) { machine in
74
+
let machine = StateMachine<MyState, MyEvent>(state: .state0) { machine in
75
75
76
76
// add 0 => 1 => 2
77
-
machine.addRoutes(event: .Event0, transitions: [
78
-
.State0=> .State1,
79
-
.State1=> .State2,
77
+
machine.addRoutes(event: .event0, transitions: [
78
+
.state0=> .state1,
79
+
.state1=> .state2,
80
80
])
81
81
82
82
// add event handler
83
-
machine.addHandler(event: .Event0) { context in
84
-
print(".Event0 triggered!")
83
+
machine.addHandler(event: .event0) { context in
84
+
print(".event0 triggered!")
85
85
}
86
86
}
87
87
88
88
// initial
89
-
XCTAssertEqual(machine.state, MyState.State0)
89
+
XCTAssertEqual(machine.state, MyState.state0)
90
90
91
91
// tryEvent
92
-
machine <-! .Event0
93
-
XCTAssertEqual(machine.state, MyState.State1)
92
+
machine <-! .event0
93
+
XCTAssertEqual(machine.state, MyState.state1)
94
94
95
95
// tryEvent
96
-
machine <-! .Event0
97
-
XCTAssertEqual(machine.state, MyState.State2)
96
+
machine <-! .event0
97
+
XCTAssertEqual(machine.state, MyState.state2)
98
98
99
99
// tryEvent (fails)
100
-
machine <-! .Event0
101
-
XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any")
100
+
machine <-! .event0
101
+
XCTAssertEqual(machine.state, MyState.state2, "event0 doesn't have 2 => Any")
102
102
```
103
103
104
104
If there is no `Event`-based transition, use built-in `NoEvent` instead.
@@ -108,33 +108,33 @@ If there is no `Event`-based transition, use built-in `NoEvent` instead.
108
108
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`.
@@ -178,40 +178,40 @@ For more examples, please see XCTest cases.
State | `StateType` (protocol) | Mostly enum, describing each state e.g. `.State0`.
204
+
State | `StateType` (protocol) | Mostly enum, describing each state e.g. `.state0`.
205
205
Event | `EventType` (protocol) | Name for route-group. Transition can be fired via `Event` instead of explicitly targeting next `State`.
206
206
State Machine | `Machine` | State transition manager which can register `Route`/`RouteMapping` and `Handler` separately for variety of transitions.
207
-
Transition | `Transition` | `From-` and `to-` states represented as `.State1 => .State2`. Also, `.Any` can be used to represent _any state_.
207
+
Transition | `Transition` | `From-` and `to-` states represented as `.state1 => .state2`. Also, `.any` can be used to represent _any state_.
208
208
Route | `Route` | `Transition` + `Condition`.
209
209
Condition | `Context -> Bool` | Closure for validating transition. If condition returns `false`, transition will fail and associated handlers will not be invoked.
210
210
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.
211
-
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.
211
+
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.
212
212
Handler | `Context -> Void` | Transition callback invoked when state has been changed successfully.
0 commit comments