Commit d5b3a3ff authored by toby's avatar toby

updated state machine

parent 6a61c045
......@@ -8,51 +8,46 @@
import Foundation
protocol StateMachineDelegate: class{
typealias StateType:StateMachineDataSource
func didTransitionFrom(from:StateType, to:StateType)
protocol StateMachineDelegate: class {
typealias StateType: StateMachineDataSource
func didTransitionFrom(from: StateType, to: StateType)
}
protocol StateMachineDataSource{
func shouldTransitionFrom(from:Self, to:Self)->Should<Self>
protocol StateMachineDataSource {
func shouldTransitionFrom(from: Self, to: Self)->Should
}
enum Should<T>{
case Continue, Abort, Redirect(T)
enum Should {
case Continue, Abort
}
class StateMachine<P:StateMachineDelegate>{
private var _state:P.StateType{
class StateMachine<P:StateMachineDelegate> {
private var _state: P.StateType {
didSet{
delegate.didTransitionFrom(oldValue, to: _state)
}
}
unowned let delegate:P
unowned let delegate: P
var state:P.StateType{
get{
var state: P.StateType {
get {
return _state
}
set{
switch _state.shouldTransitionFrom(_state, to:newValue){
set {
switch _state.shouldTransitionFrom(_state, to: newValue){
case .Continue:
_state = newValue
case .Redirect(let redirectState):
_state = newValue
self.state = redirectState
case .Abort:
break;
}
}
}
init(initialState:P.StateType, delegate:P){
init(initialState: P.StateType, delegate: P){
_state = initialState //set the primitive to avoid calling the delegate.
self.delegate = delegate
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment