Created
March 13, 2019 10:21
-
-
Save IsaAliev/e58954c37b86f1a7bd484d13cb623d27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class BasicStateController: StateController { | |
var stateQueue = Queue<State>() | |
weak var observer: CurrentStateObserver? | |
required init() {} | |
func transitTo(_ newState: State) { | |
newState.delegate = self | |
stateQueue.enqueue(newState) | |
if stateQueue.count == 1 { | |
observer?.didChangeStateTo(newState) | |
return | |
} | |
stateQueue.penult()?.exit() | |
} | |
func moveToNextState() { | |
guard stateQueue.count > 1 else { | |
return | |
} | |
stateQueue.dequeue() | |
if let state = stateQueue.first() { | |
observer?.didChangeStateTo(state) | |
} | |
} | |
} | |
extension BasicStateController: StateDelegate { | |
func didExit(_ state: State) { | |
print("out \(state)") | |
moveToNextState() | |
} | |
func didEnter(_ state: State) { | |
print("in \(state)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment