From 0f76c99c95ee8b8ad3b2f510c88b2cfcdab5c97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sun, 31 Dec 2023 10:20:38 +0300 Subject: [PATCH] d --- Modules/BusX/Bus/src/Bus.swift | 58 ++++++++++++---------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/Modules/BusX/Bus/src/Bus.swift b/Modules/BusX/Bus/src/Bus.swift index d0b3e66..4086f66 100644 --- a/Modules/BusX/Bus/src/Bus.swift +++ b/Modules/BusX/Bus/src/Bus.swift @@ -2,32 +2,17 @@ import Combine import Foundation public enum Bus { - static let e = PassthroughSubject<(key: String, value: Any), Never>() - /**/static var subscriptions = [AnyCancellable]() + private static let e = PassthroughSubject<(key: String, value: Any), Never>() + + public static var events: AnyPublisher<(key: String, value: Any), Never> { + e.eraseToAnyPublisher() + } } public extension Bus { enum Option { case async } - - static var events: AnyPublisher<(key: String, value: Any), Never> { - Self.e.eraseToAnyPublisher() - } -} - -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 { - Self.subscriptions.append(subscription) - } - } } public extension Bus { @@ -35,52 +20,50 @@ public extension Bus { _ keys: Set, _ handler: @escaping ((String, T) -> Void), opt: [Option] = [], - sub: UnsafeMutablePointer<[AnyCancellable]>? = nil + sub subscriptions: inout [AnyCancellable] ) { - var subscription: AnyCancellable? let isAsync = opt.contains(.async) // Async. if isAsync { - subscription = Self.events + Self.events .compactMap { convertKeyValue(keys, $0) } .receive(on: DispatchQueue.main) .sink { v in handler(v.0, v.1) } + .store(in: &subscriptions) } // Async. if !isAsync { - subscription = Self.events + Self.events .compactMap { convertKeyValue(keys, $0) } .sink { v in handler(v.0, v.1) } + .store(in: &subscriptions) } - - subscribe(subscription, sub) } static func send( _ key: String, _ node: AnyPublisher, opt: [Option] = [], - sub: UnsafeMutablePointer<[AnyCancellable]>? = nil + sub subscriptions: inout [AnyCancellable] ) { - var subscription: AnyCancellable? let isAsync = opt.contains(.async) // Async. if isAsync { - subscription = node + node .receive(on: DispatchQueue.main) .sink { v in Self.e.send((key, v)) } + .store(in: &subscriptions) } // Sync. if !isAsync { - subscription = node + node .sink { v in Self.e.send((key, v)) } + .store(in: &subscriptions) } - - subscribe(subscription, sub) } static func send(_ key: String, _ value: Any) { @@ -94,26 +77,25 @@ public extension Bus { _ keyOut: String, _ handler: @escaping ((Src) -> Dst?), opt: [Option] = [], - sub: UnsafeMutablePointer<[AnyCancellable]>? = nil + sub subscriptions: inout [AnyCancellable] ) { - var subscription: AnyCancellable? let isAsync = opt.contains(.async) // Async. if isAsync { - subscription = Self.events + Self.events .compactMap { processKeysValue($0, keysIn, handler) } .receive(on: DispatchQueue.main) .sink { vOut in Self.e.send((keyOut, vOut)) } + .store(in: &subscriptions) } // Sync. if !isAsync { - subscription = Self.events + Self.events .compactMap { processKeysValue($0, keysIn, handler) } .sink { vOut in Self.e.send((keyOut, vOut)) } + .store(in: &subscriptions) } - - subscribe(subscription, sub) } }