@@ -21,24 +21,24 @@ import NIOConcurrencyHelpers
2121public typealias SubscriptionMap = SharedDictionary < String , OrderedSet < DispatcherSubscription > ? >
2222
2323final public class Dispatcher {
24-
24+
2525 public struct DispatchMode {
2626 // swiftlint:disable:next type_name nesting
2727 public enum UI {
2828 case sync, async
2929 }
3030 }
31-
31+
3232 public var subscriptionCount : Int {
3333 return subscriptionMap. innerDictionary. mapValues { set -> Int in
3434 guard let setValue = set else { return 0 }
3535 return setValue. count
3636 }
3737 . reduce ( 0 , { $0 + $1. value } )
3838 }
39-
39+
4040 public static let defaultPriority = 100
41-
41+
4242 private let internalQueue = DispatchQueue ( label: " MiniSwift " , qos: . userInitiated)
4343 private var subscriptionMap = SubscriptionMap ( )
4444 private var middleware = [ Middleware] ( )
@@ -47,27 +47,27 @@ final public class Dispatcher {
4747 private var chain : Chain
4848 private var dispatching : Bool = false
4949 private var subscriptionCounter : Atomic < Int > = Atomic < Int > ( value: 0 )
50-
50+
5151 public init ( ) {
5252 root = RootChain ( map: subscriptionMap)
5353 chain = root
5454 }
55-
55+
5656 private func build( ) -> Chain {
5757 return middleware. reduce ( root, { ( chain: Chain , middleware: Middleware ) -> Chain in
5858 return ForwardingChain { action in
5959 middleware. perform ( action, chain)
6060 }
6161 } )
6262 }
63-
63+
6464 public func add( middleware: Middleware ) {
6565 internalQueue. sync {
6666 self . middleware. append ( middleware)
6767 self . chain = build ( )
6868 }
6969 }
70-
70+
7171 public func remove( middleware: Middleware ) {
7272 internalQueue. sync {
7373 if let index = self . middleware. firstIndex ( where: { middleware. id == $0. id } ) {
@@ -76,21 +76,21 @@ final public class Dispatcher {
7676 chain = build ( )
7777 }
7878 }
79-
79+
8080 public func register( service: Service ) {
8181 internalQueue. sync {
8282 self . service. append ( service)
8383 }
8484 }
85-
85+
8686 public func unregister( service: Service ) {
8787 internalQueue. sync {
8888 if let index = self . service. firstIndex ( where: { service. id == $0. id } ) {
8989 self . service. remove ( at: index)
9090 }
9191 }
9292 }
93-
93+
9494 public func subscribe( priority: Int , tag: String , completion: @escaping ( Action ) -> Void ) -> DispatcherSubscription {
9595 let subscription = DispatcherSubscription (
9696 dispatcher: self ,
@@ -100,7 +100,7 @@ final public class Dispatcher {
100100 completion: completion)
101101 return registerInternal ( subscription: subscription)
102102 }
103-
103+
104104 public func registerInternal( subscription: DispatcherSubscription ) -> DispatcherSubscription {
105105 internalQueue. sync {
106106 if let map = subscriptionMap [ subscription. tag, orPut: OrderedSet < DispatcherSubscription > ( ) ] {
@@ -109,7 +109,7 @@ final public class Dispatcher {
109109 }
110110 return subscription
111111 }
112-
112+
113113 public func unregisterInternal( subscription: DispatcherSubscription ) {
114114 internalQueue. sync {
115115 var removed = false
@@ -121,13 +121,13 @@ final public class Dispatcher {
121121 assert ( removed, " Failed to remove DispatcherSubscription, multiple dispose calls? " )
122122 }
123123 }
124-
124+
125125 public func subscribe< T: Action > ( completion: @escaping ( T ) -> Void ) -> DispatcherSubscription {
126126 return subscribe ( tag: T . tag, completion: { ( action: T ) -> Void in
127127 completion ( action)
128128 } )
129129 }
130-
130+
131131 public func subscribe< T: Action > ( tag: String , completion: @escaping ( T ) -> Void ) -> DispatcherSubscription {
132132 return subscribe ( tag: tag, completion: { object in
133133 if let action = object as? T {
@@ -137,11 +137,11 @@ final public class Dispatcher {
137137 }
138138 } )
139139 }
140-
140+
141141 public func subscribe( tag: String , completion: @escaping ( Action ) -> Void ) -> DispatcherSubscription {
142142 return subscribe ( priority: Dispatcher . defaultPriority, tag: tag, completion: completion)
143143 }
144-
144+
145145 public func dispatch( _ action: Action , mode: Dispatcher . DispatchMode . UI ) {
146146 switch mode {
147147 case . sync:
@@ -158,7 +158,7 @@ final public class Dispatcher {
158158 }
159159 }
160160 }
161-
161+
162162 private func dispatch( _ action: Action ) {
163163 assert ( DispatchQueue . isMain)
164164 internalQueue. sync {
@@ -176,21 +176,21 @@ final public class Dispatcher {
176176 }
177177 }
178178 }
179-
179+
180180 private func getNewSubscriptionId( ) -> Int {
181181 return subscriptionCounter. add ( 1 )
182182 }
183183}
184184
185185public final class DispatcherSubscription : Comparable , Disposable {
186-
186+
187187 private let dispatcher : Dispatcher
188188 public let id : Int
189189 private let priority : Int
190190 private let completion : ( Action ) -> Void
191-
191+
192192 public let tag : String
193-
193+
194194 public init ( dispatcher: Dispatcher ,
195195 id: Int ,
196196 priority: Int ,
@@ -202,33 +202,32 @@ public final class DispatcherSubscription: Comparable, Disposable {
202202 self . tag = tag
203203 self . completion = completion
204204 }
205-
205+
206206 public func dispose( ) {
207207 dispatcher. unregisterInternal ( subscription: self )
208208 }
209-
209+
210210 public func on( _ action: Action ) {
211211 completion ( action)
212212 }
213-
213+
214214 public static func == ( lhs: DispatcherSubscription , rhs: DispatcherSubscription ) -> Bool {
215215 return lhs. id == rhs. id
216216 }
217-
217+
218218 public static func > ( lhs: DispatcherSubscription , rhs: DispatcherSubscription ) -> Bool {
219219 return lhs. priority > rhs. priority
220220 }
221-
221+
222222 public static func < ( lhs: DispatcherSubscription , rhs: DispatcherSubscription ) -> Bool {
223223 return lhs. priority < rhs. priority
224224 }
225-
225+
226226 public static func >= ( lhs: DispatcherSubscription , rhs: DispatcherSubscription ) -> Bool {
227227 return lhs. priority >= rhs. priority
228228 }
229-
229+
230230 public static func <= ( lhs: DispatcherSubscription , rhs: DispatcherSubscription ) -> Bool {
231231 return lhs. priority <= rhs. priority
232232 }
233233}
234-
0 commit comments