Skip to content

Commit e63324b

Browse files
committed
Update README.md
1 parent 6858f8f commit e63324b

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SwiftState [![Circle CI](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F1.2.svg?style=svg)](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F1.2)
1+
SwiftState [![Circle CI](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F2.0.svg?style=svg)](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F2.0)
22
==========
33

44
Elegant state machine for Swift.
@@ -56,6 +56,34 @@ Any => 2, msg=Hello
5656
machine.state = 1
5757
```
5858

59+
### Transition by Event
60+
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)).
62+
63+
```swift
64+
let machine = StateMachine<MyState, MyEvent>(state: .State0) { machine in
65+
66+
// add 0 => 1 => 2
67+
machine.addRouteEvent(.Event0, transitions: [
68+
.State0 => .State1,
69+
.State1 => .State2,
70+
])
71+
}
72+
73+
// tryEvent
74+
machine <-! .Event0
75+
XCTAssertEqual(machine.state, MyState.State1)
76+
77+
// tryEvent
78+
machine <-! .Event0
79+
XCTAssertEqual(machine.state, MyState.State2)
80+
81+
// tryEvent
82+
let success = machine <-! .Event0
83+
XCTAssertEqual(machine.state, MyState.State2)
84+
XCTAssertFalse(success, "Event0 doesn't have 2 => Any")
85+
```
86+
5987
For more examples, please see XCTest cases.
6088

6189

0 commit comments

Comments
 (0)