Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nixzhu/redstone
Redstone has a State Machine
https://github.com/nixzhu/redstone
state-machine
Last synced: about 2 months ago
JSON representation
Redstone has a State Machine
- Host: GitHub
- URL: https://github.com/nixzhu/redstone
- Owner: nixzhu
- License: mit
- Created: 2017-01-06T14:54:29.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-11T06:57:53.000Z (almost 6 years ago)
- Last Synced: 2024-10-19T03:04:17.803Z (3 months ago)
- Topics: state-machine
- Language: Swift
- Homepage:
- Size: 59.6 KB
- Stars: 37
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Redstone
Redstone has a State Machine.
## Requirements
Swift 4, iOS 8
(Swift 3, use version 0.3.0)
## Example
If your room has lights, you can turn it on or turn it off. If you cut the wire, it will borken.
![Lights](https://github.com/nixzhu/Redstone/raw/master/Images/lights.png)
We can define states and transitions to descripe the state machine:
``` swift
import Redstoneclass Lights {
enum State: Int {
case off
case on
case broken
}
enum Transition: Int {
case turn
case cut
}
lazy var stateMachine: StateMachine = {
let stateMachine = StateMachine()
stateMachine.add(state: .off) {
print("Lights off")
}
stateMachine.add(state: .on) {
print("Lights on")
}
stateMachine.add(state: .broken) {
print("Lights broken")
}
stateMachine.add(transition: .turn, fromState: .off, toState: .on)
stateMachine.add(transition: .turn, fromState: .on, toState: .off)
stateMachine.add(transition: .cut, fromStates: [.on, .off], toState: .broken)
return stateMachine
}()
}
```We create a lights, set it's initial state to off.
``` swift
let lights = Lights()
lights.stateMachine.initialState = .off
```Now you can turn the lights from off to on, or from on to off:
``` swift
lights.stateMachine.fire(transition: .turn)
```Or cut the wire:
``` swift
lights.stateMachine.fire(transition: .cut)
```It will can not been turn on again.
Run the demo to feel it if you like.
## Installation
### Carthage
```ogdl
github "nixzhu/Redstone"
```### CocoaPods
```ruby
pod 'Redstone'
```## Contact
NIX [@nixzhu](https://twitter.com/nixzhu)
## License
Redstone is available under the MIT license. See the LICENSE file for more info.