|
|
@@ -1,20 +1,13 @@ |
|
|
|
import Combine |
|
|
|
import SwiftUI |
|
|
|
import Foundation |
|
|
|
|
|
|
|
public enum Bus { } |
|
|
|
|
|
|
|
extension Bus { |
|
|
|
public final class Service { |
|
|
|
static private(set) weak var singleton: Service? |
|
|
|
final class Service { |
|
|
|
static let singleton = Service() |
|
|
|
let broadcaster = PassthroughSubject<(key: String, value: Any), Never>() |
|
|
|
|
|
|
|
deinit { |
|
|
|
Self.singleton = nil |
|
|
|
} |
|
|
|
|
|
|
|
public init() { |
|
|
|
Self.singleton = self |
|
|
|
} |
|
|
|
var subscriptions = Set<AnyCancellable>() |
|
|
|
|
|
|
|
func send(_ key: String, _ value: Any) { |
|
|
|
/**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'") |
|
|
@@ -29,7 +22,7 @@ public extension Bus { |
|
|
|
_ keys: Set<String>, |
|
|
|
_ handler: @escaping ((String, T) -> Void) |
|
|
|
) { |
|
|
|
Service.singleton?.broadcaster |
|
|
|
Service.singleton.broadcaster |
|
|
|
.compactMap { v -> (String, T)? in |
|
|
|
guard |
|
|
|
keys.contains(v.key), |
|
|
@@ -49,7 +42,7 @@ public extension Bus { |
|
|
|
_ keys: Set<String>, |
|
|
|
_ handler: @escaping ((String, T) -> Void) |
|
|
|
) { |
|
|
|
Service.singleton?.broadcaster |
|
|
|
Service.singleton.broadcaster |
|
|
|
.compactMap { v -> (String, T)? in |
|
|
|
guard |
|
|
|
keys.contains(v.key), |
|
|
@@ -70,7 +63,7 @@ public extension Bus { |
|
|
|
) { |
|
|
|
node |
|
|
|
.receive(on: DispatchQueue.main) |
|
|
|
.sink { v in Service.singleton?.send(key, v) } |
|
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
.store(in: &subscriptions) |
|
|
|
} |
|
|
|
|
|
|
@@ -80,12 +73,12 @@ public extension Bus { |
|
|
|
_ node: AnyPublisher<T, Never> |
|
|
|
) { |
|
|
|
node |
|
|
|
.sink { v in Service.singleton?.send(key, v) } |
|
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
.store(in: &subscriptions) |
|
|
|
} |
|
|
|
|
|
|
|
static func sendOnce(_ key: String, _ value: Any) { |
|
|
|
Service.singleton?.send(key, value) |
|
|
|
Service.singleton.send(key, value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -96,7 +89,7 @@ public extension Bus { |
|
|
|
_ keyOut: String, |
|
|
|
_ handler: @escaping ((Src) -> Dst?) |
|
|
|
) { |
|
|
|
Service.singleton?.broadcaster |
|
|
|
Service.singleton.broadcaster |
|
|
|
.compactMap { |
|
|
|
guard |
|
|
|
$0.key == keyIn, |
|
|
@@ -106,7 +99,7 @@ public extension Bus { |
|
|
|
} |
|
|
|
return handler(vIn) |
|
|
|
} |
|
|
|
.sink { vOut in Service.singleton?.send(keyOut, vOut) } |
|
|
|
.sink { vOut in Service.singleton.send(keyOut, vOut) } |
|
|
|
.store(in: &subscriptions) |
|
|
|
} |
|
|
|
} |