|
@@ -1,7 +1,9 @@ |
|
|
import Combine |
|
|
import Combine |
|
|
import Foundation |
|
|
import Foundation |
|
|
|
|
|
|
|
|
public enum Bus { } |
|
|
|
|
|
|
|
|
public enum Bus { |
|
|
|
|
|
static let e = PassthroughSubject<(key: String, value: Any), Never>() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public extension Bus { |
|
|
public extension Bus { |
|
|
enum Option { |
|
|
enum Option { |
|
@@ -9,19 +11,7 @@ public extension Bus { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static var events: AnyPublisher<(key: String, value: Any), Never> { |
|
|
static var events: AnyPublisher<(key: String, value: Any), Never> { |
|
|
Service.singleton.events.eraseToAnyPublisher() |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
extension Bus { |
|
|
|
|
|
final class Service { |
|
|
|
|
|
static let singleton = Service() |
|
|
|
|
|
let events = PassthroughSubject<(key: String, value: Any), Never>() |
|
|
|
|
|
var subscriptions = [AnyCancellable]() |
|
|
|
|
|
|
|
|
|
|
|
func send(_ key: String, _ value: Any) { |
|
|
|
|
|
events.send((key, value)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Self.e.eraseToAnyPublisher() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -34,7 +24,7 @@ private extension Bus { |
|
|
if let sub = sub { |
|
|
if let sub = sub { |
|
|
sub.pointee.append(subscription) |
|
|
sub.pointee.append(subscription) |
|
|
} else { |
|
|
} else { |
|
|
Service.singleton.subscriptions.append(subscription) |
|
|
|
|
|
|
|
|
Self.subscriptions.append(subscription) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@@ -51,7 +41,7 @@ public extension Bus { |
|
|
|
|
|
|
|
|
// Async. |
|
|
// Async. |
|
|
if isAsync { |
|
|
if isAsync { |
|
|
subscription = Service.singleton.events |
|
|
|
|
|
|
|
|
subscription = Self.events |
|
|
.compactMap { convertKeyValue(keys, $0) } |
|
|
.compactMap { convertKeyValue(keys, $0) } |
|
|
.receive(on: DispatchQueue.main) |
|
|
.receive(on: DispatchQueue.main) |
|
|
.sink { v in handler(v.0, v.1) } |
|
|
.sink { v in handler(v.0, v.1) } |
|
@@ -59,7 +49,7 @@ public extension Bus { |
|
|
|
|
|
|
|
|
// Async. |
|
|
// Async. |
|
|
if !isAsync { |
|
|
if !isAsync { |
|
|
subscription = Service.singleton.events |
|
|
|
|
|
|
|
|
subscription = Self.events |
|
|
.compactMap { convertKeyValue(keys, $0) } |
|
|
.compactMap { convertKeyValue(keys, $0) } |
|
|
.sink { v in handler(v.0, v.1) } |
|
|
.sink { v in handler(v.0, v.1) } |
|
|
} |
|
|
} |
|
@@ -80,20 +70,20 @@ public extension Bus { |
|
|
if isAsync { |
|
|
if isAsync { |
|
|
subscription = node |
|
|
subscription = node |
|
|
.receive(on: DispatchQueue.main) |
|
|
.receive(on: DispatchQueue.main) |
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
|
|
|
|
|
.sink { v in Self.e.send(key, v) } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Sync. |
|
|
// Sync. |
|
|
if !isAsync { |
|
|
if !isAsync { |
|
|
subscription = node |
|
|
subscription = node |
|
|
.sink { v in Service.singleton.send(key, v) } |
|
|
|
|
|
|
|
|
.sink { v in Self.e.send(key, v) } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
subscribe(subscription, sub) |
|
|
subscribe(subscription, sub) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static func send(_ key: String, _ value: Any) { |
|
|
static func send(_ key: String, _ value: Any) { |
|
|
Service.singleton.send(key, value) |
|
|
|
|
|
|
|
|
Self.e.send(key, value) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -110,17 +100,17 @@ public extension Bus { |
|
|
|
|
|
|
|
|
// Async. |
|
|
// Async. |
|
|
if isAsync { |
|
|
if isAsync { |
|
|
subscription = Service.singleton.events |
|
|
|
|
|
|
|
|
subscription = Self.events |
|
|
.compactMap { processKeysValue($0, keysIn, handler) } |
|
|
.compactMap { processKeysValue($0, keysIn, handler) } |
|
|
.receive(on: DispatchQueue.main) |
|
|
.receive(on: DispatchQueue.main) |
|
|
.sink { vOut in Service.singleton.send(keyOut, vOut) } |
|
|
|
|
|
|
|
|
.sink { vOut in Self.e.send(keyOut, vOut) } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Sync. |
|
|
// Sync. |
|
|
if !isAsync { |
|
|
if !isAsync { |
|
|
subscription = Service.singleton.events |
|
|
|
|
|
|
|
|
subscription = Self.events |
|
|
.compactMap { processKeysValue($0, keysIn, handler) } |
|
|
.compactMap { processKeysValue($0, keysIn, handler) } |
|
|
.sink { vOut in Service.singleton.send(keyOut, vOut) } |
|
|
|
|
|
|
|
|
.sink { vOut in Self.e.send(keyOut, vOut) } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
subscribe(subscription, sub) |
|
|
subscribe(subscription, sub) |
|
|