Skip to content

Commit ef07416

Browse files
committed
Refactor code for better typing, naming, and routeMapping support.
1 parent cc6bc8f commit ef07416

35 files changed

+1946
-1910
lines changed

SwiftState.xcodeproj/project.pbxproj

Lines changed: 88 additions & 60 deletions
Large diffs are not rendered by default.

SwiftState/EventType.swift

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// EventType.swift
3+
// SwiftState
4+
//
5+
// Created by Yasuhiro Inami on 2014/08/05.
6+
// Copyright (c) 2014年 Yasuhiro Inami. All rights reserved.
7+
//
8+
9+
public protocol EventType: Hashable {}
10+
11+
// MARK: _Event (internal)
12+
13+
internal enum _Event<E: EventType>: Hashable
14+
{
15+
case Some(E)
16+
case Any // represents any `Some(E)` events but not `.None`
17+
case None // default internal value for `addRoute()` without event
18+
19+
internal var hashValue: Int
20+
{
21+
switch self {
22+
case .Some(let x): return x.hashValue
23+
case .Any: return -4611686018427387904
24+
case .None: return -4611686018427387905
25+
}
26+
}
27+
28+
internal var value: E?
29+
{
30+
switch self {
31+
case .Some(let x): return x
32+
default: return nil
33+
}
34+
}
35+
}
36+
37+
internal func == <E: Hashable>(lhs: _Event<E>, rhs: _Event<E>) -> Bool
38+
{
39+
return lhs.hashValue == rhs.hashValue
40+
}
41+
42+
internal func == <E: Hashable>(lhs: _Event<E>, rhs: E) -> Bool
43+
{
44+
return lhs.hashValue == rhs.hashValue
45+
}
46+
47+
internal func == <E: Hashable>(lhs: E, rhs: _Event<E>) -> Bool
48+
{
49+
return lhs.hashValue == rhs.hashValue
50+
}
51+
52+
// MARK: Event (public)
53+
54+
/// `EventType` wrapper for handling`.Any` event.
55+
public enum Event<E: EventType>: Equatable
56+
{
57+
case Some(E)
58+
case Any
59+
60+
public var value: E?
61+
{
62+
switch self {
63+
case .Some(let x): return x
64+
default: return nil
65+
}
66+
}
67+
68+
internal func _toInternal() -> _Event<E>
69+
{
70+
switch self {
71+
case .Some(let x): return .Some(x)
72+
case .Any: return .Any
73+
}
74+
}
75+
}
76+
77+
public func == <E: EventType>(lhs: Event<E>, rhs: Event<E>) -> Bool
78+
{
79+
switch (lhs, rhs) {
80+
case let (.Some(x1), .Some(x2)) where x1 == x2:
81+
return true
82+
case (.Any, .Any):
83+
return true
84+
default:
85+
return false
86+
}
87+
}
88+
89+
// MARK: NoEvent
90+
91+
/// Useful for creating StateMachine without events, i.e. `Machine<MyState, NoEvent>`.
92+
public enum NoEvent: EventType
93+
{
94+
public var hashValue: Int
95+
{
96+
return 0
97+
}
98+
}
99+
100+
public func == (lhs: NoEvent, rhs: NoEvent) -> Bool
101+
{
102+
return true
103+
}

SwiftState/HandlerID.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// HandlerID.swift
3+
// SwiftState
4+
//
5+
// Created by Yasuhiro Inami on 2015-11-10.
6+
// Copyright © 2015 Yasuhiro Inami. All rights reserved.
7+
//
8+
9+
public class HandlerID<S: StateType, E: EventType>
10+
{
11+
/// - Note: `nil` is used for error-handlerID
12+
internal let transition: Transition<S>?
13+
14+
internal let key: String
15+
16+
internal init(transition: Transition<S>?, key: String)
17+
{
18+
self.transition = transition
19+
self.key = key
20+
}
21+
}
22+
23+
public class ChainHandlerID<S: StateType, E: EventType>
24+
{
25+
internal let bundledHandlerIDs: [HandlerID<S, E>]
26+
27+
internal init(bundledHandlerIDs: [HandlerID<S, E>])
28+
{
29+
self.bundledHandlerIDs = bundledHandlerIDs
30+
}
31+
}

SwiftState/HierarchicalStateMachine.swift

Lines changed: 0 additions & 167 deletions
This file was deleted.

0 commit comments

Comments
 (0)