diff --git a/Modules/BusX/BusUI/src/BusUI.ManyTextField.swift b/Modules/BusX/BusUI/src/BusUI.ManyTextField.swift index 2bb96ad..80edf2e 100644 --- a/Modules/BusX/BusUI/src/BusUI.ManyTextField.swift +++ b/Modules/BusX/BusUI/src/BusUI.ManyTextField.swift @@ -8,13 +8,8 @@ extension BusUI { let textUI: String @Published public var id: String? @Published public var v = "a:" - { - didSet { - /**/print("ИГР BusUMTF.v didSet: '\(v)'") - } - } - var subscriptions = [AnyCancellable]() var sub = [AnyCancellable]() + var subscriptions = [AnyCancellable]() public init( _ textApp: String, @@ -22,17 +17,13 @@ extension BusUI { ) { self.textApp = textApp self.textUI = textUI - - /**///print("ИГР BusUTF(\(Unmanaged.passUnretained(self).toOpaque())).init textA/textU: '\(textApp)'/'\(textUI)'") $id - .sink { [weak self] v in self?.setup(v) } + .sink { [weak self] id in self?.setup(id) } .store(in: &sub) } private func setup(_ id: String?) { - /**/print("ИГР BusUTF(\(Unmanaged.passUnretained(self).toOpaque())).setup id: '\(id)'") - subscriptions = [] Bus.sendSync( @@ -57,7 +48,6 @@ extension BusUI { Bus.receiveSync( [textApp], { [weak self] (_, m: MPAK.Many) in - /**/print("ИГР BusUMTF.receiveS-1 id: '\(id)'") guard let id, m.keys.contains(id), @@ -65,8 +55,6 @@ extension BusUI { else { return } - /**/print("ИГР BusUMTF.receiveS-2 id: '\(id)'") - self?.v = "a:\(text)" }, &subscriptions diff --git a/Modules/BusX/BusUI/src/BusUI.ManyValue.swift b/Modules/BusX/BusUI/src/BusUI.ManyValue.swift new file mode 100644 index 0000000..843bc61 --- /dev/null +++ b/Modules/BusX/BusUI/src/BusUI.ManyValue.swift @@ -0,0 +1,42 @@ +import Combine + +extension BusUI { + public final class Value: ObservableObject { + let key: String + @Published public var id: String? + @Published public var v: T + var sub = [AnyCancellable]() + var subscriptions = [AnyCancellable]() + + public init( + _ key: String, + _ defaultValue: T + ) { + self.key = key + v = defaultValue + + $id + .sink { [weak self] id in self?.setup(id) } + .store(in: &sub) + } + + private func setup(_ id: String?) { + subscriptions = [] + + Bus.receiveSync( + [key], + { [weak self] (_, m: MPAK.Many) in + guard + let id, + m.keys.contains(id), + let v = m.dict[id] + else { + return + } + self?.v = v + }, + &subscriptions + ) + } + } +}