|
@@ -22,6 +22,20 @@ 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 receive<T>( |
|
|
static func receive<T>( |
|
|
_ keys: Set<String>, |
|
|
_ keys: Set<String>, |
|
@@ -80,41 +94,32 @@ 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>( |
|
|
|
|
|
_ subscriptions: inout [AnyCancellable], |
|
|
|
|
|
_ handler: @escaping ((Src) -> Dst?), |
|
|
|
|
|
|
|
|
static func process<Src, Dst>( |
|
|
_ keyIn: String, |
|
|
_ keyIn: String, |
|
|
_ keyOut: String |
|
|
|
|
|
|
|
|
_ keyOut: String, |
|
|
|
|
|
_ handler: @escaping ((Src) -> Dst?), |
|
|
|
|
|
opt: [Option] = [], |
|
|
|
|
|
sub: UnsafeMutablePointer<[AnyCancellable]>? = nil |
|
|
) { |
|
|
) { |
|
|
Service.singleton.events |
|
|
|
|
|
|
|
|
var subscription: AnyCancellable? |
|
|
|
|
|
let isAsync = opt.contains(.async) |
|
|
|
|
|
|
|
|
|
|
|
// Async. |
|
|
|
|
|
if isAsync { |
|
|
|
|
|
subscription = Service.singleton.events |
|
|
.compactMap { processKeyValue($0, keyIn, handler) } |
|
|
.compactMap { processKeyValue($0, keyIn, handler) } |
|
|
|
|
|
.receive(on: DispatchQueue.main) |
|
|
.sink { vOut in Service.singleton.send(keyOut, vOut) } |
|
|
.sink { vOut in Service.singleton.send(keyOut, vOut) } |
|
|
.store(in: &subscriptions) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
static func processSyncG<Src, Dst>( |
|
|
|
|
|
_ handler: @escaping ((Src) -> Dst?), |
|
|
|
|
|
_ keyIn: String, |
|
|
|
|
|
_ keyOut: String |
|
|
|
|
|
) { |
|
|
|
|
|
Service.singleton.events |
|
|
|
|
|
|
|
|
// Sync. |
|
|
|
|
|
if !isAsync { |
|
|
|
|
|
subscription = Service.singleton.events |
|
|
.compactMap { processKeyValue($0, keyIn, handler) } |
|
|
.compactMap { processKeyValue($0, keyIn, handler) } |
|
|
.sink { vOut in Service.singleton.send(keyOut, vOut) } |
|
|
.sink { vOut in Service.singleton.send(keyOut, vOut) } |
|
|
.store(in: &Service.singleton.subscriptions) |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
subscribe(subscription, sub) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |