diff --git a/src/App.swift b/src/App.swift index e287789..9755bf7 100644 --- a/src/App.swift +++ b/src/App.swift @@ -16,6 +16,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate window?.rootViewController = vc window?.backgroundColor = UIColor.white window?.makeKeyAndVisible() + + + Bus.processSyncG( + MeetupId.shouldFormat, MeetupId.K.meetupIdTextUI.rawValue, MeetupId.K.meetupIdTextApp.rawValue + ) return true } diff --git a/src/Bus.Aux.swift b/src/Bus.Aux.swift new file mode 100644 index 0000000..45cf523 --- /dev/null +++ b/src/Bus.Aux.swift @@ -0,0 +1,30 @@ +extension Bus { + /// Пропускаем далее предоставленные ключи. + static func convertKeyValue( + _ keys: Set, + _ v: (key: String, value: Any) + ) -> (String, T)? { + guard + keys.contains(v.key), + let value = v.value as? T + else { + return nil + } + return (v.key, value) + } + + /// Обрабатываем. + static func processKeyValue( + _ v: (key: String, value: Any), + _ keyIn: String, + _ handler: @escaping ((Src) -> Dst?) + ) -> Dst? { + guard + v.key == keyIn, + let vIn = v.value as? Src + else { + return nil + } + return handler(vIn) + } +} diff --git a/src/Bus.swift b/src/Bus.swift index 93a2488..ee132b3 100644 --- a/src/Bus.swift +++ b/src/Bus.swift @@ -6,119 +6,87 @@ public enum Bus { } extension Bus { final class Service { static let singleton = Service() - let broadcaster = PassthroughSubject<(key: String, value: Any), Never>() + let events = PassthroughSubject<(key: String, value: Any), Never>() var subscriptions = Set() func send(_ key: String, _ value: Any) { - /**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'") - broadcaster.send((key, value)) + /**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'") + events.send((key, value)) } } } public extension Bus { - static func receiveAsync( - _ subscriptions: inout Set, - _ keys: Set, - _ handler: @escaping ((String, T) -> Void) - ) { - Service.singleton.broadcaster - .compactMap { v -> (String, T)? in - guard - keys.contains(v.key), - let value = v.value as? T - else { - return nil - } - return (v.key, value) - } - .receive(on: DispatchQueue.main) - .sink { v in handler(v.0, v.1) } - .store(in: &subscriptions) - } + static func receiveAsync( + _ subscriptions: inout Set, + _ keys: Set, + _ handler: @escaping ((String, T) -> Void) + ) { + Service.singleton.events + .compactMap { convertKeyValue(keys, $0) } + .receive(on: DispatchQueue.main) + .sink { v in handler(v.0, v.1) } + .store(in: &subscriptions) + } - static func receiveSync( - _ subscriptions: inout Set, - _ keys: Set, - _ handler: @escaping ((String, T) -> Void) - ) { - Service.singleton.broadcaster - .compactMap { v -> (String, T)? in - guard - keys.contains(v.key), - let value = v.value as? T - else { - return nil - } - return (v.key, value) - } - .sink { v in handler(v.0, v.1) } - .store(in: &subscriptions) - } - - static func sendAsync( - _ subscriptions: inout Set, - _ key: String, - _ node: AnyPublisher - ) { - node - .receive(on: DispatchQueue.main) - .sink { v in Service.singleton.send(key, v) } - .store(in: &subscriptions) - } - - static func sendSync( - _ subscriptions: inout Set, - _ key: String, - _ node: AnyPublisher - ) { - node - .sink { v in Service.singleton.send(key, v) } - .store(in: &subscriptions) - } - - static func sendOnce(_ key: String, _ value: Any) { - Service.singleton.send(key, value) - } + static func receiveSync( + _ subscriptions: inout Set, + _ keys: Set, + _ handler: @escaping ((String, T) -> Void) + ) { + Service.singleton.events + .compactMap { convertKeyValue(keys, $0) } + .sink { v in handler(v.0, v.1) } + .store(in: &subscriptions) + } + + static func sendAsync( + _ subscriptions: inout Set, + _ key: String, + _ node: AnyPublisher + ) { + node + .receive(on: DispatchQueue.main) + .sink { v in Service.singleton.send(key, v) } + .store(in: &subscriptions) + } + + static func sendSync( + _ subscriptions: inout Set, + _ key: String, + _ node: AnyPublisher + ) { + node + .sink { v in Service.singleton.send(key, v) } + .store(in: &subscriptions) + } + + static func send(_ key: String, _ value: Any) { + Service.singleton.send(key, value) + } } public extension Bus { - static func processSync( - _ subscriptions: inout Set, - _ handler: @escaping ((Src) -> Dst?), - _ keyIn: String, - _ keyOut: String - ) { - Service.singleton.broadcaster - .compactMap { - guard - $0.key == keyIn, - let vIn = $0.value as? Src - else { - return nil - } - return handler(vIn) - } - .sink { vOut in Service.singleton.send(keyOut, vOut) } - .store(in: &subscriptions) - } + static func processSync( + _ subscriptions: inout Set, + _ handler: @escaping ((Src) -> Dst?), + _ keyIn: String, + _ keyOut: String + ) { + Service.singleton.events + .compactMap { processKeyValue($0, keyIn, handler) } + .sink { vOut in Service.singleton.send(keyOut, vOut) } + .store(in: &subscriptions) + } - static func processSyncG( - _ handler: @escaping ((Src) -> Dst?), - _ keyIn: String, - _ keyOut: String - ) { - Service.singleton.broadcaster - .compactMap { - guard - $0.key == keyIn, - let vIn = $0.value as? Src - else { - return nil - } - return handler(vIn) - } - .sink { vOut in Service.singleton.send(keyOut, vOut) } - .store(in: &Service.singleton.subscriptions) - } + static func processSyncG( + _ handler: @escaping ((Src) -> Dst?), + _ keyIn: String, + _ keyOut: String + ) { + Service.singleton.events + .compactMap { processKeyValue($0, keyIn, handler) } + .sink { vOut in Service.singleton.send(keyOut, vOut) } + .store(in: &Service.singleton.subscriptions) + } } diff --git a/src/MeetupId.swift b/src/MeetupId.swift index 06dd8dc..353c899 100644 --- a/src/MeetupId.swift +++ b/src/MeetupId.swift @@ -23,10 +23,6 @@ enum MeetupId { } return r } - - Bus.processSyncG( - shouldFormat, K.meetupIdTextUI.rawValue, K.meetupIdTextApp.rawValue - ) }