@@ -16,39 +16,39 @@ enum MyState: StateType {
16
16
17
17
``` swift
18
18
// setup state machine
19
- let machine = Machine < MyState, NoEvent> (state : .State0 ) { machine in
20
-
19
+ let machine = StateMachine < MyState, NoEvent> (state : .State0 ) { machine in
20
+
21
21
machine.addRoute (.State0 => .State1 )
22
22
machine.addRoute (.Any => .State2 ) { context in print (" Any => 2, msg=\( context.userInfo ) " ) }
23
23
machine.addRoute (.State2 => .Any ) { context in print (" 2 => Any, msg=\( context.userInfo ) " ) }
24
-
24
+
25
25
// add handler (`context = (event, fromState, toState, userInfo)`)
26
26
machine.addHandler (.State0 => .State1 ) { context in
27
27
print (" 0 => 1" )
28
28
}
29
-
29
+
30
30
// add errorHandler
31
31
machine.addErrorHandler { event, fromState, toState, userInfo in
32
- print (" [ERROR] \( transition. fromState ) => \( transition. toState ) " )
32
+ print (" [ERROR] \( fromState ) => \( toState ) " )
33
33
}
34
34
}
35
35
36
36
// initial
37
- XCTAssertTrue (machine.state == .State0 )
38
-
37
+ XCTAssertEqual (machine.state , MyState .State0 )
38
+
39
39
// tryState 0 => 1 => 2 => 1 => 0
40
40
41
41
machine <- .State1
42
- XCTAssertTrue (machine.state == .State1 )
43
-
42
+ XCTAssertEqual (machine.state , MyState .State1 )
43
+
44
44
machine <- (.State2 , " Hello" )
45
- XCTAssertTrue (machine.state == .State2 )
46
-
45
+ XCTAssertEqual (machine.state , MyState .State2 )
46
+
47
47
machine <- (.State1 , " Bye" )
48
- XCTAssertTrue (machine.state == .State1 )
49
-
48
+ XCTAssertEqual (machine.state , MyState .State1 )
49
+
50
50
machine <- .State0 // fail: no 1 => 0
51
- XCTAssertTrue (machine.state == .State1 )
51
+ XCTAssertEqual (machine.state , MyState .State1 )
52
52
```
53
53
54
54
This will print:
@@ -72,17 +72,17 @@ enum MyEvent: EventType {
72
72
73
73
``` swift
74
74
let machine = StateMachine< MyState, MyEvent> (state : .State0 ) { machine in
75
-
75
+
76
76
// add 0 => 1 => 2
77
- machine.addRoute (event : .Event0 , transitions : [
77
+ machine.addRoutes (event : .Event0 , transitions : [
78
78
.State0 => .State1 ,
79
79
.State1 => .State2 ,
80
80
])
81
81
}
82
-
82
+
83
83
// initial
84
84
XCTAssertEqual (machine.state , MyState.State0 )
85
-
85
+
86
86
// tryEvent
87
87
machine <-! .Event0
88
88
XCTAssertEqual (machine.state , MyState.State1 )
@@ -191,6 +191,7 @@ For more examples, please see XCTest cases.
191
191
- Route Chaining: ` .State0 => .State1 => .State2 `
192
192
- Hierarchical State Machine: [ #10 ] ( https://github.com/ReactKit/SwiftState/pull/10 )
193
193
194
+
194
195
## Terms
195
196
196
197
Term | Type | Description
0 commit comments