Skip to content

Commit f7ce748

Browse files
committed
Refactor code by separating event-only-driven Machine and StateMachine.
- Add `Machine` (event-only-driven state machine). - Add `Disposable` derived from ReactiveCocoa. - Add Package.swift & change directory structure for Swift-Package-Manager.
1 parent 2986012 commit f7ce748

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2061
-1393
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ DerivedData
1717
*.xcuserstate
1818

1919
Carthage/Build
20+
.build
21+
Packages/

Package.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Package.swift
3+
// SwiftState
4+
//
5+
// Created by Yasuhiro Inami on 2015-12-05.
6+
// Copyright © 2015 Yasuhiro Inami. All rights reserved.
7+
//
8+
9+
import PackageDescription
10+
11+
let package = Package(
12+
name: "SwiftState"
13+
)

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ enum MyEvent: EventType {
7474
let machine = StateMachine<MyState, MyEvent>(state: .State0) { machine in
7575

7676
// add 0 => 1 => 2
77-
machine.addRouteEvent(.Event0, transitions: [
77+
machine.addRoute(event: .Event0, transitions: [
7878
.State0 => .State1,
7979
.State1 => .State2,
8080
])
@@ -89,9 +89,8 @@ machine <-! .Event0
8989
XCTAssertEqual(machine.state, MyState.State2)
9090

9191
// tryEvent
92-
let success = machine <-! .Event0
93-
XCTAssertEqual(machine.state, MyState.State2)
94-
XCTAssertFalse(success, "Event0 doesn't have 2 => Any")
92+
machine <-! .Event0
93+
XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any")
9594
```
9695

9796
If there is no `Event`-based transition, use built-in `NoEvent` instead.
@@ -126,8 +125,9 @@ State Machine | `Machine` | State transition manager which c
126125
Transition | `Transition` | `From-` and `to-` states represented as `.State1 => .State2`. Also, `.Any` can be used to represent _any state_.
127126
Route | `Route` | `Transition` + `Condition`.
128127
Condition | `Context -> Bool` | Closure for validating transition. If condition returns `false`, transition will fail and associated handlers will not be invoked.
129-
Route Mapping | `(event: E?, fromState: S, userInfo: Any?) -> S?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state & event are enum with associated values. Return value (`S?`) means "preferred-toState", where passing `nil` means no routes available. See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info.
130-
Handler | `Context -> Void` | Transition callback invoked after state has been changed.
128+
Event Route Mapping | `(event: E?, fromState: S, userInfo: Any?) -> S?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state & event are enum with associated values. Return value (`S?`) means preferred-`toState`, where passing `nil` means no routes available. See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info.
129+
State Route Mapping | `(fromState: S, userInfo: Any?) -> [S]?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state is enum with associated values. Return value (`[S]?`) means multiple `toState`s from single `fromState`. See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info.
130+
Handler | `Context -> Void` | Transition callback invoked when state has been changed successfully.
131131
Context | `(event: E?, fromState: S, toState: S, userInfo: Any?)` | Closure argument for `Condition` & `Handler`.
132132
Chain | `TransitionChain` / `RouteChain` | Group of continuous routes represented as `.State1 => .State2 => .State3`
133133

Sources/Disposable.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Disposable.swift
3+
// ReactiveCocoa
4+
//
5+
// Created by Justin Spahr-Summers on 2014-06-02.
6+
// Copyright (c) 2014 GitHub. All rights reserved.
7+
//
8+
9+
//
10+
// NOTE:
11+
// This file is a partial copy from ReactiveCocoa v4.0.0-alpha.4 (removing `Atomic` dependency),
12+
// which has not been taken out as microframework yet.
13+
// https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2579
14+
//
15+
// Note that `ActionDisposable` also works as `() -> ()` wrapper to help suppressing warning:
16+
// "Expression resolved to unused function", when returned function was not used.
17+
//
18+
19+
/// Represents something that can be “disposed,” usually associated with freeing
20+
/// resources or canceling work.
21+
public protocol Disposable {
22+
/// Whether this disposable has been disposed already.
23+
var disposed: Bool { get }
24+
25+
func dispose()
26+
}
27+
28+
/// A disposable that will run an action upon disposal.
29+
public final class ActionDisposable: Disposable {
30+
private var action: (() -> ())?
31+
32+
public var disposed: Bool {
33+
return action == nil
34+
}
35+
36+
/// Initializes the disposable to run the given action upon disposal.
37+
public init(action: () -> ()) {
38+
self.action = action
39+
}
40+
41+
public func dispose() {
42+
self.action?()
43+
self.action = nil
44+
}
45+
}

Sources/EventType.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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
12+
13+
/// `EventType` wrapper for handling`.Any` event.
14+
public enum Event<E: EventType>: Hashable
15+
{
16+
case Some(E)
17+
case Any
18+
19+
public var value: E?
20+
{
21+
switch self {
22+
case .Some(let x): return x
23+
default: return nil
24+
}
25+
}
26+
27+
public var hashValue: Int
28+
{
29+
switch self {
30+
case .Some(let x): return x.hashValue
31+
case .Any: return _hashValueForAny
32+
}
33+
}
34+
}
35+
36+
public func == <E: EventType>(lhs: Event<E>, rhs: Event<E>) -> Bool
37+
{
38+
switch (lhs, rhs) {
39+
case let (.Some(x1), .Some(x2)) where x1 == x2:
40+
return true
41+
case (.Any, .Any):
42+
return true
43+
default:
44+
return false
45+
}
46+
}
47+
48+
// MARK: NoEvent
49+
50+
/// Useful for creating StateMachine without events, i.e. `StateMachine<MyState, NoEvent>`.
51+
public enum NoEvent: EventType
52+
{
53+
public var hashValue: Int
54+
{
55+
return 0
56+
}
57+
}
58+
59+
public func == (lhs: NoEvent, rhs: NoEvent) -> Bool
60+
{
61+
return true
62+
}
File renamed without changes.

0 commit comments

Comments
 (0)