Skip to content

Commit 3b5d315

Browse files
committed
Revert "Migrate SwiftState to Swift 1.2"
This reverts commit f6e4f50.
1 parent 703d456 commit 3b5d315

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

SwiftState/HierarchicalStateMachine.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class HierarchicalStateMachine<S: StateType, E: StateEventType>: StateMac
6161
{
6262
assert(state is HSM.State, "HSM state must be String.")
6363

64-
let components = split(state as! HSM.State, { $0 == "." }, maxSplit: 1)
64+
let components = split(state as HSM.State, { $0 == "." }, maxSplit: 1)
6565

6666
switch components.count {
6767
case 2:
@@ -85,7 +85,7 @@ public class HierarchicalStateMachine<S: StateType, E: StateEventType>: StateMac
8585
let (submachine, substate) = self._submachineTupleForState(self._state)
8686

8787
if let submachine = submachine {
88-
self._state = "\(submachine.name).\(submachine.state)" as! State
88+
self._state = "\(submachine.name).\(submachine.state)" as State
8989
}
9090

9191
return self._state
@@ -147,7 +147,7 @@ public class HierarchicalStateMachine<S: StateType, E: StateEventType>: StateMac
147147
// try changing submachine-internal state
148148
if fromSubmachine != nil && toSubmachine != nil && fromSubmachine === toSubmachine {
149149

150-
if toSubmachine!.canTryState(toSubstate, forEvent: event as! HSM.Event) {
150+
if toSubmachine!.canTryState(toSubstate, forEvent: event as HSM.Event) {
151151

152152
//
153153
// NOTE:

SwiftState/StateMachine.swift

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@ public class StateMachineRouteID<S: StateType, E: StateEventType>
2626
self.transition = transition
2727
self.routeKey = routeKey
2828
self.event = event
29-
30-
self.bundledRouteIDs = nil
3129
}
3230

3331
private init(bundledRouteIDs: [StateMachineRouteID<S, E>]?)
3432
{
3533
self.bundledRouteIDs = bundledRouteIDs
36-
37-
self.transition = nil
38-
self.routeKey = nil
39-
self.event = nil
4034
}
4135
}
4236

@@ -54,16 +48,11 @@ public class StateMachineHandlerID<S: StateType, E: StateEventType>
5448
{
5549
self.transition = transition
5650
self.handlerKey = handlerKey
57-
58-
self.bundledHandlerIDs = nil
5951
}
6052

6153
private init(bundledHandlerIDs: [StateMachineHandlerID<S, E>]?)
6254
{
6355
self.bundledHandlerIDs = bundledHandlerIDs
64-
65-
self.transition = nil
66-
self.handlerKey = nil
6756
}
6857
}
6958

@@ -350,6 +339,11 @@ public class StateMachine<S: StateType, E: StateEventType>
350339
return self.addRoute(route)
351340
}
352341

342+
public func addRoute(transition: Transition, condition: @autoclosure () -> Bool) -> RouteID
343+
{
344+
return self.addRoute(transition, condition: { t in condition() })
345+
}
346+
353347
public func addRoute(route: Route) -> RouteID
354348
{
355349
return self._addRoute(route)
@@ -395,6 +389,11 @@ public class StateMachine<S: StateType, E: StateEventType>
395389
return self.addRoute(route, handler: handler)
396390
}
397391

392+
public func addRoute(transition: Transition, condition: @autoclosure () -> Bool, handler: Handler) -> (RouteID, HandlerID)
393+
{
394+
return self.addRoute(transition, condition: { t in condition() }, handler: handler)
395+
}
396+
398397
public func addRoute(route: Route, handler: Handler) -> (RouteID, HandlerID)
399398
{
400399
let transition = route.transition
@@ -622,6 +621,11 @@ public class StateMachine<S: StateType, E: StateEventType>
622621
return self.addRouteChain(routeChain, handler: handler)
623622
}
624623

624+
public func addRouteChain(chain: TransitionChain, condition: @autoclosure () -> Bool, handler: Handler) -> (RouteID, HandlerID)
625+
{
626+
return self.addRouteChain(chain, condition: { t in condition() }, handler: handler)
627+
}
628+
625629
public func addRouteChain(chain: RouteChain, handler: Handler) -> (RouteID, HandlerID)
626630
{
627631
var routeIDs: [RouteID] = []
@@ -783,6 +787,11 @@ public class StateMachine<S: StateType, E: StateEventType>
783787
return self.addRouteEvent(event, routes: routes)
784788
}
785789

790+
public func addRouteEvent(event: Event, transitions: [Transition], condition: @autoclosure () -> Bool) -> [RouteID]
791+
{
792+
return self.addRouteEvent(event, transitions: transitions, condition: { t in condition() })
793+
}
794+
786795
public func addRouteEvent(event: Event, routes: [Route]) -> [RouteID]
787796
{
788797
var routeIDs: [RouteID] = []
@@ -809,6 +818,11 @@ public class StateMachine<S: StateType, E: StateEventType>
809818

810819
return (routeIDs, handlerID)
811820
}
821+
822+
public func addRouteEvent(event: Event, transitions: [Transition], condition: @autoclosure () -> Bool, handler: Handler) -> ([RouteID], HandlerID)
823+
{
824+
return self.addRouteEvent(event, transitions: transitions, condition: { t in condition() }, handler: handler)
825+
}
812826

813827
public func addRouteEvent(event: Event, routes: [Route], handler: Handler) -> ([RouteID], HandlerID)
814828
{

SwiftState/StateRoute.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public struct StateRoute<S: StateType>
2222
self.condition = condition
2323
}
2424

25+
public init(transition: Transition, condition: @autoclosure () -> Bool)
26+
{
27+
self.init(transition: transition, condition: { t in condition() })
28+
}
29+
2530
public func toTransition() -> Transition
2631
{
2732
return self.transition

SwiftStateTests/StateMachineChainTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ class StateMachineChainTests: _TestCase
5050
var invokeCount = 0
5151

5252
// add 0 => 1 => 2
53-
machine.addRouteChain(.State0 => .State1 => .State2, condition: { _ -> Bool in
54-
return flag
55-
}) { (context) -> Void in
53+
machine.addRouteChain(.State0 => .State1 => .State2, condition: flag) { context in
5654
invokeCount++
5755
return
5856
}
59-
57+
6058
// tryState 0 => 1 => 2
6159
machine <- .State1
6260
machine <- .State2

SwiftStateTests/StateMachineTests.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ class StateMachineTests: _TestCase
103103
var flag = false
104104

105105
// add 0 => 1
106-
machine.addRoute(.State0 => .State1, condition: { _ -> Bool in
107-
return flag
108-
})
106+
machine.addRoute(.State0 => .State1, condition: flag)
109107

110108
XCTAssertFalse(machine.hasRoute(.State0 => .State1))
111109

@@ -163,9 +161,7 @@ class StateMachineTests: _TestCase
163161
machine.addRoute(.State0 => .State1)
164162

165163
// add 0 => 1 with condition + conditionalHandler
166-
machine.addRoute(.State0 => .State1, condition: { _ -> Bool in
167-
return flag
168-
}) { context in
164+
machine.addRoute(.State0 => .State1, condition: flag) { context in
169165
returnedTransition = context.transition
170166
}
171167

SwiftStateTests/StateRouteTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class StateRouteTests: _TestCase
1818
XCTAssertEqual(route.transition.toState, MyState.State1)
1919
XCTAssertTrue(route.condition == nil)
2020

21-
let route2 = StateRoute<MyState>(transition: .State1 => .State2, condition: { _ -> Bool in
22-
return false
23-
})
21+
let route2 = StateRoute<MyState>(transition: .State1 => .State2, condition: false)
2422
XCTAssertEqual(route2.transition.fromState, MyState.State1)
2523
XCTAssertEqual(route2.transition.toState, MyState.State2)
2624
XCTAssertTrue(route2.condition != nil)

0 commit comments

Comments
 (0)