This commit is contained in:
Михаил Капелько
2023-12-25 18:27:24 +03:00
parent a77e1c82b4
commit 06c143efb4
4 changed files with 105 additions and 106 deletions

View File

@@ -16,6 +16,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate
window?.rootViewController = vc window?.rootViewController = vc
window?.backgroundColor = UIColor.white window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible() window?.makeKeyAndVisible()
Bus.processSyncG(
MeetupId.shouldFormat, MeetupId.K.meetupIdTextUI.rawValue, MeetupId.K.meetupIdTextApp.rawValue
)
return true return true
} }

30
src/Bus.Aux.swift Normal file
View File

@@ -0,0 +1,30 @@
extension Bus {
/// Пропускаем далее предоставленные ключи.
static func convertKeyValue<T>(
_ keys: Set<String>,
_ 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<Src, Dst>(
_ 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)
}
}

View File

@@ -6,119 +6,87 @@ public enum Bus { }
extension Bus { extension Bus {
final class Service { final class Service {
static let singleton = Service() static let singleton = Service()
let broadcaster = PassthroughSubject<(key: String, value: Any), Never>() let events = PassthroughSubject<(key: String, value: Any), Never>()
var subscriptions = Set<AnyCancellable>() var subscriptions = Set<AnyCancellable>()
func send(_ key: String, _ value: Any) { func send(_ key: String, _ value: Any) {
/**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'") /**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'")
broadcaster.send((key, value)) events.send((key, value))
} }
} }
} }
public extension Bus { public extension Bus {
static func receiveAsync<T>( static func receiveAsync<T>(
_ subscriptions: inout Set<AnyCancellable>, _ subscriptions: inout Set<AnyCancellable>,
_ keys: Set<String>, _ keys: Set<String>,
_ handler: @escaping ((String, T) -> Void) _ handler: @escaping ((String, T) -> Void)
) { ) {
Service.singleton.broadcaster Service.singleton.events
.compactMap { v -> (String, T)? in .compactMap { convertKeyValue(keys, $0) }
guard .receive(on: DispatchQueue.main)
keys.contains(v.key), .sink { v in handler(v.0, v.1) }
let value = v.value as? T .store(in: &subscriptions)
else { }
return nil
}
return (v.key, value)
}
.receive(on: DispatchQueue.main)
.sink { v in handler(v.0, v.1) }
.store(in: &subscriptions)
}
static func receiveSync<T>( static func receiveSync<T>(
_ subscriptions: inout Set<AnyCancellable>, _ subscriptions: inout Set<AnyCancellable>,
_ keys: Set<String>, _ keys: Set<String>,
_ handler: @escaping ((String, T) -> Void) _ handler: @escaping ((String, T) -> Void)
) { ) {
Service.singleton.broadcaster Service.singleton.events
.compactMap { v -> (String, T)? in .compactMap { convertKeyValue(keys, $0) }
guard .sink { v in handler(v.0, v.1) }
keys.contains(v.key), .store(in: &subscriptions)
let value = v.value as? T }
else {
return nil static func sendAsync<T>(
} _ subscriptions: inout Set<AnyCancellable>,
return (v.key, value) _ key: String,
} _ node: AnyPublisher<T, Never>
.sink { v in handler(v.0, v.1) } ) {
.store(in: &subscriptions) node
} .receive(on: DispatchQueue.main)
.sink { v in Service.singleton.send(key, v) }
static func sendAsync<T>( .store(in: &subscriptions)
_ subscriptions: inout Set<AnyCancellable>, }
_ key: String,
_ node: AnyPublisher<T, Never> static func sendSync<T>(
) { _ subscriptions: inout Set<AnyCancellable>,
node _ key: String,
.receive(on: DispatchQueue.main) _ node: AnyPublisher<T, Never>
.sink { v in Service.singleton.send(key, v) } ) {
.store(in: &subscriptions) node
} .sink { v in Service.singleton.send(key, v) }
.store(in: &subscriptions)
static func sendSync<T>( }
_ subscriptions: inout Set<AnyCancellable>,
_ key: String, static func send(_ key: String, _ value: Any) {
_ node: AnyPublisher<T, Never> Service.singleton.send(key, value)
) { }
node
.sink { v in Service.singleton.send(key, v) }
.store(in: &subscriptions)
}
static func sendOnce(_ key: String, _ value: Any) {
Service.singleton.send(key, value)
}
} }
public extension Bus { public extension Bus {
static func processSync<Src, Dst>( static func processSync<Src, Dst>(
_ subscriptions: inout Set<AnyCancellable>, _ subscriptions: inout Set<AnyCancellable>,
_ handler: @escaping ((Src) -> Dst?), _ handler: @escaping ((Src) -> Dst?),
_ keyIn: String, _ keyIn: String,
_ keyOut: String _ keyOut: String
) { ) {
Service.singleton.broadcaster Service.singleton.events
.compactMap { .compactMap { processKeyValue($0, keyIn, handler) }
guard .sink { vOut in Service.singleton.send(keyOut, vOut) }
$0.key == keyIn, .store(in: &subscriptions)
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 processSyncG<Src, Dst>( static func processSyncG<Src, Dst>(
_ handler: @escaping ((Src) -> Dst?), _ handler: @escaping ((Src) -> Dst?),
_ keyIn: String, _ keyIn: String,
_ keyOut: String _ keyOut: String
) { ) {
Service.singleton.broadcaster Service.singleton.events
.compactMap { .compactMap { processKeyValue($0, keyIn, handler) }
guard .sink { vOut in Service.singleton.send(keyOut, vOut) }
$0.key == keyIn, .store(in: &Service.singleton.subscriptions)
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)
}
} }

View File

@@ -23,10 +23,6 @@ enum MeetupId {
} }
return r return r
} }
Bus.processSyncG(
shouldFormat, K.meetupIdTextUI.rawValue, K.meetupIdTextApp.rawValue
)
} }