|
@@ -3,11 +3,17 @@ import Foundation |
|
|
|
|
|
|
|
|
public enum Bus { } |
|
|
public enum Bus { } |
|
|
|
|
|
|
|
|
|
|
|
public extension Bus { |
|
|
|
|
|
enum Option { |
|
|
|
|
|
case async |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
extension Bus { |
|
|
extension Bus { |
|
|
final class Service { |
|
|
final class Service { |
|
|
static let singleton = Service() |
|
|
static let singleton = Service() |
|
|
let events = PassthroughSubject<(key: String, value: Any), Never>() |
|
|
let events = PassthroughSubject<(key: String, value: Any), Never>() |
|
|
var subscriptions = Set<AnyCancellable>() |
|
|
|
|
|
|
|
|
var subscriptions = [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)'") |
|
@@ -17,48 +23,56 @@ extension Bus { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public extension Bus { |
|
|
public extension Bus { |
|
|
static func receiveAsync<T>( |
|
|
|
|
|
_ subscriptions: inout Set<AnyCancellable>, |
|
|
|
|
|
|
|
|
static func receive<T>( |
|
|
_ keys: Set<String>, |
|
|
_ keys: Set<String>, |
|
|
_ handler: @escaping ((String, T) -> Void) |
|
|
|
|
|
|
|
|
_ handler: @escaping ((String, T) -> Void), |
|
|
|
|
|
opt: [Option] = [], |
|
|
|
|
|
sub: UnsafeMutablePointer<[AnyCancellable]>? = nil |
|
|
) { |
|
|
) { |
|
|
Service.singleton.events |
|
|
|
|
|
.compactMap { convertKeyValue(keys, $0) } |
|
|
|
|
|
.receive(on: DispatchQueue.main) |
|
|
|
|
|
.sink { v in handler(v.0, v.1) } |
|
|
|
|
|
.store(in: &subscriptions) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var subscription: AnyCancellable? |
|
|
|
|
|
let isAsync = opt.contains(.async) |
|
|
|
|
|
|
|
|
static func receiveSync<T>( |
|
|
|
|
|
_ subscriptions: inout Set<AnyCancellable>, |
|
|
|
|
|
_ keys: Set<String>, |
|
|
|
|
|
_ 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<T>( |
|
|
|
|
|
_ subscriptions: inout Set<AnyCancellable>, |
|
|
|
|
|
_ key: String, |
|
|
|
|
|
_ node: AnyPublisher<T, Never> |
|
|
|
|
|
) { |
|
|
|
|
|
node |
|
|
|
|
|
.receive(on: DispatchQueue.main) |
|
|
|
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
|
|
.store(in: &subscriptions) |
|
|
|
|
|
|
|
|
// Async. |
|
|
|
|
|
if isAsync { |
|
|
|
|
|
subscription = Service.singleton.events |
|
|
|
|
|
.compactMap { convertKeyValue(keys, $0) } |
|
|
|
|
|
.receive(on: DispatchQueue.main) |
|
|
|
|
|
.sink { v in handler(v.0, v.1) } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Async. |
|
|
|
|
|
if !isAsync { |
|
|
|
|
|
subscription = Service.singleton.events |
|
|
|
|
|
.compactMap { convertKeyValue(keys, $0) } |
|
|
|
|
|
.sink { v in handler(v.0, v.1) } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
subscribe(subscription, sub) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static func sendSync<T>( |
|
|
|
|
|
_ subscriptions: inout Set<AnyCancellable>, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func send<T>( |
|
|
_ key: String, |
|
|
_ key: String, |
|
|
_ node: AnyPublisher<T, Never> |
|
|
|
|
|
|
|
|
_ node: AnyPublisher<T, Never>, |
|
|
|
|
|
opt: [Option] = [], |
|
|
|
|
|
sub: UnsafeMutablePointer<[AnyCancellable]>? = nil |
|
|
) { |
|
|
) { |
|
|
node |
|
|
|
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
|
|
.store(in: &subscriptions) |
|
|
|
|
|
|
|
|
var subscription: AnyCancellable? |
|
|
|
|
|
let isAsync = opt.contains(.async) |
|
|
|
|
|
|
|
|
|
|
|
// Async. |
|
|
|
|
|
if isAsync { |
|
|
|
|
|
subscription = node |
|
|
|
|
|
.receive(on: DispatchQueue.main) |
|
|
|
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Sync. |
|
|
|
|
|
if !isAsync { |
|
|
|
|
|
subscription = node |
|
|
|
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
subscribe(subscription, sub) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static func send(_ key: String, _ value: Any) { |
|
|
static func send(_ key: String, _ value: Any) { |
|
@@ -66,9 +80,23 @@ public extension Bus { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private extension Bus { |
|
|
|
|
|
static func subscribe( |
|
|
|
|
|
_ subscription: AnyCancellable?, |
|
|
|
|
|
_ sub: UnsafeMutablePointer<[AnyCancellable]>? |
|
|
|
|
|
) { |
|
|
|
|
|
guard let subscription else { return } |
|
|
|
|
|
if let sub = sub { |
|
|
|
|
|
sub.pointee.append(subscription) |
|
|
|
|
|
} else { |
|
|
|
|
|
Service.singleton.subscriptions.append(subscription) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public extension Bus { |
|
|
public extension Bus { |
|
|
static func processSync<Src, Dst>( |
|
|
static func processSync<Src, Dst>( |
|
|
_ subscriptions: inout Set<AnyCancellable>, |
|
|
|
|
|
|
|
|
_ subscriptions: inout [AnyCancellable], |
|
|
_ handler: @escaping ((Src) -> Dst?), |
|
|
_ handler: @escaping ((Src) -> Dst?), |
|
|
_ keyIn: String, |
|
|
_ keyIn: String, |
|
|
_ keyOut: String |
|
|
_ keyOut: String |
|
|