The framework provides 2 different state machines SimpleStateMachine
and SyncronizedStateMachine
. The second provides logic for blocking state changing until it's finilized
final class MockStateProvider: StateProvider {
typealias T = MockState
func canMove(fromState state: MockState, toState to: MockState) -> Bool {
switch (state, to) {
case (.one, .two),
(.two, .three),
(.two, .one),
(.three,. two): return true
default: return false
}
}
}
final class DelegateProxyTester {
var proxy: StateMachineDelegateProxy<MockState> {
let proxy = StateMachineDelegateProxy<MockState>()
proxy.willMoveCallback = self.willMove
proxy.errorMoving = self.errorMoving
return proxy
}
func willMove(from state: MockState, toState to: MockState) {
//do the logic based on state change
}
func errorMoving(from state: MockState, toState to: MockState) {
//process the error
}
}
mockProvider = MockStateProvider()
proxyTester = DelegateProxyTester()
machineToTest = SimpleStateMachine(initialState: .one, roadMapProvider: mockProvider)
machineToTest.delegateProxy = proxyTester.proxy
It behaves more like a decorator on top of SimpleStateMachine
that handles logic for preventing state change until it's allowed. Consider it's like using semaphore inside.
To use it follow the step above with the difference that you need to provide different proxy
final class SyncDelegateProxyTester {
var proxy: SyncronizedMachineDelegateProxy<MockState> {
let proxy = SyncronizedMachineDelegateProxy<MockState>()
proxy.syncWillMove = willMove
proxy.errorMoving = errorMoving
return proxy
}
func willMove(from state: MockState, toState to: MockState, completion: @escaping () -> Void) {
//do the logic and when it's finished call completion
}
func errorMoving(from state: MockState, toState to: MockState) {
// process the error if need
}
}
RouterStateMachine is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RouterStateMachine'
ALDO Inc., [email protected]
RouterStateMachine is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 1 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics