@@ -13,91 +13,89 @@ public class Dispatcher {
1313 private static let instance = Dispatcher ( )
1414 private var callbacks : Dictionary < String , AnyObject > = [ : ]
1515 private var _isDispatching = false
16- private var lastId = 0
16+ private var lastDispatchIdentifier = 0
1717 private init ( ) { }
1818}
1919
20- // Public interfaces
2120extension Dispatcher {
2221 public var isDispathing : Bool {
2322 get {
2423 return self . _isDispatching
2524 }
2625 }
2726
28- public class func dispatch< T: Action > ( action: T , result: Result < T . Payload , NSError > ) {
27+ public class func dispatch< T: Action > ( action: T . Type , result: Result < T . Payload , NSError > ) {
2928 instance. dispatch ( action, result: result)
3029 }
3130
32- public class func register< T: Action > ( action: T , handler: ( Result < T . Payload , NSError > ) -> Void ) -> String {
31+ public class func register< T: Action > ( action: T . Type , handler: ( Result < T . Payload , NSError > ) -> Void ) -> String {
3332 return instance. register ( action, handler: handler)
3433 }
3534
36- public class func unregister( id : String ) {
37- instance. unregister ( id )
35+ public class func unregister( identifier : String ) {
36+ instance. unregister ( identifier )
3837 }
3938
40- public class func waitFor< T: Action > ( ids : Array < String > , action: T , result: Result < T . Payload , NSError > ) {
41- instance. waitFor ( ids , action: action, result: result)
39+ public class func waitFor< T: Action > ( identifiers : Array < String > , action: T . Type , result: Result < T . Payload , NSError > ) {
40+ instance. waitFor ( identifiers , action: action, result: result)
4241 }
4342}
4443
45- // Private implementation
4644extension Dispatcher {
47- private func dispatch< T: Action > ( action: T , result: Result < T . Payload , NSError > ) {
45+ private func dispatch< T: Action > ( action: T . Type , result: Result < T . Payload , NSError > ) {
4846 objc_sync_enter ( self )
4947
5048 self . startDispatching ( action)
51- for id in self . callbacks. keys {
52- self . invokeCallback ( id , action: action, result: result)
49+ for identifier in self . callbacks. keys {
50+ self . invokeCallback ( identifier , action: action, result: result)
5351 }
5452 self . finishDispatching ( action)
5553
5654 objc_sync_exit ( self )
5755 }
5856
59- private func register< T: Action > ( action: T , handler: ( Result < T . Payload , NSError > ) -> Void ) -> String {
60- let nextId = " DISPATCH_CALLBACK_ \( ++ self . lastId ) "
61- self . callbacks [ nextId ] = DispatchCallback < T > ( action: action, handler: handler)
62- return nextId
57+ private func register< T: Action > ( action: T . Type , handler: ( Result < T . Payload , NSError > ) -> Void ) -> String {
58+ let nextDispatchIdentifier = " DISPATCH_CALLBACK_ \( ++ self . lastDispatchIdentifier ) "
59+ self . callbacks [ nextDispatchIdentifier ] = DispatchCallback < T > ( action: action, handler: handler)
60+ return nextDispatchIdentifier
6361 }
6462
65- private func unregister( id : String ) {
66- self . callbacks. removeValueForKey ( id )
63+ private func unregister( identifier : String ) {
64+ self . callbacks. removeValueForKey ( identifier )
6765 }
6866
69- private func waitFor< T: Action > ( ids : Array < String > , action: T , result: Result < T . Payload , NSError > ) {
70- for id in ids {
71- if let callback = self . callbacks [ id ] as? DispatchCallback < T > {
67+ private func waitFor< T: Action > ( identifiers : Array < String > , action: T . Type , result: Result < T . Payload , NSError > ) {
68+ for identifier in identifiers {
69+ if let callback = self . callbacks [ identifier ] as? DispatchCallback < T > {
7270 switch callback. status {
7371 case . Handled:
7472 continue
7573 case . Pending:
7674 // Circular dependency detected while
7775 continue
7876 default :
79- self . invokeCallback ( id , action: action, result: result)
77+ self . invokeCallback ( identifier , action: action, result: result)
8078 }
8179 }
8280 }
8381 }
8482
85- private func startDispatching< T: Action > ( action: T ) {
83+ private func startDispatching< T: Action > ( action: T . Type ) {
8684 self . _isDispatching = true
8785
88- for (id , calllback) in self . callbacks {
89- if let callback = self . callbacks [ id ] as? DispatchCallback < T > {
86+ for (identifier , calllback) in self . callbacks {
87+ if let callback = self . callbacks [ identifier ] as? DispatchCallback < T > {
9088 callback. status = DispatchStatus . Waiting
9189 }
9290 }
9391 }
9492
95- private func finishDispatching< T: Action > ( action: T ) {
93+ private func finishDispatching< T: Action > ( action: T . Type ) {
9694 self . _isDispatching = false
9795 }
9896
99- private func invokeCallback< T: Action > ( id : String , action: T , result: Result < T . Payload , NSError > ) {
100- if let callback = self . callbacks [ id ] as? DispatchCallback < T > {
97+ private func invokeCallback< T: Action > ( identifier : String , action: T . Type , result: Result < T . Payload , NSError > ) {
98+ if let callback = self . callbacks [ identifier ] as? DispatchCallback < T > {
10199 callback. status = DispatchStatus . Pending
102100 callback. handler ( result)
103101 callback. status = DispatchStatus . Handled
@@ -106,12 +104,12 @@ extension Dispatcher {
106104}
107105
108106internal class DispatchCallback < T: Action > {
109- let action : T
107+ let action : T . Type
110108 let handler : ( Result < T . Payload , NSError > ) -> Void
111109
112110 var status : DispatchStatus = DispatchStatus . Waiting
113111
114- init ( action: T , handler: ( Result < T . Payload , NSError > ) -> Void ) {
112+ init ( action: T . Type , handler: ( Result < T . Payload , NSError > ) -> Void ) {
115113 self . action = action
116114 self . handler = handler
117115 }
0 commit comments