Skip to content

Commit c1c18ec

Browse files
committed
Add addStateRouteMapping() & rename EventRouteMapping to RouteMapping.
1 parent e9c1bf3 commit c1c18ec

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ If there is no `Event`-based transition, use built-in `NoEvent` instead.
100100

101101
### State & Event enums with associated values
102102

103-
Above examples use _arrow-style routing_ which are easy to understand, but it lacks in ability to handle state & event enums with associated values. In such cases, use `machine.addRouteMapping()` and pass either of the following closure types (_closure-style routing_):
103+
Above examples use _arrow-style routing_ which are easy to understand, but it lacks in ability to handle state & event enums with associated values. In such cases, use either of the following functions to apply _closure-style routing_:
104104

105-
- `EventRouteMapping`: `(event: E?, fromState: S, userInfo: Any?) -> S?`
106-
- `StateRouteMapping`: `(fromState: S, userInfo: Any?) -> [S]?`
105+
- `machine.addRouteMapping(routeMapping)`
106+
- `RouteMapping`: `(event: E?, fromState: S, userInfo: Any?) -> S?`
107+
- `machine.addStateRouteMapping(stateRouteMapping)`
108+
- `StateRouteMapping`: `(fromState: S, userInfo: Any?) -> [S]?`
109+
- This is a synonym for multiple routing e.g. `.State0 => [.State1, .State2]`
107110

108111
For example:
109112

@@ -117,7 +120,6 @@ enum StrEvent: EventType {
117120

118121
let machine = Machine<StrState, StrEvent>(state: .Str("initial")) { machine in
119122

120-
// add EventRouteMapping
121123
machine.addRouteMapping { event, fromState, userInfo -> StrState? in
122124
// no route for no-event
123125
guard let event = event else { return nil }
@@ -164,7 +166,7 @@ machine <-! .Str("finish")
164166
XCTAssertEqual(machine.state, StrState.Str("end"))
165167
```
166168

167-
This behaves very similar to JavaScript's safe state-container [rackt/Redux](https://github.com/rackt/redux), where `EventRouteMapping` can be interpretted as `Redux.Reducer`.
169+
This behaves very similar to JavaScript's safe state-container [rackt/Redux](https://github.com/rackt/redux), where `RouteMapping` can be interpretted as `Redux.Reducer`.
168170

169171
For more examples, please see XCTest cases.
170172

Sources/Machine.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/// This is a superclass (simpler version) of `StateMachine` that doesn't allow `tryState()` (direct state change).
1313
///
1414
/// This class can be used as a safe state-container in similar way as [rackt/Redux](https://github.com/rackt/redux),
15-
/// where `EventRouteMapping` can be interpretted as `Redux.Reducer`.
15+
/// where `RouteMapping` can be interpretted as `Redux.Reducer`.
1616
///
1717
public class Machine<S: StateType, E: EventType>
1818
{
@@ -28,12 +28,12 @@ public class Machine<S: StateType, E: EventType>
2828

2929
/// Closure-based route, mainly for `tryEvent()` (and also works for subclass's `tryState()`).
3030
/// - Returns: Preferred `toState`.
31-
public typealias EventRouteMapping = (event: E?, fromState: S, userInfo: Any?) -> S?
31+
public typealias RouteMapping = (event: E?, fromState: S, userInfo: Any?) -> S?
3232

3333
internal typealias _RouteDict = [Transition<S> : [String : Condition?]]
3434

3535
private lazy var _routes: [Event<E> : _RouteDict] = [:]
36-
private lazy var _routeMappings: [String : EventRouteMapping] = [:]
36+
private lazy var _routeMappings: [String : RouteMapping] = [:]
3737

3838
/// `tryEvent()`-based handler collection.
3939
private lazy var _handlers: [Event<E> : [_HandlerInfo<S, E>]] = [:]
@@ -356,12 +356,12 @@ public class Machine<S: StateType, E: EventType>
356356
}
357357

358358
//--------------------------------------------------
359-
// MARK: - EventRouteMapping
359+
// MARK: - RouteMapping
360360
//--------------------------------------------------
361361

362362
// MARK: addRouteMapping
363363

364-
public func addRouteMapping(routeMapping: EventRouteMapping) -> Disposable
364+
public func addRouteMapping(routeMapping: RouteMapping) -> Disposable
365365
{
366366
let key = _createUniqueString()
367367

@@ -376,7 +376,7 @@ public class Machine<S: StateType, E: EventType>
376376

377377
// MARK: addRouteMapping + conditional handler
378378

379-
public func addRouteMapping(routeMapping: EventRouteMapping, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable
379+
public func addRouteMapping(routeMapping: RouteMapping, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable
380380
{
381381
let routeDisposable = self.addRouteMapping(routeMapping)
382382

Sources/StateMachine.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
public final class StateMachine<S: StateType, E: EventType>: Machine<S, E>
1616
{
1717
/// Closure-based routes for `tryState()`.
18-
/// - Returns: Multiple `toState`s from single `fromState`.
18+
/// - Returns: Multiple `toState`s from single `fromState`, similar to `.State0 => [.State1, .State2]`
1919
public typealias StateRouteMapping = (fromState: S, userInfo: Any?) -> [S]?
2020

2121
private lazy var _routes: _RouteDict = [:]
22-
private lazy var _routeMappings: [String : StateRouteMapping] = [:] // NOTE: `StateRouteMapping`, not `EventRouteMapping`
22+
private lazy var _routeMappings: [String : StateRouteMapping] = [:] // NOTE: `StateRouteMapping`, not `RouteMapping`
2323

2424
/// `tryState()`-based handler collection.
2525
private lazy var _handlers: [Transition<S> : [_HandlerInfo<S, E>]] = [:]
@@ -442,9 +442,9 @@ public final class StateMachine<S: StateType, E: EventType>: Machine<S, E>
442442
// MARK: - StateRouteMapping
443443
//--------------------------------------------------
444444

445-
// MARK: addRouteMapping
445+
// MARK: addStateRouteMapping
446446

447-
public func addRouteMapping(routeMapping: StateRouteMapping) -> Disposable
447+
public func addStateRouteMapping(routeMapping: StateRouteMapping) -> Disposable
448448
{
449449
let key = _createUniqueString()
450450

@@ -453,15 +453,15 @@ public final class StateMachine<S: StateType, E: EventType>: Machine<S, E>
453453
let routeMappingID = _RouteMappingID(key: key)
454454

455455
return ActionDisposable.init { [weak self] in
456-
self?._removeRouteMapping(routeMappingID)
456+
self?._removeStateRouteMapping(routeMappingID)
457457
}
458458
}
459459

460-
// MARK: addRouteMapping + conditional handler
460+
// MARK: addStateRouteMapping + conditional handler
461461

462-
public func addRouteMapping(routeMapping: StateRouteMapping, handler: Handler) -> Disposable
462+
public func addStateRouteMapping(routeMapping: StateRouteMapping, handler: Handler) -> Disposable
463463
{
464-
let routeDisposable = self.addRouteMapping(routeMapping)
464+
let routeDisposable = self.addStateRouteMapping(routeMapping)
465465

466466
let handlerDisposable = self.addHandler(.Any => .Any) { context in
467467
if self._hasRouteMappingInDict(fromState: context.fromState, toState: context.toState, userInfo: context.userInfo) != nil {
@@ -475,9 +475,9 @@ public final class StateMachine<S: StateType, E: EventType>: Machine<S, E>
475475
}
476476
}
477477

478-
// MARK: removeRouteMapping
478+
// MARK: removeStateRouteMapping
479479

480-
private func _removeRouteMapping(routeMappingID: _RouteMappingID) -> Bool
480+
private func _removeStateRouteMapping(routeMappingID: _RouteMappingID) -> Bool
481481
{
482482
if self._routeMappings[routeMappingID.key] != nil {
483483
self._routeMappings[routeMappingID.key] = nil

Tests/BasicTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class BasicTests: _TestCase
8080
{
8181
let machine = Machine<StrState, StrEvent>(state: .Str("initial")) { machine in
8282

83-
// add EventRouteMapping
8483
machine.addRouteMapping { event, fromState, userInfo -> StrState? in
8584
// no route for no-event
8685
guard let event = event else { return nil }

Tests/MachineTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,15 @@ class MachineTests: _TestCase
299299
}
300300

301301
//--------------------------------------------------
302-
// MARK: - EventRouteMapping
302+
// MARK: - RouteMapping
303303
//--------------------------------------------------
304304

305-
func testAddEventRouteMapping()
305+
func testAddRouteMapping()
306306
{
307307
var invokeCount = 0
308308

309309
let machine = Machine<StrState, StrEvent>(state: .Str("initial")) { machine in
310310

311-
// add EventRouteMapping
312311
machine.addRouteMapping { event, fromState, userInfo -> StrState? in
313312
// no route for no-event
314313
guard let event = event else { return nil }

Tests/RouteMappingTests.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class RouteMappingTests: _TestCase
8080

8181
let machine = StateMachine<_State, _Event>(state: .Pending) { machine in
8282

83-
// add EventRouteMapping
8483
machine.addRouteMapping { event, fromState, userInfo in
8584
// no routes for no event
8685
guard let event = event else {
@@ -138,8 +137,11 @@ class RouteMappingTests: _TestCase
138137

139138
let machine = StateMachine<_State, _Event>(state: .Pending) { machine in
140139

141-
// add StateRouteMapping
142-
machine.addRouteMapping { fromState, userInfo in
140+
// Add following routes:
141+
// - `.Pending => .Loading(1)`
142+
// - `.Loading(x) => .Loading(x+10)`
143+
// - `.Loading(x) => .Loading(x+100)`
144+
machine.addStateRouteMapping { fromState, userInfo in
143145
switch fromState {
144146
case .Pending:
145147
return [.Loading(1)]

Tests/StateMachineTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ class StateMachineTests: _TestCase
554554

555555
let machine = StateMachine<MyState, NoEvent>(state: .State0) { machine in
556556

557-
// add 0 => 1 & 0 => 2 (using `StateRouteMapping` closure)
558-
routeMappingDisposable = machine.addRouteMapping { fromState, userInfo -> [MyState]? in
557+
// add 0 => 1 & 0 => 2
558+
routeMappingDisposable = machine.addStateRouteMapping { fromState, userInfo -> [MyState]? in
559559
if fromState == .State0 {
560560
return [.State1, .State2]
561561
}
@@ -564,7 +564,7 @@ class StateMachineTests: _TestCase
564564
}
565565
}
566566

567-
// add 1 => 0 (can also use `EventRouteMapping` closure for single-`toState`)
567+
// add 1 => 0 (can also use `RouteMapping` closure for single-`toState`)
568568
machine.addRouteMapping { event, fromState, userInfo -> MyState? in
569569
guard event == nil else { return nil }
570570

0 commit comments

Comments
 (0)