This commit is contained in:
Михаил Капелько
2023-12-31 10:12:05 +03:00
parent 42ab87a0fb
commit db39f44aea
2 changed files with 14 additions and 49 deletions

View File

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

View File

@@ -1,25 +0,0 @@
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "BusX",
platforms: [.iOS(.v14)],
products: [
.library(
name: "BusX",
type: .dynamic,
targets: ["BusX"]
),
],
dependencies: [
],
targets: [
.target(
name: "BusX",
dependencies: [
],
path: "src"
),
]
)