Skip to content

Commit 8e093da

Browse files
committed
Update README.md & BasicTests.
1 parent 1be6782 commit 8e093da

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

README.md

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,62 @@ Elegant state machine for Swift.
1111
```swift
1212
enum MyState: StateType {
1313
case State0, State1, State2
14-
case AnyState // create case=Any
15-
16-
init(nilLiteral: Void) {
17-
self = AnyState
18-
}
1914
}
2015
```
2116

2217
```swift
23-
let machine = StateMachine<MyState, MyEvent>(state: .State0) { machine in
18+
// setup state machine
19+
let machine = Machine<MyState, NoEvent>(state: .State0) { machine in
2420

2521
machine.addRoute(.State0 => .State1)
26-
machine.addRoute(nil => .State2) { context in print("Any => 2, msg=\(context.userInfo)") }
27-
machine.addRoute(.State2 => nil) { context in print("2 => Any, msg=\(context.userInfo)") }
22+
machine.addRoute(.Any => .State2) { context in print("Any => 2, msg=\(context.userInfo)") }
23+
machine.addRoute(.State2 => .Any) { context in print("2 => Any, msg=\(context.userInfo)") }
2824

29-
// add handler (handlerContext = (event, transition, order, userInfo))
25+
// add handler (`context = (event, fromState, toState, userInfo)`)
3026
machine.addHandler(.State0 => .State1) { context in
3127
print("0 => 1")
3228
}
3329

3430
// add errorHandler
35-
machine.addErrorHandler { (event, transition, order, userInfo) in
31+
machine.addErrorHandler { event, fromState, toState, userInfo in
3632
print("[ERROR] \(transition.fromState) => \(transition.toState)")
3733
}
3834
}
3935

36+
// initial
37+
XCTAssertTrue(machine.state == .State0)
38+
4039
// tryState 0 => 1 => 2 => 1 => 0
40+
4141
machine <- .State1
42+
XCTAssertTrue(machine.state == .State1)
43+
4244
machine <- (.State2, "Hello")
45+
XCTAssertTrue(machine.state == .State2)
46+
4347
machine <- (.State1, "Bye")
48+
XCTAssertTrue(machine.state == .State1)
49+
4450
machine <- .State0 // fail: no 1 => 0
45-
46-
print("machine.state = \(machine.state)")
51+
XCTAssertTrue(machine.state == .State1)
4752
```
4853

4954
This will print:
5055

5156
```swift
5257
0 => 1
53-
Any => 2, msg=Hello
54-
2 => Any, msg=Bye
55-
[ERROR] 1 => 0
56-
machine.state = 1
58+
Any => 2, msg=Optional("Hello")
59+
2 => Any, msg=Optional("Bye")
60+
[ERROR] State1 => State0
5761
```
5862

5963
### Transition by Event
6064

61-
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` ([Test Case](https://github.com/ReactKit/SwiftState/blob/1be67826b3cc9187dfaac85c2e70613f3129fad6/SwiftStateTests/TryEventTests.swift#L32-L54)).
6266

6367
```swift
64-
enum MyEvent: StateEventType {
68+
enum MyEvent: EventType {
6569
case Event0, Event1
66-
case AnyEvent // create case=Any
67-
68-
init(nilLiteral: Void) {
69-
self = AnyEvent
70-
}
7170
}
7271
```
7372

@@ -95,6 +94,8 @@ XCTAssertEqual(machine.state, MyState.State2)
9594
XCTAssertFalse(success, "Event0 doesn't have 2 => Any")
9695
```
9796

97+
If there is no `Event`-based transition, use built-in `NoEvent` instead.
98+
9899
For more examples, please see XCTest cases.
99100

100101

@@ -106,27 +107,29 @@ For more examples, please see XCTest cases.
106107
- Try transition + messaging: `machine <- (.State1, "GoGoGo")`
107108
- Try event: `machine <-! .Event1`
108109
- Highly flexible transition routing
109-
- using Condition
110-
- using AnyState (`nil` state)
111-
- or both (blacklisting): `nil => nil` + condition
112-
- Success/Error/Entry/Exit handlers with `order: UInt8` (no before/after handler stuff)
110+
- Using `Condition`
111+
- Using `.Any` state/event
112+
- Blacklisting: `.Any => .Any` + `Condition`
113+
- Route Mapping (closure-based routing): [#36](https://github.com/ReactKit/SwiftState/pull/36)
114+
- Success/Error/Entry/Exit handlers with `order: UInt8` (more flexible than before/after handlers)
113115
- Removable routes and handlers
114116
- Chaining: `.State0 => .State1 => .State2`
115-
- Event: `machine.addRouteEvent("WakeUp", transitions); machine <-! "WakeUp"`
116117
- Hierarchical State Machine: [#10](https://github.com/ReactKit/SwiftState/pull/10)
117118

118119
## Terms
119120

120-
Term | Class | Description
121-
--------- | ----------------------------- | ------------------------------------------
122-
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`.
126-
Route | `StateRoute` | `Transition` + `Condition`.
127-
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`
121+
Term | Type | Description
122+
------------- | ----------------------------- | ------------------------------------------
123+
State | `StateType` (protocol) | Mostly enum, describing each state e.g. `.State0`.
124+
Event | `EventType` (protocol) | Name for route-group. Transition can be fired via `Event` instead of explicitly targeting next `State`.
125+
State Machine | `Machine` | State transition manager which can register `Route`/`RouteMapping` and `Handler` separately for variety of transitions.
126+
Transition | `Transition` | `From-` and `to-` states represented as `.State1 => .State2`. Also, `.Any` can be used to represent _any state_.
127+
Route | `Route` | `Transition` + `Condition`.
128+
Condition | `Context -> Bool` | Closure for validating transition. If condition returns `false`, transition will fail and associated handlers will not be invoked.
129+
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.
130+
Handler | `Context -> Void` | Transition callback invoked after state has been changed.
131+
Context | `(event: E?, fromState: S, toState: S, userInfo: Any?)` | Closure argument for `Condition` & `Handler`.
132+
Chain | `TransitionChain` / `RouteChain` | Group of continuous routes represented as `.State1 => .State2 => .State3`
130133

131134

132135
## Related Articles

SwiftStateTests/BasicTests.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ class BasicTests: _TestCase
1313
{
1414
func testREADME()
1515
{
16+
// setup state machine
1617
let machine = Machine<MyState, NoEvent>(state: .State0) { machine in
1718

1819
machine.addRoute(.State0 => .State1)
1920
machine.addRoute(.Any => .State2) { context in print("Any => 2, msg=\(context.userInfo)") }
2021
machine.addRoute(.State2 => .Any) { context in print("2 => Any, msg=\(context.userInfo)") }
2122

22-
// add handler (handlerContext = (event, transition, order, userInfo))
23+
// add handler (`context = (event, fromState, toState, userInfo)`)
2324
machine.addHandler(.State0 => .State1) { context in
2425
print("0 => 1")
2526
}
@@ -30,6 +31,9 @@ class BasicTests: _TestCase
3031
}
3132
}
3233

34+
// initial
35+
XCTAssertTrue(machine.state == .State0)
36+
3337
// tryState 0 => 1 => 2 => 1 => 0
3438

3539
machine <- .State1
@@ -43,8 +47,6 @@ class BasicTests: _TestCase
4347

4448
machine <- .State0 // fail: no 1 => 0
4549
XCTAssertTrue(machine.state == .State1)
46-
47-
print("machine.state = \(machine.state)")
4850
}
4951

5052
func testREADME_string()

SwiftStateTests/MyState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import SwiftState
1010

11-
enum MyState: Int, StateType
11+
enum MyState: StateType
1212
{
1313
case State0, State1, State2, State3
1414
}

0 commit comments

Comments
 (0)