Михаил Капелько 10 months ago
parent
commit
db39f44aea
2 changed files with 14 additions and 49 deletions
  1. +14
    -24
      Modules/BusX/Bus/src/Bus.swift
  2. +0
    -25
      Modules/BusX/Package.swift

+ 14
- 24
Modules/BusX/Bus/src/Bus.swift View File

@@ -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)


+ 0
- 25
Modules/BusX/Package.swift 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"
),
]
)

Loading…
Cancel
Save