This commit is contained in:
Михаил Капелько
2023-12-16 11:00:13 +03:00
parent 769ade70c9
commit 1e36ef3d71
4 changed files with 72 additions and 134 deletions

View File

@@ -24,25 +24,6 @@ extension Bus {
}
public extension Bus {
static func receive<T>(
_ subscriptions: inout Set<AnyCancellable>,
_ keys: Set<String>,
_ 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 receiveAsync<T>(
_ subscriptions: inout Set<AnyCancellable>,
_ keys: Set<String>,
@@ -62,14 +43,27 @@ public extension Bus {
.sink { v in handler(v.0, v.1) }
.store(in: &subscriptions)
}
static func send(_ key: String, _ value: Any) {
Service.singleton?.send(key, value)
static func receiveSync<T>(
_ subscriptions: inout Set<AnyCancellable>,
_ keys: Set<String>,
_ 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<T: Equatable>(
static func sendAsync<T>(
_ subscriptions: inout Set<AnyCancellable>,
_ key: String,
_ node: AnyPublisher<T, Never>
@@ -79,5 +73,36 @@ public extension Bus {
.sink { v in Service.singleton?.send(key, v) }
.store(in: &subscriptions)
}
*/
static func sendSync<T>(
_ subscriptions: inout Set<AnyCancellable>,
_ key: String,
_ node: AnyPublisher<T, Never>
) {
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 {
static func registerProcessing<Src, Dst>(
_ subscriptions: inout Set<AnyCancellable>,
_ keyIn: String,
_ keyOut: String,
_ handler: @escaping ((Src) -> Dst?)
) {
Service.singleton?.broadcaster
.filter { $0.key == keyIn }
.compactMap {
guard let vIn = $0.value as? Src else { return nil }
return handler(vIn)
}
.sink { vOut in Service.singleton?.send(keyOut, vOut) }
.store(in: &subscriptions)
}
}