diff --git a/src/App.swift b/src/App.swift index c35f2f9..e287789 100644 --- a/src/App.swift +++ b/src/App.swift @@ -3,7 +3,6 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - let fmt = MeetupId.MeetupIdFormatter() var window: UIWindow? func application( diff --git a/src/Bus.swift b/src/Bus.swift index 9353143..93a2488 100644 --- a/src/Bus.swift +++ b/src/Bus.swift @@ -85,9 +85,9 @@ public extension Bus { public extension Bus { static func processSync( _ subscriptions: inout Set, + _ handler: @escaping ((Src) -> Dst?), _ keyIn: String, - _ keyOut: String, - _ handler: @escaping ((Src) -> Dst?) + _ keyOut: String ) { Service.singleton.broadcaster .compactMap { @@ -102,4 +102,23 @@ public extension Bus { .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) + } } diff --git a/src/MeetupId.swift b/src/MeetupId.swift index 5435b8a..06dd8dc 100644 --- a/src/MeetupId.swift +++ b/src/MeetupId.swift @@ -2,7 +2,7 @@ import Combine import Foundation enum MeetupId { - enum Keys: String, RawRepresentable { + enum K: String, RawRepresentable { case meetupIdTextApp case meetupIdTextUI } @@ -24,21 +24,9 @@ enum MeetupId { return r } - final class MeetupIdFormatter { - var subscriptions = Set() + Bus.processSyncG( + shouldFormat, K.meetupIdTextUI.rawValue, K.meetupIdTextApp.rawValue + ) +} - deinit { - /**/print("ИГР MeetupIF.DEinit") - } - init() { - Bus.processSync( - &subscriptions, - Keys.meetupIdTextUI.rawValue, - Keys.meetupIdTextApp.rawValue, - shouldFormat - ) - /**/print("ИГР MeetupIF.init") - } - } -} diff --git a/src/VM.swift b/src/VM.swift index f79d2c5..5fbce07 100644 --- a/src/VM.swift +++ b/src/VM.swift @@ -9,7 +9,7 @@ final class VM: ObservableObject { init() { Bus.sendSync( &subscriptions, - MeetupId.Keys.meetupIdTextUI.rawValue, + MeetupId.K.meetupIdTextUI.rawValue, $text // Исключаем конфликты от UI и App путём игнорирования спама. .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main) @@ -22,7 +22,7 @@ final class VM: ObservableObject { Bus.receiveAsync( &subscriptions, - [MeetupId.Keys.meetupIdTextApp.rawValue], + [MeetupId.K.meetupIdTextApp.rawValue], { [weak self] (_, v: String) in self?.text = "a:\(v)" } ) }