99import Foundation
1010import ReSwift
1111
12- public class Router < State: StateType > : StoreSubscriber {
12+ open class Router < State: StateType > : StoreSubscriber {
1313
1414 public typealias NavigationStateSelector = ( State ) -> NavigationState
1515
1616 var store : Store < State >
1717 var lastNavigationState = NavigationState ( )
1818 var routables : [ Routable ] = [ ]
19- let waitForRoutingCompletionQueue = dispatch_queue_create ( " WaitForRoutingCompletionQueue " , nil )
19+ let waitForRoutingCompletionQueue = DispatchQueue ( label : " WaitForRoutingCompletionQueue " , attributes : [ ] )
2020
21- public init ( store: Store < State > , rootRoutable: Routable , stateSelector: NavigationStateSelector ) {
21+ public init ( store: Store < State > , rootRoutable: Routable , stateSelector: @escaping NavigationStateSelector ) {
2222 self . store = store
2323 self . routables. append ( rootRoutable)
2424
2525 self . store. subscribe ( self , selector: stateSelector)
2626 }
2727
28- public func newState( state: NavigationState ) {
28+ open func newState( state: NavigationState ) {
2929 let routingActions = Router . routingActionsForTransitionFrom (
3030 lastNavigationState. route, newRoute: state. route)
3131
3232 routingActions. forEach { routingAction in
3333
34- let semaphore = dispatch_semaphore_create ( 0 )
34+ let semaphore = DispatchSemaphore ( value : 0 )
3535
3636 // Dispatch all routing actions onto this dedicated queue. This will ensure that
3737 // only one routing action can run at any given time. This is important for using this
3838 // Router with UI frameworks. Whenever a navigation action is triggered, this queue will
3939 // block (using semaphore_wait) until it receives a callback from the Routable
4040 // indicating that the navigation action has completed
41- dispatch_async ( waitForRoutingCompletionQueue) {
41+ waitForRoutingCompletionQueue. async {
4242 switch routingAction {
4343
44- case let . Pop ( responsibleRoutableIndex, segmentToBePopped) :
45- dispatch_async ( dispatch_get_main_queue ( ) ) {
44+ case let . pop ( responsibleRoutableIndex, segmentToBePopped) :
45+ DispatchQueue . main . async {
4646 self . routables [ responsibleRoutableIndex]
4747 . popRouteSegment (
4848 segmentToBePopped,
4949 animated: state. changeRouteAnimated) {
50- dispatch_semaphore_signal ( semaphore)
50+ semaphore. signal ( )
5151 }
5252
53- self . routables. removeAtIndex ( responsibleRoutableIndex + 1 )
53+ self . routables. remove ( at : responsibleRoutableIndex + 1 )
5454 }
5555
56- case let . Change ( responsibleRoutableIndex, segmentToBeReplaced, newSegment) :
57- dispatch_async ( dispatch_get_main_queue ( ) ) {
56+ case let . change ( responsibleRoutableIndex, segmentToBeReplaced, newSegment) :
57+ DispatchQueue . main . async {
5858 self . routables [ responsibleRoutableIndex + 1 ] =
5959 self . routables [ responsibleRoutableIndex]
6060 . changeRouteSegment (
6161 segmentToBeReplaced,
6262 to: newSegment,
6363 animated: state. changeRouteAnimated) {
64- dispatch_semaphore_signal ( semaphore)
64+ semaphore. signal ( )
6565 }
6666 }
6767
68- case let . Push ( responsibleRoutableIndex, segmentToBePushed) :
69- dispatch_async ( dispatch_get_main_queue ( ) ) {
68+ case let . push ( responsibleRoutableIndex, segmentToBePushed) :
69+ DispatchQueue . main . async {
7070 self . routables. append (
7171 self . routables [ responsibleRoutableIndex]
7272 . pushRouteSegment (
7373 segmentToBePushed,
7474 animated: state. changeRouteAnimated) {
75- dispatch_semaphore_signal ( semaphore)
75+ semaphore. signal ( )
7676 }
7777 )
7878 }
7979 }
8080
81- let waitUntil = dispatch_time ( DISPATCH_TIME_NOW , Int64 ( 3 * Double( NSEC_PER_SEC) ) )
81+ let waitUntil = DispatchTime . now ( ) + Double ( Int64 ( 3 * Double( NSEC_PER_SEC) ) ) / Double ( NSEC_PER_SEC )
8282
83- let result = dispatch_semaphore_wait ( semaphore, waitUntil)
83+ let result = semaphore. wait ( timeout : waitUntil)
8484
85- if result != 0 {
86- print ( " [SwiftFlowRouter ]: Router is stuck waiting for a " +
85+ if case . timedOut = result {
86+ print ( " [ReSwiftRouter ]: Router is stuck waiting for a " +
8787 " completion handler to be called. Ensure that you have called the " +
8888 " completion handler in each Routable element. " )
8989 print ( " Set a symbolic breakpoint for the `ReSwiftRouterStuck` symbol in order " +
@@ -99,7 +99,7 @@ public class Router<State: StateType>: StoreSubscriber {
9999
100100 // MARK: Route Transformation Logic
101101
102- static func largestCommonSubroute( oldRoute: Route , newRoute: Route ) -> Int {
102+ static func largestCommonSubroute( _ oldRoute: Route , newRoute: Route ) -> Int {
103103 var largestCommonSubroute = - 1
104104
105105 while largestCommonSubroute + 1 < newRoute. count &&
@@ -115,11 +115,11 @@ public class Router<State: StateType>: StoreSubscriber {
115115 // is not represented in the route, e.g.
116116 // route = ["tabBar"]
117117 // routables = [RootRoutable, TabBarRoutable]
118- static func routableIndexForRouteSegment( segment: Int ) -> Int {
118+ static func routableIndexForRouteSegment( _ segment: Int ) -> Int {
119119 return segment + 1
120120 }
121121
122- static func routingActionsForTransitionFrom( oldRoute: Route ,
122+ static func routingActionsForTransitionFrom( _ oldRoute: Route ,
123123 newRoute: Route ) -> [ RoutingActions ] {
124124
125125 var routingActions : [ RoutingActions ] = [ ]
@@ -147,7 +147,7 @@ public class Router<State: StateType>: StoreSubscriber {
147147 while routeBuildingIndex > commonSubroute + 1 {
148148 let routeSegmentToPop = oldRoute [ routeBuildingIndex]
149149
150- let popAction = RoutingActions . Pop (
150+ let popAction = RoutingActions . pop (
151151 responsibleRoutableIndex: routableIndexForRouteSegment ( routeBuildingIndex - 1 ) ,
152152 segmentToBePopped: routeSegmentToPop
153153 )
@@ -160,7 +160,7 @@ public class Router<State: StateType>: StoreSubscriber {
160160 // "The old route had an element after the commonSubroute and the new route does not
161161 // we need to pop the route segment after the commonSubroute"
162162 if oldRoute. count > newRoute. count {
163- let popAction = RoutingActions . Pop (
163+ let popAction = RoutingActions . pop (
164164 responsibleRoutableIndex: routableIndexForRouteSegment ( routeBuildingIndex - 1 ) ,
165165 segmentToBePopped: oldRoute [ routeBuildingIndex]
166166 )
@@ -172,7 +172,7 @@ public class Router<State: StateType>: StoreSubscriber {
172172 // "The new route has a different element after the commonSubroute, we need to replace
173173 // the old route element with the new one"
174174 else if oldRoute. count > ( commonSubroute + 1 ) && newRoute. count > ( commonSubroute + 1 ) {
175- let changeAction = RoutingActions . Change (
175+ let changeAction = RoutingActions . change (
176176 responsibleRoutableIndex: routableIndexForRouteSegment ( commonSubroute) ,
177177 segmentToBeReplaced: oldRoute [ commonSubroute + 1 ] ,
178178 newSegment: newRoute [ commonSubroute + 1 ] )
@@ -189,7 +189,7 @@ public class Router<State: StateType>: StoreSubscriber {
189189 while routeBuildingIndex < newRouteIndex {
190190 let routeSegmentToPush = newRoute [ routeBuildingIndex + 1 ]
191191
192- let pushAction = RoutingActions . Push (
192+ let pushAction = RoutingActions . push (
193193 responsibleRoutableIndex: routableIndexForRouteSegment ( routeBuildingIndex) ,
194194 segmentToBePushed: routeSegmentToPush
195195 )
@@ -206,8 +206,8 @@ public class Router<State: StateType>: StoreSubscriber {
206206func ReSwiftRouterStuck( ) { }
207207
208208enum RoutingActions {
209- case Push ( responsibleRoutableIndex: Int , segmentToBePushed: RouteElementIdentifier )
210- case Pop ( responsibleRoutableIndex: Int , segmentToBePopped: RouteElementIdentifier )
211- case Change ( responsibleRoutableIndex: Int , segmentToBeReplaced: RouteElementIdentifier ,
209+ case push ( responsibleRoutableIndex: Int , segmentToBePushed: RouteElementIdentifier )
210+ case pop ( responsibleRoutableIndex: Int , segmentToBePopped: RouteElementIdentifier )
211+ case change ( responsibleRoutableIndex: Int , segmentToBeReplaced: RouteElementIdentifier ,
212212 newSegment: RouteElementIdentifier )
213213}
0 commit comments