Skip to content

Commit ed939d4

Browse files
authored
Merge pull request #64 from humblehacker/swift/4.0
Replace Machine.Context tuple with a struct
2 parents 5541807 + bd79d8e commit ed939d4

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed

Sources/Machine.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
public class Machine<S: StateType, E: EventType>
1818
{
1919
/// Closure argument for `Condition` & `Handler`.
20-
public typealias Context = (event: E?, fromState: S, toState: S, userInfo: Any?)
20+
public struct Context
21+
{
22+
public let event: E?
23+
public let fromState: S
24+
public let toState: S
25+
public let userInfo: Any?
26+
}
2127

2228
/// Closure for validating transition.
2329
/// If condition returns `false`, transition will fail and associated handlers will not be invoked.
@@ -569,7 +575,7 @@ internal func _validTransitions<S>(fromState: S, toState: S) -> [Transition<S>]
569575

570576
internal func _canPassCondition<S:StateType, E:EventType>(_ condition: Machine<S, E>.Condition?, forEvent event: E?, fromState: S, toState: S, userInfo: Any?) -> Bool
571577
{
572-
return condition?((event, fromState, toState, userInfo)) ?? true
578+
return condition?(Machine<S, E>.Context(event: event, fromState: fromState, toState: toState, userInfo: userInfo)) ?? true
573579
}
574580

575581
internal func _insertHandlerIntoArray<S, E>(_ handlerInfos: inout [_HandlerInfo<S, E>], newHandlerInfo: _HandlerInfo<S, E>)

SwiftState.xcodeproj/project.pbxproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@
236236
5243E3C71E9821E200D031A9 /* TransitionChainTests.swift */,
237237
5243E3C81E9821E200D031A9 /* TransitionTests.swift */,
238238
);
239-
path = SwiftStateTests;
239+
name = SwiftStateTests;
240+
path = Tests/SwiftStateTests;
240241
sourceTree = "<group>";
241242
};
242243
/* End PBXGroup section */
@@ -555,7 +556,7 @@
555556
"DEBUG=1",
556557
"$(inherited)",
557558
);
558-
INFOPLIST_FILE = SwiftStateTests/Info.plist;
559+
INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist;
559560
PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)";
560561
PRODUCT_NAME = "$(TARGET_NAME)";
561562
SWIFT_VERSION = 4.0;
@@ -571,7 +572,7 @@
571572
"$(DEVELOPER_FRAMEWORKS_DIR)",
572573
"$(inherited)",
573574
);
574-
INFOPLIST_FILE = SwiftStateTests/Info.plist;
575+
INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist;
575576
PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)";
576577
PRODUCT_NAME = "$(TARGET_NAME)";
577578
SWIFT_VERSION = 4.0;

Tests/SwiftStateTests/BasicTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class BasicTests: _TestCase
2626
}
2727

2828
// add errorHandler
29-
machine.addErrorHandler { event, fromState, toState, userInfo in
30-
print("[ERROR] \(fromState) => \(toState)")
29+
machine.addErrorHandler { context in
30+
print("[ERROR] \(context.fromState) => \(context.toState)")
3131
}
3232
}
3333

Tests/SwiftStateTests/HierarchicalMachineTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ class HierarchicalMachineTests: _TestCase
9292
let mainMachine = StateMachine<_MainState, NoEvent>(state: .mainState0) { mainMachine in
9393

9494
// add routes & handle for same-subMachine internal transitions
95-
mainMachine.addRoute(.any => .any, condition: { _, fromState, toState, userInfo in
95+
mainMachine.addRoute(.any => .any, condition: { context in
9696

97-
switch (fromState, toState) {
97+
switch (context.fromState, context.toState) {
9898
case let (.subMachine1(state1), .subMachine1(state2)):
9999
return subMachine1.hasRoute(fromState: state1, toState: state2)
100100
case let (.subMachine2(state1), .subMachine2(state2)):
@@ -103,8 +103,8 @@ class HierarchicalMachineTests: _TestCase
103103
return false
104104
}
105105

106-
}, handler: { _, fromState, toState, userInfo in
107-
switch (fromState, toState) {
106+
}, handler: { context in
107+
switch (context.fromState, context.toState) {
108108
case let (.subMachine1, .subMachine1(state2)):
109109
subMachine1 <- state2
110110
case let (.subMachine2, .subMachine2(state2)):

Tests/SwiftStateTests/MachineTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class MachineTests: _TestCase
341341

342342
let machine = Machine<MyState, MyEvent>(state: .state0) { machine in
343343
machine.addRoutes(event: .event0, transitions: [ .state0 => .state1 ])
344-
machine.addErrorHandler { event, fromState, toState, userInfo in
344+
machine.addErrorHandler { _ in
345345
invokeCount += 1
346346
}
347347
}

Tests/SwiftStateTests/MiscTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class MiscTests: _TestCase
2626
}
2727

2828
// add errorHandler
29-
machine.addErrorHandler { event, fromState, toState, userInfo in
30-
print("[ERROR] \(fromState) => \(toState)")
29+
machine.addErrorHandler { context in
30+
print("[ERROR] \(context.fromState) => \(context.toState)")
3131
}
3232
}
3333

@@ -63,8 +63,8 @@ class MiscTests: _TestCase
6363
}
6464

6565
// add errorHandler
66-
machine.addErrorHandler { event, fromState, toState, userInfo in
67-
print("[ERROR] \(fromState) => \(toState)")
66+
machine.addErrorHandler { context in
67+
print("[ERROR] \(context.fromState) => \(context.toState)")
6868
}
6969
}
7070

Tests/SwiftStateTests/RouteMappingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class RouteMappingTests: _TestCase
9898
}
9999

100100
// increment `count` when any events i.e. `.cancelAction` and `.loadAction(x)` succeed.
101-
machine.addHandler(event: .any) { event, transition, order, userInfo in
101+
machine.addHandler(event: .any) { _ in
102102
count += 1
103103
}
104104

@@ -153,7 +153,7 @@ class RouteMappingTests: _TestCase
153153
}
154154

155155
// increment `count` when any events i.e. `.cancelAction` and `.loadAction(x)` succeed.
156-
machine.addHandler(.any => .any) { event, transition, order, userInfo in
156+
machine.addHandler(.any => .any) { _ in
157157
count += 1
158158
}
159159

0 commit comments

Comments
 (0)