From 37b4955bf4a9d6dfd5e73dcafac247951f251910 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:33:48 +0300 Subject: [PATCH] d --- Modules/BusX/Bus/src/Bus.Processor.swift | 10 +- Modules/BusX/Bus/src/Bus.swift | 121 ++++++++---------- Modules/BusX/BusUI/src/BusUI.Button.swift | 4 +- Modules/BusX/BusUI/src/BusUI.TextField.swift | 8 +- Modules/BusX/BusUI/src/BusUI.Value.swift | 4 +- Modules/MeetupIdX/src/MeetupId.Debounce.swift | 8 +- Modules/MeetupIdX/src/MeetupId.Delay.swift | 8 +- 7 files changed, 76 insertions(+), 87 deletions(-) diff --git a/Modules/BusX/Bus/src/Bus.Processor.swift b/Modules/BusX/Bus/src/Bus.Processor.swift index c116f78..c67d02e 100644 --- a/Modules/BusX/Bus/src/Bus.Processor.swift +++ b/Modules/BusX/Bus/src/Bus.Processor.swift @@ -7,19 +7,17 @@ public extension Bus { public init( _ handler: @escaping ((Src) -> Dst?), _ keyIn: String, - _ keyOut: String, - opt: [Option] = [] + _ keyOut: String ) { - Bus.process([keyIn], keyOut, handler, opt: opt, sub: &subscriptions) + Bus.processSync([keyIn], keyOut, handler, &subscriptions) } public init( _ handler: @escaping ((Src) -> Dst?), _ keysIn: Set, - _ keyOut: String, - opt: [Option] = [] + _ keyOut: String ) { - Bus.process(keysIn, keyOut, handler, opt: opt, sub: &subscriptions) + Bus.processSync(keysIn, keyOut, handler, &subscriptions) } } } diff --git a/Modules/BusX/Bus/src/Bus.swift b/Modules/BusX/Bus/src/Bus.swift index 4086f66..2aec9c0 100644 --- a/Modules/BusX/Bus/src/Bus.swift +++ b/Modules/BusX/Bus/src/Bus.swift @@ -10,92 +10,83 @@ public enum Bus { } public extension Bus { - enum Option { - case async + /// Асинхронно обрабатывать входящие события из шины. + static func receiveAsync( + _ keys: Set, + _ handler: @escaping ((String, T) -> Void), + _ subscriptions: inout [AnyCancellable] + ) { + Self.events + .compactMap { convertKeyValue(keys, $0) } + .receive(on: DispatchQueue.main) + .sink { v in handler(v.0, v.1) } + .store(in: &subscriptions) } -} - -public extension Bus { - static func receive( + + /// Синхронно обрабатывать входящие события из шины. + static func receiveSync( _ keys: Set, _ handler: @escaping ((String, T) -> Void), - opt: [Option] = [], - sub subscriptions: inout [AnyCancellable] + _ subscriptions: inout [AnyCancellable] ) { - let isAsync = opt.contains(.async) - - // Async. - if isAsync { - Self.events - .compactMap { convertKeyValue(keys, $0) } - .receive(on: DispatchQueue.main) - .sink { v in handler(v.0, v.1) } - .store(in: &subscriptions) - } - - // Async. - if !isAsync { - Self.events - .compactMap { convertKeyValue(keys, $0) } - .sink { v in handler(v.0, v.1) } - .store(in: &subscriptions) - } + Self.events + .compactMap { convertKeyValue(keys, $0) } + .sink { v in handler(v.0, v.1) } + .store(in: &subscriptions) } - static func send( + /// Асинхронно отправлять события из узла в шину. + static func sendAsync( _ key: String, _ node: AnyPublisher, - opt: [Option] = [], - sub subscriptions: inout [AnyCancellable] + _ subscriptions: inout [AnyCancellable] ) { - let isAsync = opt.contains(.async) - - // Async. - if isAsync { - node - .receive(on: DispatchQueue.main) - .sink { v in Self.e.send((key, v)) } - .store(in: &subscriptions) - } - - // Sync. - if !isAsync { - node - .sink { v in Self.e.send((key, v)) } - .store(in: &subscriptions) - } + node + .receive(on: DispatchQueue.main) + .sink { v in Self.e.send((key, v)) } + .store(in: &subscriptions) + } + + /// Синхронно отправлять события из узла в шину. + static func sendSync( + _ key: String, + _ node: AnyPublisher, + _ subscriptions: inout [AnyCancellable] + ) { + node + .sink { v in Self.e.send((key, v)) } + .store(in: &subscriptions) } + /// Синхронно отправить событие в шину один раз. static func send(_ key: String, _ value: Any) { Self.e.send((key, value)) } } public extension Bus { - static func process( + static func processAsync( _ keysIn: Set, _ keyOut: String, _ handler: @escaping ((Src) -> Dst?), - opt: [Option] = [], - sub subscriptions: inout [AnyCancellable] + _ subscriptions: inout [AnyCancellable] ) { - let isAsync = opt.contains(.async) - - // Async. - if isAsync { - Self.events - .compactMap { processKeysValue($0, keysIn, handler) } - .receive(on: DispatchQueue.main) - .sink { vOut in Self.e.send((keyOut, vOut)) } - .store(in: &subscriptions) - } + 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 { - Self.events - .compactMap { processKeysValue($0, keysIn, handler) } - .sink { vOut in Self.e.send((keyOut, vOut)) } - .store(in: &subscriptions) - } + static func processSync( + _ keysIn: Set, + _ keyOut: String, + _ handler: @escaping ((Src) -> Dst?), + _ subscriptions: inout [AnyCancellable] + ) { + Self.events + .compactMap { processKeysValue($0, keysIn, handler) } + .sink { vOut in Self.e.send((keyOut, vOut)) } + .store(in: &subscriptions) } } diff --git a/Modules/BusX/BusUI/src/BusUI.Button.swift b/Modules/BusX/BusUI/src/BusUI.Button.swift index 7b1186b..44166aa 100644 --- a/Modules/BusX/BusUI/src/BusUI.Button.swift +++ b/Modules/BusX/BusUI/src/BusUI.Button.swift @@ -6,10 +6,10 @@ extension BusUI { var subscriptions = [AnyCancellable]() public init(_ key: String) { - Bus.send( + Bus.sendSync( key, v.map { true }.eraseToAnyPublisher(), - sub: &subscriptions + &subscriptions ) } } diff --git a/Modules/BusX/BusUI/src/BusUI.TextField.swift b/Modules/BusX/BusUI/src/BusUI.TextField.swift index 174421e..c48ffd7 100644 --- a/Modules/BusX/BusUI/src/BusUI.TextField.swift +++ b/Modules/BusX/BusUI/src/BusUI.TextField.swift @@ -10,19 +10,19 @@ extension BusUI { _ textApp: String, _ textUI: String ) { - Bus.send( + Bus.sendSync( textUI, $v .removeDuplicates() .compactMap(onlyUIText) .eraseToAnyPublisher(), - sub: &subscriptions + &subscriptions ) - Bus.receive( + Bus.receiveSync( [textApp], { [weak self] (_, v: String) in self?.v = "a:\(v)" }, - sub: &subscriptions + &subscriptions ) } } diff --git a/Modules/BusX/BusUI/src/BusUI.Value.swift b/Modules/BusX/BusUI/src/BusUI.Value.swift index 0e4c142..558e4af 100644 --- a/Modules/BusX/BusUI/src/BusUI.Value.swift +++ b/Modules/BusX/BusUI/src/BusUI.Value.swift @@ -10,10 +10,10 @@ extension BusUI { _ defaultValue: T ) { v = defaultValue - Bus.receive( + Bus.receiveSync( [key], { [weak self] (_, v: T) in self?.v = v }, - sub: &subscriptions + &subscriptions ) } } diff --git a/Modules/MeetupIdX/src/MeetupId.Debounce.swift b/Modules/MeetupIdX/src/MeetupId.Debounce.swift index 33de3c7..e603395 100644 --- a/Modules/MeetupIdX/src/MeetupId.Debounce.swift +++ b/Modules/MeetupIdX/src/MeetupId.Debounce.swift @@ -12,19 +12,19 @@ extension MeetupId { _ src: String, _ dst: String ) { - Bus.receive( + Bus.receiveSync( [src], { [weak self] _, v in self?.v.send(v) }, - sub: &subscriptions + &subscriptions ) - Bus.send( + Bus.sendSync( dst, v .debounce(for: .seconds(sec), scheduler: DispatchQueue.main) .compactMap { (v: Src) in handler(v) } .eraseToAnyPublisher(), - sub: &subscriptions + &subscriptions ) } } diff --git a/Modules/MeetupIdX/src/MeetupId.Delay.swift b/Modules/MeetupIdX/src/MeetupId.Delay.swift index 78243d8..7324077 100644 --- a/Modules/MeetupIdX/src/MeetupId.Delay.swift +++ b/Modules/MeetupIdX/src/MeetupId.Delay.swift @@ -12,19 +12,19 @@ extension MeetupId { _ src: String, _ dst: String ) { - Bus.receive( + Bus.receiveSync( [src], { [weak self] _, v in self?.v.send(v) }, - sub: &subscriptions + &subscriptions ) - Bus.send( + Bus.sendSync( dst, v .delay(for: .seconds(sec), scheduler: DispatchQueue.main) .compactMap { (v: Src) in handler(v) } .eraseToAnyPublisher(), - sub: &subscriptions + &subscriptions ) } }